2
votes

I am trying to calculate Normalized Gini Coefficient in tensorflow but am unable to do so. I have the below python code for the same executed in numpy but I want to implement it using tensorflow. If any ideas, please help. I will be having actual with the tensor shape (1,?) and pred with tensor shape (1,?)

Python Code:

def gini(actual, pred, cmpcol = 0, sortcol = 1):
     assert( len(actual) == len(pred) )
     all = np.asarray(np.c_[ actual, pred, np.arange(len(actual)) ], dtype=np.float)
     all = all[ np.lexsort((all[:,2], -1*all[:,1])) ]
     totalLosses = all[:,0].sum()
     giniSum = all[:,0].cumsum().sum() / totalLosses

     giniSum -= (len(actual) + 1) / 2.
     return giniSum / len(actual)

 def gini_normalized(a, p):
     return gini(a, p) / gini(a, a)
2

2 Answers

0
votes

Here's a tensorflow version (uses tf.nn.top_k instead of np.lexsort for sorting).

def gini_tf(actual, pred):
  assert (len(actual) == len(pred))
  n = int(actual.get_shape()[-1])
  indices = tf.reverse(tf.nn.top_k(pred, k=n)[1], axis=0)
  a_s = tf.gather(actual, indices)
  a_c = tf.cumsum(a_s)
  giniSum = tf.reduce_sum(a_c) / tf.reduce_sum(a_s)
  giniSum -= (n + 1) / 2.
  return giniSum / n

gini_normalized doesn't change. By the way, looks like your version ignores cmpcol and sortcol arguments.

0
votes

Here's a working solution.

def gini(actual, pred):
    n = tf.shape(actual)[1]
    indices = tf.reverse(tf.nn.top_k(pred, k=n)[1], axis=[1])[0]
    a_s = tf.gather(tf.transpose(actual), tf.transpose(indices))
    a_c = tf.cumsum(a_s)
    giniSum = tf.reduce_sum(a_c) / tf.reduce_sum(a_s)
    giniSum = tf.subtract(giniSum, tf.divide(tf.to_float(n + 1), tf.constant(2.)))
    return giniSum / tf.to_float(n)

def gini_normalized(a, p):
    return gini(a, p) / gini(a, a)