Evaluating a Classification Model: Confusion Matrix, Precision and Recall
Evaluating a classification model goes beyond accuracy: you need the confusion matrix and precision and recall to know what kind of mistake the model makes. Accuracy collapses every error into a single number and, when classes are imbalanced, it usually hides the error that matters. Choosing between precision and recall depends on the cost of each error in the problem you are solving.
Why isn't accuracy enough to evaluate a model?
Accuracy is the share of correct predictions out of all predictions. It is easy to compute and easy to explain, but it treats every error as if it cost the same — and errors rarely cost the same.
The problem appears with imbalanced classes. If the class you care about is rare, a classifier that always answers "no" gets most cases right and still finds no positive case at all. Accuracy stays high while the model is useless for the decision it was built to support.
Accuracy also says nothing about the decision threshold. In models that return a probability, such as logistic regression, moving the cut-off changes the balance between false positives and false negatives without changing the trained model.
- It treats an error on a rare class and an error on a common class as equivalent.
- It does not show whether the error is a false positive or a false negative.
- It does not reveal whether the model learned a pattern or just repeated the majority class.
- It does not tell you which decision threshold makes sense for the problem.
What does a confusion matrix show that accuracy hides?
A confusion matrix cross-tabulates what the model predicted against what actually happened. In a binary problem it has four cells: true positive, true negative, false positive and false negative. Precision, recall, specificity and F1 all come from this table.
Two cells count the hits and two count the misses, split by type. Two models with the same accuracy can fail in completely different ways, and that difference is what decides whether a model is usable for your case.
With more than two classes the matrix grows, but the logic holds. Each row or column shows where the model confuses one class with another, which points to pairs of classes the model cannot separate.
- True positive: the model predicted positive and it was positive.
- True negative: the model predicted negative and it was negative.
- False positive: the model predicted positive and it was not — a false alarm.
- False negative: the model predicted negative and it was positive — a missed case.
How do you calculate precision and recall, and what does each one answer?
Precision answers: of the cases the model flagged as positive, how many really were positive. It is the ratio of true positives to true positives plus false positives. High precision means few false alarms.
Recall answers: of the cases that really were positive, how many the model found. It is the ratio of true positives to true positives plus false negatives. High recall means few missed cases.
The two move in opposite directions in most adjustments. Lowering the decision threshold tends to capture more positives and create more false positives: recall goes up and precision goes down. Raising the threshold does the opposite. F1 combines both in a harmonic mean and works as a summary when there is no reason to prioritize one.
Specificity is worth reading too. It measures correct calls among the actual negatives, so it matters when the false positive is the expensive error, and it completes the reading of the matrix alongside precision and recall.
Which metric should you prioritize in your problem?
The answer comes from the cost of each error, not from preference. When letting a positive case slip through is expensive, recall is the metric that matters. When a false alarm is expensive, precision matters.
In fraud detection the false negative is usually the costly error: a fraudulent transaction that is not blocked causes direct loss. In a filter that decides whether a message goes to the inbox or to spam, the false positive is more annoying, because it hides a legitimate message.
When classes are balanced and both errors cost about the same, accuracy becomes a reasonable metric again — but it is still worth reporting per-class numbers. And any metric only means something next to a reference: compare it with what a majority-class predictor would score on the same data.
Where do precision and recall fit in a full project?
In practice the metrics come at the end of a sequence: clean and prepare the data, check for outliers, split the data, train and compare several models, validate with cross-validation, and only then read the confusion matrix and choose the threshold. The metric you report should match the decision the model will support.
The Python for Data Science and Analytics course covers that sequence from zero to advanced, in three main modules — Basic, Intermediate and Advanced — plus bonus modules. It starts with IDE and Jupyter Notebook setup, basic functions, NumPy and Pandas, user-defined functions, loops and conditionals, data visualization, data cleaning and preparation and outlier detection, then moves into machine learning: linear and logistic regression, K-Means, SVM, Naive Bayes, decision tree, Random Forest, Gradient Boosting, XGBoost, LightGBM, feature selection and cross-validation. It closes with a final project that runs exploratory analysis and machine learning end to end.
Lessons are recorded in high quality and you watch them at your own pace, with guided exercises, real market projects for your portfolio, a certificate of completion and a community for networking. Access does not expire, and you do not need to know how to program before starting. The course costs R$ 470, or 12 payments of R$ 20.37 on a card, through the Hotmart checkout, which accepts card or boleto. The full outline is at python.paanalytics.net.
| Metric | Question it answers | What a high value means |
|---|---|---|
| Accuracy | How many predictions were correct out of all predictions? | Most predictions are correct — but it says nothing about the type of error. |
| Precision | Of the cases flagged positive, how many were really positive? | Few false alarms. |
| Recall | Of the cases that were really positive, how many were found? | Few missed cases. |
| Specificity | Of the cases that were really negative, how many were left alone? | Few false alarms among the actual negatives. |
| F1 | How do precision and recall balance out in a single number? | Precision and recall are both reasonably high. |
All definitions come from the standard confusion-matrix formulas (TP, TN, FP, FN). No vendor, benchmark or competitor data is used.
Learn to evaluate models with a confusion matrix, precision and recall
Secure payment via Hotmart, up to 12 installments.
Enroll now Product siteFrequently asked questions
What counts as a good precision or recall score?
There is no universal number. It depends on the cost of each error in your problem and on the baseline you compare against. A useful first step is to check what a majority-class predictor would score on the same data; if your model barely beats that, the metric is not telling you much. Report precision and recall together rather than one alone.
What is the difference between recall and specificity?
Recall is measured over the actual positives: of everything that was positive, how much the model caught. Specificity is measured over the actual negatives: of everything that was negative, how much the model correctly left alone. Changing the decision threshold moves them in opposite directions, the same way it moves precision and recall.
Can I still use accuracy with imbalanced classes?
You can report it, but it should not be the only number. With imbalanced classes, add per-class precision and recall and read the confusion matrix. Splitting the data so that the class proportions are kept in the training and test sets makes those per-class numbers more trustworthy.
When is F1 a better choice than precision or recall?
F1 combines precision and recall into a single harmonic mean, which is useful when you need one number to compare models and neither error is clearly more expensive. When one error is clearly costlier, F1 can hide it: a model with high recall and poor precision can still post a decent F1.
Does the course cover model evaluation in Python?
Yes. The machine learning modules cover logistic regression, decision tree, Random Forest, Gradient Boosting, XGBoost, LightGBM, SVM, Naive Bayes and K-Means, along with feature selection and cross-validation, and the course ends with an end-to-end project that includes exploratory analysis and machine learning. Lessons are recorded, you study at your own pace, access does not expire, and there is a certificate and a community. The price is R$ 470, or 12 payments of R$ 20.37 on a card, through the Hotmart checkout, which accepts card or boleto.