# import numpy and scikit-learn libraries import numpy as np
from sklearn.metrics import label_ranking_average_precision_score
# take sample datasets y_true = np.array([[ 1 , 0 , 0 ],
[ 1 , 0 , 1 ],
[ 1 , 1 , 0 ]])
y_score = np.array([[ 0.75 , 0.5 , 1 ],
[ 1 , 0.2 , 0.1 ],
[ 0.9 , 0.7 , 0.6 ]])
# print the output print (label_ranking_average_precision_score(
y_true, y_score))
|
0.777To understand above example, Let’s take three categories human (represented by [1, 0, 0]), cat(represented by [0, 1, 0]), dog(represented by [0, 0, 1]). We were provided three samples such as [1, 0, 0], [1, 0, 1], [1, 1, 0] . This means we have total number of 5 ground truth labels (3 of humans, 1 of cat and 1 of dog). In the first sample for example, only true label human got 2nd place in prediction label. so, rank = 2. Next we need to find out how many correct labels along the way. There is only one correct label that is human so the numerator value is 1. Hence the fraction becomes 1/2 = 0.5. Therefore, the LRAP value of 1st sample is:
Article Tags :
Recommended Articles