question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConfusionMatrix classes: one label must be specified in y true.

See original GitHub issue

When trying to passes class names to the ConfusionMatrix, I’m getting the following error:

/usr/local/lib/python3.6/site-packages/sklearn/metrics/classification.py:248: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if np.all([l not in y_true for l in labels]):
Traceback (most recent call last):
  File "rc.py", line 63, in <module>
    viz.score(X_test, y_test)
  File "/Users/benjamin/Repos/ddl/yellowbrick/yellowbrick/classifier/confusion_matrix.py", line 107, in score
    y, y_pred, labels=self.classes_, sample_weight=sample_weight
  File "/usr/local/lib/python3.6/site-packages/sklearn/metrics/classification.py", line 249, in confusion_matrix
    raise ValueError("At least one label specified must be in y_true")
ValueError: At least one label specified must be in y_true

This is do to the fact that the y_true vector is label encoded (e.g. ones and zeros) but that the labels are strings, e.g. “occupied” and “unoccupied”.

Here is the code to produce the error:

df = pd.read_csv('examples/data/occupancy/occupancy.csv')

features = ["temperature", "relative humidity", "light", "C02", "humidity"]
target = "occupancy"

X = df[features]
y = df[target]
classes = ["unoccupied", "occupied"]

X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2)

viz = ConfusionMatrix(MultinomialNB(), classes=classes)
viz.fit(X_train, y_train)
viz.score(X_test, y_test)
viz.poof()

Note that:

viz = ConfusionMatrix(MultinomialNB())

Does not raise an exception.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
cjmoralecommented, May 25, 2017

@bbengfort Is anyone working on this bug yet? I was able to reproduce the error last night on my local environment and wanted to offer my help.

0reactions
bbengfortcommented, Sep 15, 2018

@rebeccabilbro yep, sorry about this, it should have been closed on June 8 with #253. For future reference, the fix to this is as follows:

viz = ConfusionMatrix(
    MultinomialNB(), classes=classes, label_encoder={0: "unoccupied", 1: "occupied"}
)

Where the label_encoder argument can be a sklearn.preprocessing.LabelEncoder (or really anything with inverse_transform that does the mapping) or a dict.

closed via ff10ca0

Read more comments on GitHub >

github_iconTop Results From Across the Web

At least one label specified must be in y_true, target vector is ...
I checked this question which was like mine and I changed the y from categorical to numerical but the error is still there!...
Read more >
How to pass class names to ConfusionMatrix
This is a follow up to issue #244 and PR #253, to document how to pass class names ... is None: ValueError: At...
Read more >
sklearn.metrics.ConfusionMatrixDisplay
class sklearn.metrics. ... If None, display labels are set from 0 to n_classes - 1 . ... Plot the confusion matrix given the...
Read more >
Understanding Confusion Matrix | by Sarang Narkhede
Well, it is a performance measurement for machine learning classification problem where output can be two or more classes. It is a table...
Read more >
Confusion Matrix — Yellowbrick v0.5 文档
The ConfusionMatrix visualizer is a ScoreVisualizer that takes a fitted ... If the X and y datasets have been encoded prior to training...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found