Imbalanced Datasets and Baseline Models
Changing how strict a model is about predicting positive moves a sample across the boundary between positive and negative predictions.
Why a High Score Can Hide a Failing Model
Suppose a dataset has 100 samples, and only 10 of them belong to the class you care about. This class is the minority class. The other 90 samples belong to the majority class. A classifier that never looks at the input and simply answers with the majority class will still be right 90 times out of 100. Its accuracy is 90 percent, which sounds impressive, yet it has found none of the minority cases at all.
This is the central problem of imbalanced datasets. A single accuracy value counts correct predictions, both true positives and true negatives, and divides by the total number of samples. Two models can share the same accuracy while one floods the output with false alarms and the other quietly misses most of the real positive cases. Precision and recall were developed to separate those two failure modes, and the F-score then combines them into one value.
What do you think happens?
A model always predicts the majority class on the 100-sample dataset described above. What is its recall on the minority class?
Reveal answer
Zero
The model never predicts the minority class, so it never captures a true positive. Every minority sample is a false negative, and recall is true positives divided by all actual positives, which gives zero divided by 10.
Tracing the Four Prediction Outcomes
A confusion matrix records how a binary classifier's predictions line up with the true labels. Its cells count four outcomes. A true positive is a sample that is really positive and was predicted positive. A true negative is a sample that is really negative and was predicted negative. A false positive is a sample that is really negative but was predicted positive, which is a false alarm. A false negative is a sample that is really positive but was predicted negative, which is a miss.
Precision and recall both use the true positives in their numerator. They differ only in their denominators, and that single difference determines which mistake each metric counts against the model. The diagram below shows which outcomes feed into each metric.
Precision: Checking the Positive Predictions
Precision answers one question: of all the samples the model labeled as positive, how many really are positive? It only looks at the positive predictions. It ignores the negatives the model correctly rejected, and it ignores the positives the model missed entirely. Precision is therefore sensitive to false positives.
Consider a model that predicts whether a new drug will be effective in clinical trials. Clinical trials are very costly, so a pharmaceutical company only wants to run an experiment when it is quite sure the drug will work. The model therefore needs to avoid producing many false positives, which means it needs high precision. Precision is the right yardstick when false positives are expensive.
Recall: Checking the Actual Positives
Recall flips the viewpoint. Instead of asking how many of the positive predictions are right, it asks how many of the actual positive samples the model managed to capture. Its denominator is the set of true positives plus the positives that were missed, which are the false negatives. Recall is therefore sensitive to false negatives.
Recall is the right yardstick when missing a positive case is the costly error. In cancer diagnosis, it is important to find all the people who are sick, even if a few healthy patients are flagged along the way. A missed sick patient is a false negative, and high recall is the property that keeps those rare.
Computing precision and recall from one confusion matrix
A test set has 100 samples, with 10 truly positive and 90 truly negative. The model finds 8 true positives, produces 4 false positives, and misses 2 positives as false negatives. Compute precision, recall, and accuracy.
- 1Identify the counts
TP = 8, FP = 4, FN = 2. Because 10 samples are positive, the 2 misses plus the 8 hits account for all of them. The 90 negatives leave 4 false alarms, so TN = 90 - 4 = 86.
- 2Compute precision
Precision = TP / (TP + FP) = 8 / (8 + 4) = 8 / 12, which is about 0.67. Of the 12 positive predictions, two thirds are correct.
- 3Compute recall
Recall = TP / (TP + FN) = 8 / (8 + 2) = 8 / 10 = 0.80. The model captures 80 percent of the actual positives.
- 4Compute accuracy
Accuracy = (TP + TN) / total = (8 + 86) / 100 = 0.94. The accuracy is the highest of the three numbers, yet it says nothing about how the 2 misses or the 4 false alarms are distributed.
Precision is about 0.67, recall is 0.80, and accuracy is 0.94. The accuracy looks strongest, which is why precision and recall must be read alongside it.
Trading Precision Against Recall
The two metrics pull against each other. Changing how strict a model is about predicting positive moves a sample across the boundary between positive and negative predictions. Making the model more permissive usually adds true positives and false positives together, which tends to raise recall and lower precision. Making it stricter usually removes false positives but also removes some true positives, which tends to raise precision and lower recall. The two extremes below show why neither can be used alone.
The F-Score as a Harmonic Mean
Because precision and recall each tell only part of the story, it helps to have a single summary that accounts for both. The F-score is the harmonic mean of precision and recall. The most common variant weights the two equally and is called the F1-score.
A harmonic mean differs from the ordinary average. Because it is built from reciprocals, the result stays close to the smaller of the two inputs. Take precision 0.9 and recall 0.1. The ordinary average is 0.5, which looks comfortable. The F1-score is 2 times 0.9 times 0.1 divided by 1.0, which is 0.18. The low recall drags the score down rather than being hidden behind the high precision. That is why the F-score is considered a more faithful picture of a good model than accuracy on imbalanced binary datasets.
Mechanism Trace: Scoring the Four Model Predictions
The source applies these metrics to a 'nine vs. rest' dataset, where the class 'nine' is the positive class and therefore the minority class. Four sets of predictions were produced: from a most-frequent baseline, a dummy classifier, a decision tree, and a logistic regression. Each set is scored against the same true labels. The ordering you would expect appears in the result: the logistic regression scores highest and the most-frequent baseline scores lowest.
The baseline's zero score is a warning sign rather than a broken metric. The baseline makes no positive predictions at all, so its precision denominator is zero and the ratio cannot be computed in the usual way. In the source, this zero denominator is the reason an error message appears for that model. A zero F1 here means the model made no positive predictions.
The F1-score also separates models that accuracy alone does not distinguish sharply. The gap between the dummy classifier and the decision tree is clearer when read through F1 than through accuracy. This is the practical reason the F-score is often chosen for imbalanced problems.
Reading a Classification Report
When you want precision, recall, and F1-score together for every class, scikit-learn provides the classification_report convenience function. It prints one row per class and a final row of averages. Each row reads the metrics with that class treated as the positive class. The columns are precision, recall, F1-score, and support. The rightmost column, support, is the count of samples in that class according to the ground truth. The bottom row is an average of the per-class values weighted by support.
| Column | What it tells you | How to read it |
|---|---|---|
| Precision | Share of positive predictions for this class that are correct | Low values mean many false alarms for this class |
| Recall | Share of actual samples of this class that the model found | Low values mean many misses for this class |
| F1-score | Harmonic mean of precision and recall for this class | Shows the balance between the two for one class |
| Support | Number of ground-truth samples in this class | Small support means the row rests on few samples |
Reading the columns of a per-class classification report
Read the report row by row and column by column. A row shows how one model behaves on one class. A column shows how every class behaves on one metric. The most-frequent baseline has a recall of zero for the 'nine' class because it never predicts that class. To locate where one model's results diverge from what you expected, compare a column across models, then look at the matching row for the other class.
Choosing the Positive Class Changes Every Number
Precision, recall, and F1 are defined relative to whichever class you call positive, so swapping the label changes every number. In the report for the most-frequent baseline, treating 'not nine' as the positive class gives an F1-score of about 0.94. The reason is that this baseline predicts 'not nine' for every sample, so it recalls all of the 'not nine' samples. The same model looks very different depending on the choice.
Common Mistakes With Imbalanced Metrics
Common mistake Trusting accuracy alone on an imbalanced dataset
What it looks likeA model that predicts the majority class for every sample reaches 90 percent accuracy on a 90/10 split.
Why it failsAccuracy counts correct predictions across both classes, so a large majority class can dominate the number even when the minority class is never found.
Do this insteadReport precision, recall, and F1 for the minority class alongside accuracy.
Common mistake Optimizing precision without checking recall
What it looks likePredicting only the single most confident point can give perfect precision while recall is very poor.
Why it failsPrecision ignores the positives the model missed, so a strict model can look excellent while passing over most real cases.
Do this insteadCheck both metrics together, or use F1, before deciding a model is good.
Common mistake Reading a zero score as a bug
What it looks likeThe most-frequent baseline shows an error message and a zero F1 for the 'nine' class.
Why it failsA zero F1 here reflects that the model made no positive predictions, not that the metric is broken.
Do this insteadRead the zero as evidence that the model never predicts the positive class.
Common mistake Comparing scores without naming the positive class
What it looks likeAn F1 of about 0.94 for the baseline is reported with no label, while the 'nine' class score is much lower.
Why it failsEach metric depends on which class is positive, so unlabeled numbers can point in opposite directions.
Do this insteadState the positive class every time you report these metrics.
Practice: Diagnosing a Model From Its Counts
A test set has 25 truly positive samples. A model produces 5 true positives, 15 false positives, and 20 false negatives. Compute precision, recall, and F1. Then decide whether the model's weakness is in its positive predictions or in the positives it missed, and say which class you would name as positive in your report.
Hints
- Count the positive predictions first: TP + FP. Precision divides TP by that total.
- Recall divides TP by all actual positives, which is TP + FN.
- Compare precision and recall before computing F1. The F1 value will be closer to the smaller of the two.
Key Takeaways
- Accuracy can hide failure on imbalanced data, because a majority-class model can score high while missing every minority-class case.
- Precision is TP divided by all positive predictions and penalizes false positives; recall is TP divided by all actual positives and penalizes false negatives.
- Precision and recall pull against each other. Predicting everything positive gives perfect recall with collapsed precision, and predicting only the most confident point can give perfect precision with very poor recall.
- The F1-score is the harmonic mean of precision and recall, which stays close to the smaller input and so cannot hide a weak metric behind a strong one.
- A classification report gives precision, recall, F1, and support per class; locate divergence by comparing columns across models and rows across classes, and always state the positive class.