Lesson 10
Imbalanced Data
Nine beginner steps on one exam of 100 papers: 90 pass and 10 fail, and why 90 percent accuracy can still miss every fail.
What You Will Learn
Nine steps. One exam. 100 papers. Click a step to jump.
- 1. What is imbalanced data?
- 2. Types of fixes
- 3. Where is it used?
- 4. Where it sits in the workflow
- 5. What problem does accuracy hide?
- 6. How a careful count fixes the reading
- 7. Key Takeaways
- 8. Mind Map
- 9. Quiz
1. What is imbalanced data?
The class you care about is the small pile.
This exam has 90 pass papers and 10 fail papers. Fail is the class a teacher must not miss. It is only 10 out of 100, so any rule that says "pass" will look clever if you only count how many papers it gets right.
That is imbalanced data: one class has many more rows than the other. The model can ignore the small class and still post a high score.
2. Types of fixes
Three honest ways to stop the 90 passes from drowning the 10 fails. The chart counts rows, not accuracy.
| Fix | What the model sees | On this exam |
|---|---|---|
| Class weight | The same 100 papers, but each fail counts as 9 passes | 10 fails × 9 = 90, the same voice as the pass pile |
| Copy the fails | Each fail is repeated until there are 90 fails | 90 pass and 90 fail, 180 rows |
| Keep fewer passes | Throw away passes until the piles match | 10 pass and 10 fail, 20 rows |
Copying is oversampling. Throwing passes away is undersampling. A later method, SMOTE, invents new fail rows near the real ones instead of photocopying the same 10. The arithmetic on this page stays with the three rows in the chart.
3. Where is it used?
Anywhere the costly class is rare.
| Job | The large pile | The small pile |
|---|---|---|
| This exam | Pass | Fail |
| Payments | Ordinary payments | Fraud |
| Clinics | Well visits | The condition you are screening for |
| Factories | Good parts | Defects |
| Ordinary mail | A rare abuse pattern |
Spam can be common. Do not assume the rare class is always the "bad" name. Count the rows.
4. Where it sits in the workflow
- Count the classes before you trust a score. Here, 90 and 10.
- Choose a fix: weights, copies, or fewer passes.
- Fit that choice on the training papers only.
- Score the held-out papers with recall and precision, not accuracy alone.
A tree model from gradient boosting can still use these papers. The fix is about how much each fail is allowed to matter, not about a new kind of tree.
5. What problem does accuracy hide?
Accuracy is the share of papers marked correctly.
The lazy rule marks every paper pass.
Correct papers: the 90 passes. Missed: all 10 fails.
Accuracy = 90 / 100 = 0.90
Fails caught = 0 / 10 = 0
A score of 90 percent, and not one fail found. That is the trap.
Two more costs sit beside it.
Copying the same 10 fails nine times can make the model memorize those 10 students. A new fail that does not look like them is still missed. Extra copies are a bit like a model that hugs the old rows, the worry in regularization.
Keeping only 10 passes throws away 80 ordinary papers. The model never learns what a normal pass looks like.
Do not balance the pile you are using as the final test. The test should stay 90 and 10, like the real exam. Change the training pile only.
6. How a careful count fixes the reading
A careful rule flags 20 papers as fail. 8 of those are real fails. 12 are passes flagged by mistake. The fails it missed: 10 − 8 = 2.
Precision answers: of the papers I flagged, how many really failed?
8 / 20 = 0.40
Recall answers: of the real fails, how many did I catch?
8 / 10 = 0.80
F1 is the balance of those two. Neither is allowed to hide the other.
2 × 0.40 × 0.80 / (0.40 + 0.80) = 0.64 / 1.20 = 0.53
Accuracy for this careful rule:
Passes marked correctly: 90 − 12 = 78
Fails marked correctly: 8
Accuracy = 86 / 100 = 0.86
Accuracy went from 0.90 down to 0.86. Fails caught went from 0 to 8. For this exam, the second number is the one that matters.
The weight version does not add rows. Each fail is given 9 times the say of one pass, because 90 / 10 = 9. Ten fails then speak as loudly as ninety passes. You still report precision and recall on the original 100.
The module that counts the papers
The playground needs pandas only. import pandas as pd loads the table module and nicknames it pd.
| Piece | What it does here |
|---|---|
import pandas as pd |
Load the table module |
pd.DataFrame |
Build one column named result |
["pass"] * 90 |
A list of 90 passes |
value_counts() |
How many rows say pass, how many say fail |
.mean() |
The share of rows equal to pass, which is the lazy accuracy 0.90 |
import pandas as pd
papers = pd.DataFrame({"result": ["pass"] * 90 + ["fail"] * 10})
print(papers["result"].value_counts())
lazy_accuracy = (papers["result"] == "pass").mean()
print(lazy_accuracy)value_counts() prints pass 90 and fail 10. mean() prints 0.9. That 0.9 is the lazy accuracy, not the share of fails you caught.
7. Key Takeaways
- This exam is 90 pass and 10 fail.
- Marking every paper pass scores 0.90 and catches 0 fails.
- Precision here is 8/20 = 0.40. Recall is 8/10 = 0.80. F1 is 0.53.
- That careful rule has accuracy 0.86, lower than 0.90, and it is the more useful one.
- A weight of 9 on each fail lets 10 fails speak as loudly as 90 passes.
- Copying fails or dropping passes changes the training pile. Leave the test pile at 90 and 10.
- The count uses pandas:
import pandas as pd, thenvalue_counts().
8. Mind Map
Twenty flags, eight of them real fails. If you can still say 8 out of 10 fails, and you know the lazy rule caught none, the page is yours.
9. Quiz
The questions use these papers. 90 pass, 10 fail, lazy accuracy 0.90, careful flags 20 with 8 real fails.
Loading playground…
Check your understanding
Imbalanced Data Quiz
This exam has 90 pass papers and 10 fail papers. A rule marks every paper pass. What is the accuracy?
1 / 6