Imbalanced data

Machine Learning · Preparing data a model can learn from · 05 / 05 · Intermediate · 14 min

When one class vastly outnumbers another, why accuracy becomes a misleading metric, and the techniques — resampling, class weights — that address it.

Before thisFeature engineering

Emma Ding · 14 min

Imbalanced data

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Emma DingReport a problem with this video

What you'll learn

  • When one class vastly outnumbers another, a model that always predicts the majority class achieves high accuracy while being completely useless, which is exactly why accuracy alone is a misleading metric here.
  • Resampling techniques change the training data itself — oversampling the minority class, undersampling the majority, or synthesising new minority examples with SMOTE — to give the model a more balanced view during training.
  • Class weights change the training objective instead of the data, penalising a mistake on the minority class more heavily than an identical mistake on the majority class.

When accuracy stops meaning anything

Fraud detection, rare disease diagnosis, and manufacturing defect detection share a structural feature: the thing being predicted is rare. If only one in a thousand transactions is fraudulent, a model that predicts “not fraud” for every single transaction achieves 99.9 percent accuracy while being completely useless — it has never once correctly identified the case that actually matters. This is not a hypothetical edge case; it is the default outcome of training naively on imbalanced data without accounting for the imbalance at all, because that trivial always-predict-the-majority model is a genuine local optimum for plain accuracy.

This is exactly why evaluation metrics beyond raw accuracy — precision, recall, and the tradeoff between them — matter enormously once a dataset is imbalanced, and why this lesson sits right before that one.

What to watch for

The video covers the full scope in one pass: why accuracy misleads on imbalanced data, and then a survey of both the resampling and the class-weight fixes, which is exactly the shape of this lesson rather than a narrow dive into one single technique.

Resampling: changing the data

Oversampling duplicates examples of the minority class so it makes up a larger share of the training set, at the risk of the model simply memorising those duplicated examples rather than generalising from them. Undersampling instead removes examples of the majority class, which avoids duplication but throws away data that might otherwise have been useful, and can hurt badly when the original dataset was not large to begin with.

SMOTE (Synthetic Minority Oversampling Technique) improves on plain duplication by generating new, synthetic minority examples rather than copying existing ones — each synthetic point is created somewhere between two real minority examples that are close to each other in feature space. This gives the model new, plausible minority examples to learn from rather than exact repeats, which generally helps it generalise better than duplication alone.

Class weights: changing the cost instead

Rather than touching the data at all, an alternative approach changes what the model is being optimised to minimise. A standard training objective treats every mistake equally; a weighted objective makes a mistake on the minority class cost more than an identical mistake on the majority class, directly countering the model’s natural tendency to ignore a rare class in favour of the much larger one. This has a practical advantage over resampling — the original data is left completely untouched, with no duplicated or synthetic examples introduced — though it requires a model and training setup that actually supports weighted costs, which not every implementation does equally well.

Choosing the right metric is still the real fix

Both of these techniques improve what a model learns, but neither one matters at all if the metric used to judge the model continues to be plain accuracy. Precision, recall, and the balance between them, covered in the evaluation metrics lesson immediately following this one, are what actually reveal whether a model handles the minority class well — and in a genuinely imbalanced problem, that choice of metric usually matters more than which specific resampling technique or weighting scheme gets used.

Check yourself

  1. 01Why can a model with 99% accuracy still be useless on an imbalanced dataset?

    If 99% of examples belong to one class, a model that always predicts that class achieves 99% accuracy while never correctly identifying a single example of the minority class — the number looks excellent and the model has learned nothing useful.

  2. 02What is the difference between how resampling and class weights address imbalance?

    Resampling changes the training data itself, by duplicating, synthesising, or removing examples before training. Class weights leave the data untouched and instead change the cost function, making an error on the minority class more expensive to the model than the same error on the majority class.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark