In [1]:
import numpy as np
In [175]:
def _last_axis_binary_clf_curve(y_true, y_predicted):
"""
returns y_predicted.shape[-2] binary clf curves calculated axis[-1]-wise
"""
assert y_true.shape == y_predicted.shape
axis = -1
sort_idx = list(np.ogrid[[slice(x) for x in predicted.shape]])
sort_idx[axis] = predicted.argsort(axis=axis)
reverse = [slice(None)] * predicted.ndim
reverse[axis] = slice(None, None, -1)
sorted_y_predicted = predicted[sort_idx][reverse]
sorted_y_true = true[sort_idx][reverse]
tps = sorted_y_true.cumsum(axis=axis)
count = (np.ones(y_predicted.shape) * np.arange(y_predicted.shape[-1]))
fps = 1 + count - tps
threshold_values = sorted_y_predicted
return fps, tps, threshold_values
def last_axis_roc_curve(y_true, y_predicted):
fps, tps, thresholds = _last_axis_binary_clf_curve(y_true, y_predicted)
i = [slice(None)] * fps.ndim
i[-1] = -1
fpr = fps.astype('float32') / fps[i][:, np.newaxis]
tpr = tps.astype('float32') / tps[i][:, np.newaxis]
return fpr, tpr, thresholds
In [179]:
true = np.random.binomial(n=1, p=.5, size=(2, 10))
predicted = np.random.random((2, 10))
print true
print predicted
In [174]:
a = np.asarray([[1, 1, 0],
[0, 1, 1]])
a.cumsum(axis=-1)
Out[174]:
In [180]:
fps, tps, thresh = _last_axis_binary_clf_curve(true, predicted)
print 'fps'
print fps
print 'tps'
print tps
print 'thresh'
print thresh
#i = [slice(None)] * fps.ndim
#i[-1] = -1
#print fps / fps[i][:, np.newaxis]
fpr, tpr, thresh2 = last_axis_roc_curve(true, predicted)
print 'fpr'
print fpr
print 'tpr'
print tpr
In [181]:
np.trapz(tpr, fpr)
Out[181]:
In [142]:
fps.shape
Out[142]:
In [5]:
predicted.ndim
Out[5]:
In [6]:
axis = -1
In [18]:
sort_idx[axis] = predicted.argsort(axis=axis)
sort_idx
Out[18]:
In [19]:
predicted[sort_idx]
Out[19]:
In [9]:
true[sort_idx]
Out[9]:
In [10]:
true[sort_idx].cumsum(axis=axis)
Out[10]:
In [11]:
true
Out[11]:
In [12]:
predicted
Out[12]:
In [26]:
predicted[..., predicted.argsort()].shape
Out[26]:
In [27]:
predicted[np.asarray([[0,0,0],[1,1,1]]), predicted.argsort()]
Out[27]:
In [31]:
np.choose(predicted.argsort(), predicted)
In [32]:
predicted.argsort().shape
Out[32]:
In [39]:
np.where(predicted.argsort())
Out[39]:
In [40]:
predicted.argsort()
Out[40]:
In [45]:
predicted[np.ogrid[0:predicted.shape[0], ], predicted.argsort()]
In [91]:
predicted
Out[91]:
In [100]:
import copy
axis = -1
i = list(np.ogrid[[slice(x) for x in predicted.shape]])
reverse = [slice(None)] * predicted.ndim
reverse[axis] = slice(None, None, -1)
i[axis] = predicted.argsort(axis=axis)
predicted[i][reverse]
Out[100]:
In [48]:
i[axis] = predicted.argsort(axis=axis)
In [49]:
predicted[i]
Out[49]:
In [92]:
predicted.argsort().shape
Out[92]:
In [93]:
predicted.argsort()
Out[93]:
In [94]:
predicted.argsort()[::-1]
Out[94]:
In [95]:
Out[95]:
In [182]:
predicted
Out[182]:
In [183]:
predicted.shape
Out[183]:
In [189]:
predicted[slice(None), predicted.argsort()].shape
Out[189]:
In [ ]: