Reputation: 11
I build a confusion matrix according to the code below:
conf_matrix = confusion_matrix(y_test, y_test_predictions)
print(conf_matrix)
[[122 27]
[ 40 42]]
Observe that the values for it are all armed in an array. I want to plot these values right now with this information using matplotlib and the seaborn library. For this reason, use or follow the code trecho.
plt.figure(figsize=(3, 3), dpi=300)
# Scale up the size of all text
sns.set(font_scale = 1.1)
ax = sns.heatmap(conf_matrix, annot=True, fmt='d', )
# set x-axis label and ticks.
ax.set_xlabel("Predicted Diagnosis", fontsize=14, labelpad=20)
ax.xaxis.set_ticklabels(['Negative', 'Positive'])
# set y-axis label and ticks
ax.set_ylabel("Actual Diagnosis", fontsize=14, labelpad=20)
ax.yaxis.set_ticklabels(['Negative', 'Positive'])
# set plot title
ax.set_title("Confusion Matrix for the Diabetes Detection Model", fontsize=14, pad=20)
plt.show()
To use this code, which should plot the confusing matrix values in each cell, numbers 40 and 42 will not appear (second matrix line). Have you ever passed by someone else?
I'm used the jupyter notebook 7.0.8, python 3.11.7, matplotlib 3.8.0 and seaborn 0.12.2.
Upvotes: 1
Views: 55
Reputation: 1
I ran your code on Google Colab with Python version 3.11.11 and it worked perfectly.
I made dummy y_test
and y_predicted
arrays.
y_test = [0]*122 + [0]*27 + [1]*40 + [1]*42
y_predicted = [0]*122 + [1]*27 + [0]*40 + [1]*42
Then, running your provided code plotted the confusion matrix using matplotlib.
conf_matrix = confusion_matrix(y_test, y_predicted)
print(conf_matrix)
plt.figure(figsize=(3, 3), dpi=300)
# Scale up the size of all text
sns.set(font_scale = 1.1)
ax = sns.heatmap(conf_matrix, annot=True, fmt='d',)
# set x-axis label and ticks.
ax.set_xlabel("Predicted Diagnosis", fontsize=14, labelpad=20)
ax.xaxis.set_ticklabels(['Negative', 'Positive'])
# set y-axis label and ticks
ax.set_ylabel("Actual Diagnosis", fontsize=14, labelpad=20)
ax.yaxis.set_ticklabels(['Negative', 'Positive'])
# set plot title
ax.set_title("Confusion Matrix for the Diabetes Detection Model", fontsize=14, pad=20)
plt.show()
Here's how it looked: Confusion Matrix
Upvotes: 0