I am trying to solve a multi-class machine learning problem with KNearest Neighbors, and am plotting a confusion matrix for the predictions of all my 10 classes in my data using Matplotlib.pyplot's imshow. Some classes have considerably more occurrences in the data than others, up to 3000 where others may only have 50, so I normalized it to just show the percentage. There is a colorbar next to the graph, that if not normalized, will range from 1 to 3000, which makes sense. However after normalizing it, the range stays all the way up to 3000. I am using a plotting function supplied by Scikit Learn themselves at their site here. Is there something obvious I'm missing, or is there an extra step to reduce the colorbar value range ?
Code
virdis = plt.cm.viridis
blues = plt.cm.Blues
autumn = plt.cm.autumn
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
bounds=[0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1]
plt.colorbar(boundaries=bounds)
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
cm = np.around(cm, decimals=3)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if i == 9 and j == 9 else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
knn = KNeighborsClassifier()
knn.fit(X_train, y_train)
knn_score = knn.score(X_test, y_test)
knn_fold_score = model_selection.cross_val_score(knn, X_test, y_test, cv=10).mean()
predictions = knn.predict(X_test)
c_matrix = confusion_matrix(y_test, predictions)
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(c_matrix, classes=country_names, normalize=True,
title='Normalized confusion matrix')
plt.show()
