Issue
I am trying to present the confusion matrix after deep learning model, but there is an error, how can I fix it?
Y_pred = model.predict(X_val)
Y_pred_classes = np.argmax(Y_pred,axis = 1)
Y_true = np.argmax(y_val,axis = 0)
confusion_mtx = confusion_matrix(Y_true, Y_pred_classes)
f,ax = plt.subplots(figsize=(8, 8))
sns.heatmap(confusion_mtx, annot=True, linewidths=0.01,cmap="Greens",linecolor="gray", fmt= '.1f',ax=ax)
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.title("Confusion Matrix")
plt.show()
TypeError: Singleton array 1 cannot be considered a valid collection.
This is the output of Y_true
and Y_pred_classes
Y_true
1
Y_pred_classes
array([0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0])
Solution
You need to remove this code #Y_true = np.argmax(y_val,axis = 0)
as Y_true
already carries actual label values, you need not to find the max probable number. You can directly use Y_true
in confusion_matrix()
.
Please find below replicated and fixed code snippets:
Y_pred = model.predict(X_val)
Y_pred_classes = np.argmax(Y_pred,axis = 1)
#Y_true = np.argmax(y_val,axis = 0)
confusion_mtx = confusion_matrix(Y_true, Y_pred_classes)
f,ax = plt.subplots(figsize=(8, 8))
sns.heatmap(confusion_mtx, annot=True, linewidths=0.01,cmap="Greens",linecolor="gray", fmt= '.1f',ax=ax)
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.title("Confusion Matrix")
plt.show()
Answered By - TFer2
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.