Next, we need to evaluate the performance of the classifier on the test dataset. The following code snippet shows how to do that:
# evaluate the classifier
t_accuracy = sum(test_predictions == test_labels) / float(len(test_labels))
t_accuracy
# 0.96909999999999996
import pandas as pd
import seaborn as sn
from sklearn import metrics
cm = metrics.confusion_matrix(test_labels,test_predictions)
df_cm = pd.DataFrame(cm, range(10), range(10))
sn.set(font_scale=1.2)#for label size
sn.heatmap(df_cm, annot=True,annot_kws={"size": 16}, fmt="g")
The following screenshot shows the confusion matrix for the classification; we can see there are a few misclassified test images and the overall accuracy of the training dataset is 96.9%:
The following screenshot shows:
- A success case: 1-NN prediction label = True label = 0
- A failure case: 1-NN prediction label = 2, True label = 3
The code to find a success and a failure case for prediction is left as an exercise for the reader.