How do I add class labels to the confusion matrix? The label display number in the label not the actual value of the label Eg. labels = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

confusion matrix image

Here is the code I used to generate it.

x_train, y_train, x_test, y_test = train_images, train_labels, test_images, test_labels
model = KNeighborsClassifier(n_neighbors=7, metric='euclidean')
model.fit(x_train, y_train)
# predict labels for test data
predictions = model.predict(x_test)

# Print overall accuracy
print("KNN Accuracy = ", metrics.accuracy_score(y_test, predictions))

# Print confusion matrix
cm = confusion_matrix(y_test, predictions)

plt.subplots(figsize=(30, 30))         # Sample figsize in inches
sns.heatmap(cm, annot=True)

1 Answer 1


You can use the xticklabels and yticklabels arguments for this as specified in the seaborn documentation. See also the following answer on a different question on the datascience stackexchange.

  • $\begingroup$ Thanks, I will look at it. $\endgroup$
    – seyinia
    May 21 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.