Skip to content
Advertisement

confusion matrix error “Classification metrics can’t handle a mix of multilabel-indicator and multiclass targets”

I am getting a

JavaScript

error when I try to use confusion matrix.

I am doing my first deep learning project. I am new to it. I am using the mnist dataset provided by keras. I have trained and tested my model successfully.

However, when I try to use the scikit learn confusion matrix I get the error stated above. I have searched for an answer and while there are answers on this error, none of them worked for me. From what I found online it probably has something to do with the loss function (I use the categorical_crossentropy in my code). I tried changing it to sparse_categorical_crossentropy but that just gave me the

JavaScript

when I run the fit() function on the model.

This is the code. (I have left out the imports for the sake of brevity)

JavaScript

How can i fix this?

Advertisement

Answer

Confusion matrix needs both labels & predictions as single-digits, not as one-hot encoded vectors; although you have done this with your predictions using model.predict_classes(), i.e.

JavaScript

your test_labels are still one-hot encoded:

JavaScript

So, you should convert them too to single-digit ones, as follows:

JavaScript

After which, the confusion matrix should come up OK:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement