Emna
Emna

Reputation: 9

Classification metrics can't handle a mix of multiclass and multilabel-indicator targets

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 ^^

Upvotes: 0

Views: 2745

Answers (1)

Andrey Lukyanenko
Andrey Lukyanenko

Reputation: 3851

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.

Upvotes: 0

Related Questions