What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
A confusion matrix shows how a classifier’s predicted labels compare with the known labels. Each cell counts examples for one actual-and-predicted class pair, making correct predictions and specific errors visible. For developers, it is a practical starting point for calculating metrics such as precision, recall, F1, and accuracy—and for checking whether those metrics reflect the real cost of mistakes.
How do you read a confusion matrix?
Start by checking which axis represents actual labels and which represents predictions. In scikit-learn’s documented convention, actual classes are rows and predicted classes are columns: C[i,j] counts observations known to be in class i and predicted to be in class j. Other tools may orient a matrix differently, so do not interpret cells until you have confirmed the axis convention and class order.
As an Amazon Associate I earn from qualifying purchases.
For a binary classifier where class 0 is negative and class 1 is positive, that convention gives this matrix:
Recommended Free Tools
| Actual Predicted | Negative (0) | Positive (1) |
|---|---|---|
| Negative (0) | True negative (TN) | False positive (FP) |
| Positive (1) | False negative (FN) | True positive (TP) |
In other words, TN = C[0,0], FP = C[0,1], FN = C[1,0], and TP = C[1,1]. If you change the label order, the counts move to different cells. The scikit-learn API lets you set the order with the labels argument.
#1 Best Overall
What do TP, FP, TN, and FN mean?
“True” means the predicted class matches the known class; “false” means it does not. “Positive” and “negative” refer to the class assigned to an example, not whether the prediction or outcome is good or bad.
- True positive (TP): An actual positive example predicted positive.
- True negative (TN): An actual negative example predicted negative.
- False positive (FP): An actual negative example incorrectly predicted positive.
- False negative (FN): An actual positive example incorrectly predicted negative.
The two diagonal cells are correct predictions; the off-diagonal cells are errors. Which error matters more depends on the application: an FP can trigger an unnecessary action, while an FN can leave a real positive undetected.
How do you calculate metrics from the counts?
With TP, TN, FP, and FN established, you can calculate several commonly reported metrics. The formulas assume their denominators are nonzero.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Rank #2
- Use scikit-learn to track an example ML project end to end
- Explore several models, including support vector machines, decision trees, random forests, and ensemble methods
- Exploit unsupervised learning techniques such as dimensionality reduction, clustering, and anomaly detection
- Dive into neural net architectures, including convolutional nets, recurrent nets, generative adversarial networks, autoencoders, diffusion models, and transformers
- Use TensorFlow and Keras to build and train neural nets for computer vision, natural language processing, generative models, and deep reinforcement learning
| Metric | Formula | Question it answers |
|---|---|---|
| Accuracy | (TP + TN) / (TP + TN + FP + FN) | What share of all predictions was correct? |
| Precision | TP / (TP + FP) | Among examples predicted positive, what share was actually positive? |
| Recall (true positive rate) | TP / (TP + FN) | Among actual positives, what share did the classifier find? |
| False positive rate | FP / (FP + TN) | Among actual negatives, what share was incorrectly flagged positive? |
| F1 | 2TP / (2TP + FP + FN) | What is the harmonic mean of precision and recall? |
Precision and recall answer different questions
Precision focuses on predicted positives and is useful when false alarms are costly or positive predictions must be trustworthy. Recall focuses on actual positives and is useful when missing a positive is costly. A classifier can improve one at the expense of the other, so neither number should be read without knowing the intended use.
F1 is a summary, not a complete cost model
Standard F1 gives precision and recall equal relative contribution through their harmonic mean. It does not include true negatives directly, and it does not encode application-specific costs for FP and FN errors. Use it when a combined precision-and-recall summary is appropriate, not as a substitute for deciding which mistakes matter.
Handle zero denominators explicitly
Some metrics are undefined when their denominator is zero—for example, precision when there are no predicted positives. Libraries may handle this differently or expose a setting. In scikit-learn, F1’s zero_division parameter controls behavior for such cases. When reporting a score, state the convention rather than presenting an undefined value as an ordinary measurement.
Rank #3
Why can accuracy mislead on imbalanced data?
Accuracy weights every example equally, so a model can score well by favoring a common class while failing on a rare one. Google’s Machine Learning Crash Course gives a hypothetical illustration: if positives account for 1% of examples, a classifier that always predicts negative reaches 99% accuracy but finds none of the positives. That 1% is an illustrative scenario, not a measured prevalence or study result. Google recommends considering accuracy alongside precision, recall, and related metrics, particularly when class balance is uneven.
False positive rate can also be unstable when there are very few actual negatives: a small change in FP count may noticeably shift the ratio. Keep class counts in view when interpreting rates, rather than treating a percentage as independent of the sample volume behind it.
How should you choose a metric and threshold?
Choose the measure that matches the decision the classifier supports. Before comparing models or publishing a score, work through these questions:
Rank #4
- Which error is more costly? If false positives are the main concern, examine precision and false positive rate. If missed positives are more harmful, examine recall.
- How balanced are the classes? On strongly imbalanced data, do not use accuracy alone to represent performance.
- Is the decision threshold fixed or being tuned? Precision and recall are computed at a particular threshold. Changing the threshold can trade one against the other, so report the operating threshold when it is relevant.
- Do you need class-level detail or one aggregate? A single score may hide weak performance on an important class.
There is no universally best metric: the appropriate choice follows from error costs, prevalence, threshold, and whether the reader needs a per-class or aggregate view. F1 can summarize precision and recall, but it cannot supply application-specific priorities that were never included in the formula.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.What changes in a multiclass confusion matrix?
For multiple classes, the matrix has one row and one column per class. A diagonal cell counts correct predictions for that class; an off-diagonal cell shows which other class it was mistaken for. This detail can reveal patterns a single overall score obscures—for example, a classifier may distinguish most classes well but regularly confuse two similar ones.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minutePrecision, recall, and F-measures can be computed separately for each class, then combined using an averaging convention. Scikit-learn supports binary, macro, weighted, and other averaging modes for F1. Macro averaging gives each class equal weight; weighted averaging accounts for class support. State the averaging method when reporting a single value, because the result can depend on how class frequencies are treated. Scikit-learn’s model-evaluation documentation describes these options.
Best Value
How to calculate a confusion matrix in scikit-learn
The confusion_matrix function accepts true labels and predicted labels, with optional label ordering, sample weights, and normalization. This minimal example follows the documented API:
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_true, y_pred)
Here, y_true contains the known labels and y_pred contains the classifier’s estimates. The returned matrix contains counts by default. The labels argument can specify or reorder the classes; normalize can request normalized output. Keep the raw counts available when using normalized values, since proportions alone hide how many observations support each cell.
For binary interpretation, confirm the class order and positive-label convention before assigning TN, FP, FN, and TP. For a class-specific metric report, pair the matrix with precision, recall, or F-score output and document the averaging and zero-division behavior used.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




