0
$\begingroup$

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.

$\endgroup$

1 Answer 1

0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.