Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting ValueError when trying to obtain confusion matrix of trained CNN model

I had a classification problem in which I trained a CNN and now I was hoping I could obtain its confusion matrix. I tried the following:

from sklearn.metrics import confusion_matrix

y_pred = model.predict(x_test)
#Generate the confusion matrix
cf_matrix = confusion_matrix(y_test, y_pred)

print(cf_matrix)

But I got the following error:

ValueError: Classification metrics can't handle a mix of unknown and continuous-multioutput targets

x_test is made of (84, 32, 32) – 84 monochrome images of shape 32×32

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way around this problem?

Addendum: Model Summary (note: the output activation fn is softmax)
enter image description here

>Solution :

Just to sum it up from the comments, there were two problems:

  1. confusion_matrix expects the class labels and not the logits output from the Dense layer with softmax activation. This is fixed simply by doing:

y_pred = np.argmax(y_pred, axis=1)

  1. y_true is identified as having targets of unknown type (see the error). Hence, make sure it has the correct type of data (i.e., the true class labels).
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading