Empty rows in confusion matrix of a neural network

74 Views Asked by At

Could you please say what's wrong with my confusion matrix? Two rows are empty, however, when I print out the confusion matrix (without plotting) the rows are not empty. That is the code that I use:

num_classes = 2
confusion_mat = confusion_matrix(y_test, predicted_labels)

# Calculate total counts for each label
label_counts = np.sum(confusion_mat, axis=1)

# Create a new confusion matrix with an additional row and column for label counts
confusion_mat_with_counts = np.zeros((num_classes + 1, num_classes + 1), dtype=np.int64)
confusion_mat_with_counts[:num_classes, :num_classes] = confusion_mat
confusion_mat_with_counts[:num_classes, num_classes] = label_counts
confusion_mat_with_counts[num_classes, :num_classes] = np.sum(confusion_mat, axis=0)
confusion_mat_with_counts[num_classes, num_classes] = np.sum(confusion_mat)

plt.figure(figsize=(8, 6))
sns.heatmap(confusion_mat_with_counts, annot=True, cmap="YlGnBu", fmt="d", xticklabels=list(label_encoder.classes_) + ['Total'], yticklabels=list(label_encoder.classes_) + ['Total'], cbar=False)
plt.title("Confusion Matrix")
plt.xlabel("True Labels")
plt.ylabel("Predicted Labels")
plt.show()

I tried to simplify the code like this:

num_classes = 2
confusion_mat = confusion_matrix(y_test, predicted_labels)

sns.heatmap(confusion_mat, annot=True, cmap='Blues', fmt='g')

plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.savefig('confusion_matrix.png')
plt.show()

The printed confusion matrix is:

[[ 28  23  51]
 [ 19 104 123]
 [ 47 127 174]]

But the problem is not resolved. The image shows the issue with the confusion matrix plot. enter image description here

0

There are 0 best solutions below