Loading ModelMetric Lab
Classification metrics · 8 min read

Confusion matrix explained: TP, FP, FN, TN and examples

A confusion matrix is a compact table that counts the four ways a binary classifier can be right or wrong. Once you can read those four cells, accuracy, precision, recall, specificity, and F1 become straightforward calculations—and you can see which errors your model is actually making.

What is a confusion matrix?

A confusion matrix is a table comparing a model’s predicted class with the actual class for every evaluated observation. In binary classification there are two classes, usually called positive and negative, so the table has four counts: true positives (TP), false positives (FP), false negatives (FN), and true negatives (TN).

“True” means the prediction agrees with the actual label. “False” means it does not. “Positive” and “negative” describe the class, not whether the prediction was good or bad. The matrix is useful because a single accuracy number can hide whether a model is raising too many false alarms or missing the cases you care about.

How do you read the four cells?

There are two common table orientations. Some libraries put actual labels on rows and predictions on columns; others do the reverse. The safe practice is to label both axes explicitly. The meanings below do not change with the layout:

CellPredictionActual labelPlain-English meaning
TPPositivePositiveThe model found a real positive case.
FPPositiveNegativeThe model raised a false alarm.
FNNegativePositiveThe model missed a real positive case.
TNNegativeNegativeThe model correctly rejected a negative case.

The positive class is a decision, not a universal fact. In a fraud detector, “positive” might mean suspected fraud. In a medical screening example, it might mean a condition is present. In a support classifier, it might mean the ticket needs escalation. State the positive class before interpreting precision or recall.

To build a matrix from predictions, align the actual and predicted labels by observation, compare each pair, and increment exactly one cell. The confusion matrix calculator accepts labels such as positive/negative, yes/no, true/false, or 1/0 and performs that counting step for you.

What does a confusion matrix look like in a worked example?

Suppose a binary classifier is evaluated on 200 cases. It produces 80 true positives, 20 false positives, 10 false negatives, and 90 true negatives:

Actual positiveActual negativeRow total
Predicted positiveTP = 80FP = 20100
Predicted negativeFN = 10TN = 90100
Column total90110200

The row totals tell you what the model predicted: it predicted positive 100 times and negative 100 times. The column totals tell you what actually happened: there were 90 positive cases and 110 negative cases. Those totals are already useful before calculating a metric because they reveal the class prevalence and the model’s prediction mix.

Each case contributes to one cell only. The four counts sum to the evaluation-set size:

N = TP + FP + FN + TN = 80 + 20 + 10 + 90 = 200

Which metrics can you calculate from a confusion matrix?

Accuracy: how often was the model correct?

accuracy = (TP + TN) / (TP + FP + FN + TN) = (80 + 90) / 200 = 0.85 = 85%

Accuracy counts both types of correct prediction. In the example, 170 of 200 cases were correct. Accuracy is a reasonable summary when the classes are fairly balanced and the costs of the two error types are similar. It becomes misleading when one class dominates.

Precision: when the model says positive, is it right?

precision = TP / (TP + FP) = 80 / (80 + 20) = 0.80 = 80%

Precision focuses on the model’s positive predictions. Here, 80 of the 100 positive predictions were correct. Low precision means the positive queue contains many false alarms. Use it when acting on a positive prediction is costly or disruptive.

Recall: did the model find the positives?

recall = TP / (TP + FN) = 80 / (80 + 10) = 0.8889 ≈ 88.89%

Recall, also called sensitivity or true positive rate, focuses on the actual positive cases. The model found 80 of the 90 positives and missed 10. Use it when a missed positive is especially costly.

Specificity: did the model reject the negatives?

specificity = TN / (TN + FP) = 90 / (90 + 20) = 0.8182 ≈ 81.82%

Specificity, also called the true negative rate, is the negative-class counterpart to recall. It says how many actual negatives were correctly identified. Its complement, 1 − specificity, is the false positive rate.

F1: are precision and recall balanced?

F1 = 2 × precision × recall / (precision + recall) = 2 × 0.80 × 0.8889 / (0.80 + 0.8889) ≈ 0.8421 = 84.21%

F1 combines precision and recall with a harmonic mean. It is low when either component is low, which makes it useful when you want one score that does not let a strong recall completely hide weak precision. The precision, recall, F1, and accuracy guide goes deeper into that trade-off and class imbalance.

You can also calculate balanced accuracy, the average of recall and specificity: (88.89% + 81.82%) / 2 ≈ 85.35%. It is useful when you want each class to contribute equally, but it still does not replace inspecting the four cells.

