Confusion matrix multilabel-indicator is not supported in scikit learn

Sometimes you encoded the labels to One Hot Version. Like when the labels are digits and you encoded them such as

1 -> [0,1,0,0,0,0,0,0,0,0]

5->[0,0,0,0,0,1,0,0,0,0]

We usually do this by:

from keras.utils import to_categoricaltrain_labels = to_categorical(train_labels)

Issue:

from sklearn.metrics import confusion_matrix
print(confusion_matrix(test_labels,predictions))

you encounter an error like:

Image for post
Image for post

Solution:

Like:

print(confusion_matrix(test_labels.argmax(axis=1), predictions.argmax(axis=1)))

Why do we do this? Because the confusion matrix takes a vector of labels (not the one-hot encoding).

Here you will see how argmax works:

Image for post
Image for post

Written by

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store