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

Time:05-04

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 32x32

Is there a way around this problem?

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

CodePudding user response:

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).
  • Related