Confusion Matrix

SeemzQ
1 min readApr 29, 2021

A Confusion matrix is useful to test the performance of your classification model in machine learning. It will tell you the true positive, false positive, true negative, and true positive.

It is used to measure the performance of a classification model. The give a a good idea of a model’s performance as compared to classification accuracy. With a confusion matrix, you can also calculate the: precision, recall, accuracy, and specificity.

For my project on cardiovascular disease, I ran several machine learn models and made sure to do a confusion matrix for each of them to get an idea of validity.

Here is how I created a confusion matrix for my decision tree classifier:

cm = confusion_matrix(y_test,y_pred)
#---------Confusion Matrix----------#
group_names = ['True Neg','False Pos','False Neg','True Pos']
group_counts = ["{0:0.0f}".format(value) for value in
cm.flatten()]
group_percentages = ["{0:.2%}".format(value) for value in
cm.flatten()/np.sum(cm)]
labels = [f"{v1}\n{v2}\n{v3}" for v1, v2, v3 in
zip(group_names,group_counts,group_percentages)]
labels = np.asarray(labels).reshape(2,2)
plt.figure(figsize = (6,5))
sns.heatmap(cm, annot=labels, fmt='', cmap='Blues')
fig.tight_layout()



print('Accuracy', accuracy_score(y_test,y_pred))
print('AUC_ROC',roc_auc_score(y_pred,y_test))
print('Recall', recall_score(y_pred,y_test))
print('Precision',precision_score(y_pred,y_test))
Accuracy 0.7202325888727369
AUC_ROC 0.7311748216924057
Recall 0.7765197700748998
Precision 0.6017006343636119

One can see that the breakdown and get an idea of model performance. A confusion matrix is definitely worth doing for your machine learning projects.

--

--