# SKLEARN Metrics report "Number of classes, 28, does not match size of target_names, 35. Try specifying the labels parameter"

What's the proper way to define the labels or target names of classification_report?

I have the report like this:

print(metrics.classification_report(twenty_test.target[:n_samples], predicted, target_names=twenty_test.target_names))


I noticed i get the error like in the title or similar once the twenty_test.target[:n_samples] (or possibly also predicted array) is different length than twenty_test.target_names...

It doesn't work just out of box if i simply provide all target names (also those not included in the "target" or "predicted" array. (If i provide it as "target_names" param).

But if I provided it as the "labels" param as hinted i simply see 0 filled report.

Btw this is the output of unique values of "target" array:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 25}


This is the output of predicted array (unique values):

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 25, 27, 30, 31, 32}


I tried to get the longer list of these and build the list of the longest value like this:

longer = max(max(set(predicted)), max(set(twenty_test.target[:n_samples])))+1
print(metrics.classification_report(twenty_test.target[:n_samples], predicted, target_names=twenty_test.target_names[:longer])


Doesn't work. I would expect all target names also those not include should work out of box.

from sklearn.utils.multiclass import unique_labels