0

I'm working on a classification problem with 20 classes. I'm trying to visualize the results through a confusion matrix using matplotlib.

After computing my confusion matrix, I used the plot_confusion_matrix described here.

def plot_confusion_matrix(y_true, y_pred, classes,
                      normalize=False,
                      title=None,
                      cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if not title:
    if normalize:
        title = 'Normalized confusion matrix'
    else:
        title = 'Confusion matrix, without normalization'

# Compute confusion matrix
cm = confusion_matrix(y_true, y_pred)
# Only use the labels that appear in the data
classes = classes[unique_labels(y_true, y_pred)]
if normalize:
    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    print("Normalized confusion matrix")
else:
    print('Confusion matrix, without normalization')

print(cm)

fig, ax = plt.subplots()
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
ax.figure.colorbar(im, ax=ax)
# We want to show all ticks...
ax.set(xticks=np.arange(cm.shape[1]),
       yticks=np.arange(cm.shape[0]),
       # ... and label them with the respective list entries
       xticklabels=classes, yticklabels=classes,
       title=title,
       ylabel='True label',
       xlabel='Predicted label')

# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
         rotation_mode="anchor")

# Loop over data dimensions and create text annotations.
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
    for j in range(cm.shape[1]):
        ax.text(j, i, format(cm[i, j], fmt),
                ha="center", va="center",
                color="white" if cm[i, j] > thresh else "black")
fig.tight_layout()
return ax

Here is what it looks like : enter image description here It looks like the problem comes from dealing with too many classes, so a natural solution would be scalling up the plot. But doing that distorts it. Also, how do I choose the correct scale/size ?

How do I proceed to make it look better ?

P.S. You can find the confution matrix as a csv file here.

tyassine
  • 142
  • 9
  • Try this one for some tips maybe: https://stackoverflow.com/questions/35572000/how-can-i-plot-a-confusion-matrix – jtweeder Nov 02 '19 at 22:12
  • @jtweeder I've already tried. It looks much better, but as you can see [here](https://imgur.com/zdNqS7j), the plot is truncated at the top and bottom. Also, numbers barely fit in the squares. – tyassine Nov 02 '19 at 22:28

2 Answers2

2

Since you dont specified the estrict use of matplotlib I recomend you to use the seaborn library its so much easy and simple and if you want to change something weird was constructed with matplolib if I aint wrong. Using seaborn is:

import seaborn as sns 

plt.figure(figsize = (10,10))  #This is the size of the image
heatM = sns.heatmap(cov_vals, vmin = -1, vmax = 1,center = 0, cmap = sns.diverging_palette(20, 220, n = 200),  square = True, annot = True) #this are the caracteristics of the heatmap
heatM.set_ylim([10,0]) # This is the limit in y axis (number of features)

and this is the result. be careful with the limits heatM.set_ylim([10,0]) for x too, this need to be the number of variables that you have.

hope this was useful.

enter image description here

  • Thanks for your answer. I had already tried doing that but I faced a problem. The confusion matrix looked like [this](https://imgur.com/zdNqS7j). It was actually a bug in the latest version (3.1.1) of seaborn (see [issue](https://github.com/matplotlib/matplotlib/issues/14675) here). The solution was to use a prior version (3.1.0 in my case). – tyassine Nov 22 '19 at 14:21
0

I ended up using seaborn but I faced a problem. The confusion matrix looked like this. It was actually a bug in the latest version (3.1.1) of seaborn (see this issue). The solution was to use a prior version (3.1.0 in my case).

tyassine
  • 142
  • 9
  • yes its an error to correct it, its necessary to re-define the limits of the plot, but since the error its already corrected doesnt care haha. – Andres Rubiano Nov 23 '19 at 04:25