TransWikia.com

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

Data Science Asked on August 15, 2021

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.

One Answer

I tried to fix it like this, not sure if it is correct.

from sklearn.utils.multiclass import unique_labels
target_names_filtered = [twenty_test.target_names[i] for i in unique_labels(twenty_test.target[:n_samples], predicted)]

then i used target_names_filtered as the target_names param.

Answered by luky on August 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP