Home > front end >  How to remove Unspecified class from a Confusion Matrix
How to remove Unspecified class from a Confusion Matrix

Time:07-16

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

Please let me know if something is still not specified.

CodePudding user response:

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()
  • Related