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)