← AVIATION GLOSSARY
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
Aviation glossary

What is a Confusion Matrix?

Machine Learning · Model Evaluation 8 languages
Definition
A confusion matrix is a table that counts how many items of each true class a classification model assigned to each predicted class.

A confusion matrix is a table that counts how many items of each true class a classification model assigned to each predicted class. It is also called an error matrix.

How to read it

In scikit-learn, each row is a true class and each column is a predicted class. The entry in row i and column j counts the observations of true class i that the model put in class j. Other texts swap the axes, so check the labels first.

The cells on the diagonal are correct predictions. The cells off the diagonal are errors, and each one shows which class the model confused with which.

The binary case

A model with two classes, positive and negative, gives four cells.

Predicted positivePredicted negative
Actual positivetrue positive (TP)false negative (FN)
Actual negativefalse positive (FP)true negative (TN)

A false negative is an actual positive that the model called negative. A false positive is an actual negative that the model called positive.

Metrics from the cells

  • Accuracy = (TP + TN) / all observations.
  • Precision = TP / (TP + FP).
  • Recall = TP / (TP + FN).
  • F1 = 2TP / (2TP + FP + FN).

Example from the scikit-learn documentation: 8 observations give TN = 2, FP = 1, FN = 2 and TP = 3. Accuracy is 5/8 = 0.625, precision is 3/4 = 0.75, recall is 3/5 = 0.6 and F1 is 6/9 = 0.67.

For more than two classes, compute precision and recall for each class. Balanced accuracy is the average of the recall of all classes. Cohen’s kappa and the Matthews correlation coefficient (MCC) also use the whole matrix. Kappa is between -1 and 1. The MCC has a maximum of +1, and with more than two classes its minimum is between -1 and 0.

Why not accuracy alone

On imbalanced data, accuracy can hide a model that finds nothing. The matrix shows the actual positives and where they went, so it exposes this at once.

Frequently Asked Questions

Are the rows the true class or the predicted class?
It depends on the source. In scikit-learn, each row is the true class and each column is the predicted class. Other texts swap the axes. Read the axis labels before you read the numbers.
What is the difference between precision and recall?
Precision is the share of positive predictions that are correct: TP / (TP + FP). Recall is the share of actual positives that the model finds: TP / (TP + FN). Recall is also called sensitivity.
Why is accuracy not enough to judge a model?
Accuracy can be high for a model that is useless. If 99 of 100 items are negative, a model that always predicts negative scores 99 percent. The matrix shows the problem, because every actual positive is a false negative.
What does a normalized confusion matrix show?
It shows proportions instead of counts. In scikit-learn, normalize='true' divides each row by its total, so the diagonal cell is the recall of that class. normalize='pred' divides each column, and normalize='all' divides by the number of observations.

References

sklearn-confusion-matrix-api
sklearn.metrics.confusion_matrix scikit-learn
google-ml-crash-course-metrics
Machine Learning Crash Course: Classification, accuracy, precision and recall Google for Developers
wikipedia-confusion-matrix
Confusion matrix Wikipedia

This is a reference entry. TarmacView provides drone inspection services and doesn't sell the equipment or materials described in this glossary.

Get Started Need your airfield lighting verified? TarmacView inspects PAPI, runway and approach lighting by drone, between aircraft movements, and delivers a compliance report built for your regulator. Pavement and obstacle inspection are in development.
Keep reading

Learn more

03 PAGES
Precision, Recall, and F1 Score
glossary

Precision, Recall, and F1 Score

Precision, recall, and F1 score are classification metrics used to evaluate the performance of AI and machine learning models in pavement and infrastructure defect detection. Precision measures the fraction of detected defects that are correct, recall measures the fraction of actual defects that are found, F1 is their harmonic mean.

Intersection Over Union (IoU)
glossary

Intersection Over Union (IoU)

Intersection over union (IoU), or the Jaccard index, scores the overlap of two regions. The formula, the link to the Dice coefficient and the VOC and COCO thresholds.

k-Nearest Neighbors (kNN)
glossary

k-Nearest Neighbors (kNN)

The k-nearest neighbors (kNN) algorithm assigns a class by majority vote among the k closest stored points. How it works, how to choose k, and where it fails.