flags = np.zeros_like(predictions)
for i in range(predictions.shape[0]):
for j in range(predictions.shape[1]):
if predictions[i][j] in np.arange(labels[i][j]):
flags[i][j] = 1.
return np.mean((np.sum(flags, -1) >= 1.).astype(float))
@register_metric("rank_response")
After Change
return recall_at_k(labels, predictions, k=10)
def recall_at_k(y_true, y_pred, k):
num_examples = float(len(y_pred))
predictions = np.array(y_pred)
predictions = np.flip(np.argsort(predictions, -1), -1)[:, :k]
num_correct = 0
for el in predictions: