I have trained my model (multiclass classification) of CNN using keras and now I want to evaluate the model on my test set of images. Is there a way to create confusion matrix?
Asked
Active
Viewed 1.6k times
1
-
1Please notice that you do (and should) not need to post a [wall of code](https://idownvotedbecau.se/toomuchcode/) in order to ask how to calculate a confusion matrix; posting your predictions and the respective ground truth should be nore than enough - please see how to create a [mre] (emphasis on *minimal*). – desertnaut Jan 19 '22 at 20:21
2 Answers
6
Thanks for answers. I did like this:
#Predict
y_prediction = model.predict(x_test)
y_prediction = np.argmax (y_prediction, axis = 1)
y_test=np.argmax(y_test, axis=1)
#Create confusion matrix and normalizes it over predicted (columns)
result = confusion_matrix(y_test, y_prediction , normalize='pred')
print(result)

Itra
- 137
- 1
- 1
- 9
3
You can use sklearn for that.
from sklearn.metrics import confusion_matrix
#Predict
y_prediction = model.predict(x_test)
#Create confusion matrix and normalizes it over predicted (columns)
result = confusion_matrix(y_test, y_prediction , normalize='pred')
If you want to plot it as well, you can find several ways here: How can I plot a confusion matrix?

Henrique
- 328
- 1
- 2
- 10
-
Are you sure from OP's description that both `y_test` and `y_prediction` are single-digit predictions, and not one-hot encoded vectors (in which case `confusion_matrix` will simply not work)? – desertnaut Jan 20 '22 at 00:27
-
I got an error - ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets – Itra Jan 20 '22 at 08:41