This metric is usually used in multiclass classification problems.
Each multiclass model gives a probability score for all the classes it is being trained on, but often you take the highest one, by using np.argmax but what if you took the top n classes and gave credit to the model if it got right in one of the n predictions.
That is what is top n accuracy, it gives the model more chances to be right.
Lets take an example.
Suppose you built a model that predicts 3 classes and you want to find the top 2 accuracy of your model.
Then you would pass the prediction array to the model and the true values and if the correct prediction is in the top 2 then you give it credit for being right.
import numpy as np
from sklearn.metrics import top_k_accuracy_score
y_true = [0,1,1,2,2]
y_pred = [[0.25, 0.2,0.3], #Here 0 is in the top 2
[0.3, 0.35, 0.5], #Here 1 is in the top 2
[0.2,0.4, 0.45], #Here 1 is in the top 2
[0.5, 0.1, 0.2], #Here 2 is in the top 2
[0.1, 0.4, 0.2]] #Here 2 is in the top 2
top_k_accuracy_score(y_true, y_pred, k=2)
It is 1.0, because the correct class was always in our top 2 prediction, actually, if you notice then it was always the second prediction of our model, so if we take regular accuracy or set the value k = 1 in top_k_accuracy_score(y_true, y_pred, k=2), the answer is 0.
Hopefully, this explains what top N accuracy is, and if you want me to cover any ML topic, write in the comments below. Thanks for reading.
Leave a comment