Machine Learning (ML) · Intermediate level

Intermediate level · Module 5: Imbalanced and Messy Real-World Data · Lesson 1 of 3

Class Imbalance

Build useful models when the event of interest is rare.

In this lesson you will

  • Apply class weights and resampling correctly
  • Evaluate imbalanced models with suitable metrics

Key concepts

imbalanceclass_weight='balanced'oversampling and undersamplingprecision–recall evaluation

The lesson

When only 3% of agent-weeks have a shortage, a model can be 97% accurate by always predicting 'no shortage'. Use recall, precision, F1 or average precision instead of accuracy.

Many models accept class_weight='balanced', which makes errors on the rare class count more. Resampling — duplicating rare cases or removing common ones — is an alternative, but must be done only on training folds.

Often the simplest effective step is adjusting the decision threshold, as chosen from costs in the previous module.

Step by step

  1. Measure the baseline's accuracy and recall.
  2. Train with class_weight='balanced'.
  3. Try oversampling inside the pipeline's training folds.
  4. Compare average precision across approaches.

Practice activity

Choose the best approach to imbalance for the cash-shortage model and justify it.

Expected result: A comparison table and a justified choice using imbalance-appropriate metrics.

Check your understanding

  1. Why is accuracy misleading for rare events?

    Show answer

    Always predicting the common class gives high accuracy but finds no events.

  2. Where must resampling happen?

    Show answer

    Only on the training part of each fold, never before splitting.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.