0

here is the snippet of my code :

from mlxtend.plotting import plot_confusion_matrix
from sklearn.metrics import confusion_matrix

y_pred = (model.predict(X_test) > 0.5).astype("int32")

mat = confusion_matrix(y_test, y_pred)
plot_confusion_matrix(conf_mat=mat, class_names=label.classes_, show_normed=True, figsize=(7,7))

I am getting this error on the 4th line "Classification metrics can't handle a mix of multiclass and multilabel-indicator targets" so the confusion matrix is not shown , so anyone could tell me what's wrong on it ? Thanks in advance ^^

Emna
  • 9
  • 2
  • 1
    What's in `y_test`? – tobiv Sep 25 '22 at 11:16
  • What kind of model is `model`? What does `model.predict(X_test)` look like (and subsequently what does `y_pred` look like), and, as tobiv already asks, what does `y_test` look like? // Always try to provide a minimal reproducible example; here, a small dataset that gives the same issue and the code that produced the model. – Ben Reiniger Sep 25 '22 at 15:00

1 Answers1

0

I think you need to add one more step to your calculations. Right now you're doing this:

y_pred = (model.predict(X_test) > 0.5).astype("int32") But usually, for binary classification, model.predict(X_test) produces two values - for class 0 and class 1. You need to take the values for class 1:

y_pred = (model.predict(X_test)[:, 1] > 0.5).astype("int32")

After this the confusion matrix should work without errors.

Andrey Lukyanenko
  • 3,679
  • 2
  • 18
  • 21