TransWikia.com

Plotting scikit-learn confusion matrix returns no values in the last class

Data Science Asked on October 20, 2020

I am attempting to create a confusion matrix using Scikit-Learn for a multiclass classification CNN, and it works well except for the fact that it does not provide values for the last class (i.e. all of them are zero), as illustrated in the image below:

enter image description here

Here is the function that I used to generate the confusion matrix and plot:

# Function to Calculate Ensemble Confusion Matrix for Given Dataset
    def getConfusionMatrix(self, y_true, y_pred, speaker_labels, title):
        
        # Calculate Confusion Matrix
        cm = confusion_matrix(y_true, y_pred)
        
        print(cm)
        
        # Instantiate Plot Variables
        cmap = plt.cm.Blues # Color map for confusion matrix
        title = title # Plot title
        ticks = np.arange(len(speaker_labels)+1)
        fmt = 'd' # Data format
        thresh = cm.max()/2. # Treshold
        
        # Plot Confusion Matrix
        plt.figure(figsize=(15, 10))
        plt.imshow(cm, interpolation='nearest', cmap=cmap)
        plt.title(title)
        plt.colorbar()
        plt.xticks(ticks, speaker_labels, rotation=45)
        plt.yticks(ticks, speaker_labels)
        
        for (i, j) in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            plt.text(j, i, format(cm[i, j], fmt),
                     color='white' if cm[i, j] > thresh else 'black')
        
        plt.ylabel('True Label')
        plt.xlabel('Predicted Label')

How can I ensure that the confusion matrix calculates TP/FP/TN/FN for the final class (i.e. ARA NORM in the image)? Is this possibly an issue with input into the confusion_matrix() function?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP