Giuseppe Minardi
Giuseppe Minardi

Reputation: 411

MultilabelBinarizer error when empty class is present

I'm building a multilabel classifier, when I try to call the classification report I get the following error: ValueError: All labels must be in [0, n labels) for multilabel targets. Got 6 > 2, following there's a minimal example that produces the error

from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.metrics import classification_report

import pandas as pd

a = [
     [3, 4],
     [6],
     [3],
     [],
     [6, 4]
]
bin = MultiLabelBinarizer()
bin.fit(a)

a = bin.transform(a)
report = classification_report(
    a,
    a, 
    output_dict=True, 
    zero_division=0, 
    labels=bin.classes_
)
print(report)

when I try to convert classes to strings I get the following error: TypeError: '>=' not supported between instances of 'str' and 'int'. If I comment out labels=bin.classes_ everything works, but I'd like to have the original classes in the report. How can I solve it? Is it a bug?

Upvotes: 0

Views: 234

Answers (1)

AbbeGijly
AbbeGijly

Reputation: 1211

For the classification report, try using the target_names argument in place of labels:

report = classification_report(
    a,
    a, 
    output_dict=True, 
    zero_division=0, 
    target_names=list(map(str, bin.classes_)),
)
print(report)

This is suboptimal in the sense that the original integer classes are "stringified" in the report.

Upvotes: 2

Related Questions