# How to interpret the confusion matrix?

I'm fairly new to python and I'm having trouble wrapping my head around interpreting the results of a confusion matrix and finding misclassified results.

How can I interpret the result from:

array([[50,  0,  0],
[  0, 48,  2],
[  0, 14, 36]])


Does this mean the first line is 100% true? How can I determine whether these predicted values are good or not, and which results are misclassified?

Seems like you are working with iris, so let's try this example below, using multinomial bayes:

from sklearn.datasets import load_iris
from sklearn.naive_bayes import MultinomialNB
from sklearn import metrics
import pandas as pd

df = pd.DataFrame(data.data, columns=['sepal.length','sepal.width','petal.length','petal.width'])
labels = data.target

clf = MultinomialNB()
clf.fit(df,labels)


You can put your results in a DataFrame to see it better:

results = pd.DataFrame({'predicted':clf.predict(df),
'actual':labels})


And we crosstab the results, asking for each class predicted, what are the actual labels:

pd.crosstab(results.actual,results.predicted)
actual   0  1   2
predicted
0     50 0   0
1     0  46  4
2     0  3   47


It's the same as a confusion matrix:

metrics.confusion_matrix(results.actual,results.predicted)

array([[50,  0,  0],
[ 0, 46,  4],
[ 0,  3, 47]])


To answer your question, the 50 value in the diagonal tells you out 50 predicted '0' labels, all of them are actually '0'. So it's accuracy is 100%, for this class. You can go onto to the other columns and reason from there. In my example above, the training results are pretty ok. I trained on the whole dataset, but in practice you should train / fit using something like cross-validation and see how it performs on test data.

In your case, it seems like the predictions for the last class is not so accurate, with some mislabelled as the 2nd class. You can also check this discussion also. So you might want to think about what is causing 1,2 to be mislabelled and try to improve the model. One handy way of looking at the results is:

print(metrics.classification_report(results.actual,results.predicted))

precision    recall  f1-score   support

0       1.00      1.00      1.00        50
1       0.94      0.92      0.93        50
2       0.92      0.94      0.93        50

accuracy                           0.95       150
macro avg       0.95      0.95      0.95       150
weighted avg       0.95      0.95      0.95       150


This tells you the accuracy by class.