0

I'm not sure what's wrong, I tried to create a confusion matrix using matplotlib but it doesn't look right. The box isn't of the same size & the value is out of the matrix. The background is transparent but I don't mind it.

Here's the plot

Here's the code:

# Get confusion matrix
import itertools
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    plt.figure(figsize=(5, 5))
    
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.show()
    
# Predict test data
y_pred=model.predict(X_test)

# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
np.set_printoptions(precision=2)

# Plot non-normalized confusion matrix
plot_confusion_matrix(cnf_matrix, 
                      classes=['benign', 'malignant', 'normal'],
                      normalize=False,
                      title='Confusion matrix, with normalization')

Anyone knows what's wrong and how to fix it?

arahpanah
  • 349
  • 1
  • 6
  • 14
  • This is more a suggestion, than an answer, but you should give a look to [sklearn.metrics.plot_confusion_matrix](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.plot_confusion_matrix.html#sklearn.metrics.plot_confusion_matrix) – Lescurel Oct 30 '20 at 10:00
  • 1
    Similar: [How can I plot a confusion matrix?](https://stackoverflow.com/questions/35572000/how-can-i-plot-a-confusion-matrix) and [How to add correct labels for Seaborn Confusion Matrix](https://stackoverflow.com/questions/61526287/how-to-add-correct-labels-for-seaborn-confusion-matrix). Anyhow, using Seaborn (version 0.11) would save some trouble. – JohanC Oct 30 '20 at 10:06
  • @Lescurel I've actually looked up the documentation but I'm still not sure what's wrong. – arahpanah Oct 30 '20 at 10:06
  • Does upgrading matplotlib to the latest version help? – JohanC Oct 30 '20 at 10:09
  • @JohanC Just tried it. It didn't. Nothing changes. – arahpanah Oct 30 '20 at 10:18
  • Your code works fine for me with sample data. If you want help you will need to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that includes a toy dataset (refer to [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) – Diziet Asahi Oct 30 '20 at 15:12

0 Answers0