Top 2 accuracy ValueError: Number of classes in 'y_true' (10) not equal to the number of classes in 'y_score' (11)

I working on classification problem with 11 possible labels (classes)
when I use the sklearn implementation of top k accuracy (in my case I use k=2).
I get this error:

top2=top_k_accuracy_score(target_top_numpy, predicted_top_numpy, k=2)

Number of classes in ‘y_true’ (10) not equal to the number of classes in ‘y_score’ (11).

whne I print what I provide to the function I get this :

target_top_numpy [ 7 4 3 4 5 8 4 4 5 6 9 10 1 0 10 10]

predicted_top_numpy [[ 3.64876091e-01 7.53873736e-02 1.05422363e-03 5.05129546e-02
2.16314554e-01 1.38154477e-01 -1.14530839e-01 5.82735389e-02
4.13481146e-02 1.22349434e-01 -6.59204051e-02]
[ 3.16644907e-01 7.77033158e-03 -7.20558316e-02 -6.00859150e-02
-8.01762193e-03 2.11278737e-01 -3.34433466e-01 -4.90757078e-03
3.12402844e-05 6.02411479e-03 -1.62094861e-01]
[-3.13724801e-02 -2.00152487e-01 -9.78353918e-02 2.41941616e-01
8.20444822e-02 1.60071626e-02 1.05403595e-01 -2.43545681e-01
-2.80880153e-01 2.08564132e-01 5.07901981e-02]
[ 3.96135807e-01 4.14540768e-02 -1.59000635e-01 6.96345977e-03
-2.15933099e-01 3.68947625e-01 4.26303655e-01 1.64577328e-02
6.88274056e-02 -1.91847384e-01 -3.17941099e-01]
[-1.56998903e-01 -4.41738904e-01 9.57103521e-02 -4.80621979e-02
1.93291128e-01 -2.23846883e-01 -1.23909242e-01 -5.06545082e-02
-2.72444129e-01 1.93614930e-01 -1.26726717e-01]
[ 2.48090159e-02 -1.04816929e-02 -2.16993332e-01 -1.50090173e-01
4.47610319e-02 2.08084434e-02 -8.80720988e-02 -1.70478880e-01
-3.12956125e-01 1.90198779e-01 -2.25080639e-01]
[ 2.21970081e-01 1.59545437e-01 1.61625639e-01 7.50204735e-03
-2.77360290e-01 -8.03043768e-02 -3.99282068e-01 -1.08894639e-01
4.45883423e-02 8.48998725e-02 -4.10080850e-02]
[ 3.37239712e-01 1.62343055e-01 -1.66178823e-01 6.95665181e-02
1.96123302e-01 1.21007398e-01 3.64012569e-02 -5.82684577e-02
-5.24711192e-01 -9.84926894e-02 -2.02147603e-01]
[ 1.11381486e-02 -1.58907428e-01 -1.43398225e-01 2.03085989e-02
2.90830433e-01 -1.34285629e-01 3.72584797e-02 1.18210070e-01
-3.31480324e-01 2.01538831e-01 -2.29168892e-01]
[ 2.93235391e-01 -1.56496629e-01 -3.95823091e-01 -3.11217569e-02
3.32119390e-02 2.71259129e-01 4.12342221e-01 -2.44913995e-02
-2.71460533e-01 2.01342821e-01 3.48200984e-02]
[ 1.66218251e-01 -5.69603406e-02 -2.24357933e-01 6.05592877e-02
1.14171773e-01 -1.19616829e-01 9.93291736e-02 -2.46701315e-02
2.16950625e-02 4.18458313e-01 -6.70969412e-02]
[ 3.30241203e-01 -2.54895747e-01 -2.78036386e-01 -5.55236638e-03
-2.17291400e-01 1.58615947e-01 3.16702127e-01 -1.52390391e-01
-4.49812710e-02 -1.33779973e-01 -2.13309616e-01]
[ 1.65853560e-01 1.57377750e-01 -1.74856558e-01 1.60512879e-01
-1.67636931e-01 2.73997486e-02 -1.89344496e-01 1.24902800e-01
-3.72637630e-01 4.76141274e-03 -1.92126274e-01]
[ 1.34329706e-01 1.14485815e-01 -7.76365027e-03 -1.59174949e-02
9.75754857e-03 2.00641692e-01 1.60150379e-01 1.14389196e-01
-3.14345658e-01 -1.59578443e-01 -7.12610111e-02]
[ 2.98025697e-01 1.09786630e-01 2.12374516e-02 1.99311420e-01
8.56085271e-02 4.56976779e-02 3.12629580e-01 1.74697757e-01
-1.23753622e-01 2.98946023e-01 -6.45548478e-02]
[ 2.10407883e-01 -2.40327224e-01 2.30352670e-01 2.63346374e-01
1.04940936e-01 -7.43195340e-02 -2.83480808e-02 3.25459659e-01
-1.09645352e-01 1.50563955e-01 -1.40263081e-01]]

Since your target_top_numpy doesn’t contain all unique labels, you have yo provide the labels argument as described in the docs of sklearn.metrics.top_k_accuracy_score:

top2 = top_k_accuracy_score(target_top_numpy, predicted_top_numpy, k=2, labels=np.arange(11))
1 Like