Today I tried to build a confusion matrix from my classification model.
After searching on some pages, I found that matshow
from pyplot
might help me.
import matplotlib.pyplot as plt from sklearn.metrics import confusion_matrix def plot_confusion_matrix(cm, title='Confusion matrix', cmap=plt.cm.Blues, labels=None): fig = plt.figure() ax = fig.add_subplot(111) cax = ax.matshow(cm) plt.title(title) fig.colorbar(cax) if labels: ax.set_xticklabels([''] + labels) ax.set_yticklabels([''] + labels) plt.xlabel('Predicted') plt.ylabel('True') plt.show()
Works well if I have multiple shortcuts
y_true = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'a', 'c', 'd', 'b', 'a', 'b', 'a'] y_pred = ['a', 'b', 'c', 'd', 'a', 'b', 'b', 'a', 'c', 'a', 'a', 'a', 'a', 'a'] labels = list(set(y_true)) cm = confusion_matrix(y_true, y_pred) plot_confusion_matrix(cm, labels=labels)
But if I have many tags, some tags do not display correctly
y_true = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'] y_pred = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'] labels = list(set(y_true)) cm = confusion_matrix(y_true, y_pred) plot_confusion_matrix(cm, labels=labels)
My question is, how can I display ALL shortcuts in matshow graphics? I tried something like fontdict
but it still doesn't work