How does a classification threshold change the matrix?

Many classifiers output a score or probability rather than a final class. A threshold converts that score into positive or negative. Lowering the threshold usually labels more cases positive. That can increase recall because fewer positives are missed, but it can also increase false positives and reduce precision. Raising the threshold usually does the reverse.

There is no universally correct threshold. Choose it from the cost of FP versus FN, operational capacity, and the quality target of the application. If a review team can handle only 100 alerts per day, a threshold that generates 500 high-recall alerts may not be useful even if its recall looks impressive. Evaluate several thresholds on a validation set, then lock the policy before testing once on the final holdout set.

Threshold movement changes the confusion matrix, so it changes every derived metric. A model should not be called “better” because it has higher accuracy at one arbitrary threshold. Compare models at a thresholding policy that reflects the same business constraint, or compare a curve such as precision-recall when the threshold is still undecided.

How can confusion-matrix analysis go wrong?

Accuracy paradox. Imagine 990 negative cases and 10 positive cases. A model that predicts negative every time gets 990 correct, so accuracy is 99%, but recall is 0% because it finds none of the positives. This is why prevalence and the matrix matter.

Unclear positive class. Precision for “fraud” is not the same question as precision for “not fraud.” If you reverse the class without saying so, you can report a technically correct number that answers the wrong question.

Reading a normalized matrix as counts. A row-normalized or column-normalized heatmap contains proportions, not raw observations. Check the legend and retain the integer counts when you need support, prevalence, or auditability.

Dropping the denominator. Precision with one predicted positive and one true positive is 100%, but it is based on one positive prediction. Always report support: the number of actual positives, actual negatives, and predicted positives.

Evaluating after tuning on the same data. If you choose the threshold and tune the model repeatedly on the test set, the matrix becomes optimistic. Use training and validation data for decisions, and reserve the test set for the final estimate.

Ignoring subgroup differences. Aggregate counts can hide a high false negative rate for a particular language, region, device type, or user group. Calculate the matrix for important subgroups when the data and privacy rules allow it, and compare both rates and sample sizes.

What are the important edge cases?

If there are no predicted positives, the usual precision denominator is zero. If there are no actual positives, the usual recall denominator is zero. Libraries differ in whether they return zero, a warning, or an undefined value, so document the convention used by your reporting code. The calculator displays zero for a zero denominator and explains the assumption in its guide.

For multiclass classification, the matrix becomes a square table with one row and column per class. Each diagonal cell is a correct prediction; off-diagonal cells show which classes are confused. Metrics then require an averaging choice—macro, weighted, or micro—and those averages answer different questions. Do not silently treat a multiclass matrix as binary.

For a production report, include the dataset split, positive class, threshold, date, class prevalence, total support, raw matrix, and the metrics that match the decision. A single rounded percentage is not enough to reproduce or audit the result.

Confusion-matrix checklist: label the positive class; verify actual and predicted rows are paired; show raw TP, FP, FN, and TN counts; report prevalence and support; choose precision or recall from the cost of errors; inspect the threshold; and calculate subgroup matrices when fairness or reliability depends on them.

Use the calculator and compare evaluation methods

Try the confusion matrix calculator with the 10-label preset or your own prediction logs. If you already have the four counts, use the accuracy, precision, recall, and F1 calculator directly.

For the classification metric trade-offs, continue to accuracy vs precision vs recall vs F1. For the regression counterpart, read MAE vs MSE vs RMSE vs R²; it shows how error metrics work when the prediction is a number rather than a class.

FAQ

What is the difference between a confusion matrix and accuracy?

A confusion matrix contains the four underlying counts: TP, FP, FN, and TN. Accuracy is one number calculated from those counts, so it cannot show which type of error occurred.

Which confusion-matrix cell is a false alarm?

A false positive is a false alarm: the model predicted positive, but the actual label was negative.

Which metric should I use for an imbalanced dataset?

Do not rely on accuracy alone. Inspect the raw matrix and consider precision, recall, specificity, F1, or balanced accuracy based on the cost of false positives and false negatives.

Can a model have high accuracy and low recall?

Yes. If negative cases dominate, a model can predict negative most of the time, achieve high accuracy, and still miss most positive cases.

Does changing the threshold change the confusion matrix?

Yes. A threshold converts scores into classes, so changing it changes TP, FP, FN, and TN and therefore changes every metric calculated from them.