Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove Unspecified class from a Confusion Matrix

I would like to remove the Unspecified class from the Confusion Matrix.

Please find the code as follows:

# Print the confusion matrix using Matplotlib
# 0 - unspecified
# 1 - Buildings
# 2 - Vegetation
# 3 - Roads
# 4 - Water
# 5 - Cars

labels = ['Unspecified', 'Buildings','Vegetation','Roads','Water','Cars']
fig, ax = plt.subplots(figsize=(10, 10))
ax.matshow(conf_matrix, cmap=plt.cm.Oranges, alpha=0.3)
for i in range(conf_matrix.shape[0]):
    for j in range(conf_matrix.shape[1]):
        ax.text(x=j, y=i,s=conf_matrix[i,j], va='center', ha='center', size='xx-large')
        ax.set_xticklabels([''] + labels)
        ax.set_yticklabels([''] + labels)
 
plt.xlabel('Predictions', fontsize=18)
plt.ylabel('Actuals', fontsize=18)
plt.title('Confusion Matrix', fontsize=18)
plt.show()

The output I get after applying confusion matrix looks like this:
Confusion Matrix

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please let me know if something is still not specified.

>Solution :

Pretty simple, just ignore the first row and column:

# Removed the label 'Unspecified'
labels = ['Buildings','Vegetation','Roads','Water','Cars']
fig, ax = plt.subplots(figsize=(10, 10))

# Use slice operator to not use first row and column
ax.matshow(conf_matrix[1:, 1:], cmap=plt.cm.Oranges, alpha=0.3)

# Use ranges starting from 1
for i in range(1, conf_matrix.shape[0]):
    for j in range(1, conf_matrix.shape[1]):
        ax.text(x=j-1, y=i-1,s=conf_matrix[i,j], va='center', ha='center', size='xx-large')
        ax.set_xticklabels([''] + labels)
        ax.set_yticklabels([''] + labels)
 
plt.xlabel('Predictions', fontsize=18)
plt.ylabel('Actuals', fontsize=18)
plt.title('Confusion Matrix', fontsize=18)
plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading