Visualize and Print Confusion Matrix

In many cases you would like to print the confusion matrix in a better format and look and feel than what is provided by scikit learn by default.

The default look when printing confusion matrix using scikit learn

scikit-learn default confusion matrix print
scikit-learn default confusion matrix print

However in many cases you may like to print the confusion matrix in a format like below

For this you will need to write your custom code as below

pyplt.clf()
pyplt.figure(figsize=(9,9))
pyplt.rcParams.update({'font.size': 12})
CategoryNames = ['Negative','Positive']
pyplt.title('Confusion Matrix - Vizualization')
pyplt.ylabel('True')
pyplt.xlabel('Predicted')
xytics = np.arange(len(CategoryNames))
pyplt.imshow(confusionmatrix, cmap=plt.cm.cividis)
myArray = [['True Negative','False Positive'], ['False Negative', 'True Positive']]
for k in range(2):
    for j in range(2):
        pyplt.text(j,k, str(myArray[k][j])+" = "+str(confusionmatrix[k][j]))

pyplt.show()

Thus you can print the confusion matrix in the way you want.

Cheers !!

Advertisements

Leave a Reply