Lesson 4 of 4 Bias and Class Imbalance

Resolving Class Imbalance in Training and Evaluation

This lesson explains how to handle class imbalance in both training and evaluation, since a model trained naively on rare events predicts the majority class and still scores high accuracy.

Learning outcomes

After completing this lesson, you will be able to:

  • Resample numeric data, including with SMOTE.
  • Use class weights and algorithm-level methods.
  • Augment minority classes in text and image data.
  • Evaluate imbalanced models with suitable metrics.

Resampling numeric data

When one class is rare, such as fraud in 1% of transactions, models tend to predict the majority class and miss the minority. Resampling changes the training data to balance the classes. Random undersampling removes majority-class examples; it is fast but discards information. Random oversampling duplicates minority examples; it keeps all the data but can cause the model to overfit to those repeated examples.

SMOTE (synthetic minority over-sampling technique) creates new minority examples by interpolating between a minority example and its nearest minority neighbours, which adds variety rather than exact copies. Variants combine SMOTE with removal of overlapping examples near the class boundary. SageMaker Data Wrangler includes a balance data transform with these options. Resampling is applied only to the training set.

The following diagram shows how SMOTE places synthetic minority examples.

How SMOTE creates minority examples. Random oversampling duplicates the three minority points exactly, which invites overfitting. SMOTE instead places new synthetic points on the lines between a minority example and its nearest minority neighbours, adding variety, and is applied to the training set only.
The points are schematic.
Sort into groups

Resample or reweight?

Exam skills 1.3.5

Sort each technique for imbalanced data by how it works.

Log in to try this activity Interactive activities are free with an account.

Algorithm-level methods

Instead of changing the data, the training objective can be changed so that minority-class errors matter more. Class weights make mistakes on the minority class cost more; for XGBoost, scale_pos_weight is commonly set to the ratio of negative to positive examples. Linear Learner has a positive_example_weight_mult setting and can optimise directly for recall or precision. In deep learning, focal loss reduces the weight of easy examples so the model concentrates on hard ones.

Finally, adjusting the decision threshold after training, for example classifying a transaction as fraud when the predicted probability exceeds 0.2 rather than 0.5, trades precision for recall without retraining the model.

The following diagram compares a fraud model trained on data with 0.2% fraud before and after it is fixed to stop predicting "not fraud" for every transaction.

Why 99.8% accuracy caught no fraud, and the fix. Predicting not fraud for all 1,000,000 transactions is right 998,000 times, so accuracy is 99.8% with recall 0%. Setting scale_pos_weight to 499, evaluating with recall, precision and PR AUC on a test set that keeps its natural 0.2% rate, and choosing the threshold that meets the recall target gives a model that flags fraud.
The scenario is illustrative.
Fill in the value

Set the class weight

Exam skills 1.3.5

Calculate the XGBoost weight for an imbalanced dataset.

Log in to try this activity Interactive activities are free with an account.

Text and image data

For text, create minority examples with back-translation (translate to another language and back with Amazon Translate), synonym replacement or FM-generated paraphrases checked for correctness. For images, apply flips, rotations, crops, colour and brightness changes and noise to the minority class. In both cases, augment only the training set.

Evaluate with metrics that reflect the minority class: precision, recall, F1, PR AUC and the confusion matrix. The following table summarises the techniques.

TechniqueData typeTrade-off
UndersamplingAnyLoses majority information
OversamplingAnyRisk of overfitting duplicates
SMOTENumericSynthetic points may be unrealistic near class overlap
Class weights / scale_pos_weightAny (algorithm setting)No data change; needs tuning
Back-translation, paraphrasingTextCheck that labels stay correct
Geometric and colour augmentationImagesKeep transformations realistic
Threshold tuningAny (after training)Shifts precision-recall balance
Match the pairs

Balance text and images

Exam skills 1.3.5

Match each imbalanced dataset with a suitable technique.

Log in to try this activity Interactive activities are free with an account.
Worked example

Fraud with 0.2% positives

A fraud dataset has 1,000,000 transactions with 2,000 frauds. An XGBoost model reports 99.8% accuracy but catches no fraud.

Predict "not fraud" for everything  -> accuracy 99.8%, recall 0%
scale_pos_weight = 998,000 / 2,000  = 499
Evaluate with: recall, precision, PR AUC on an untouched test set
Then choose the threshold that meets the business target (e.g. recall >= 85%)

Setting scale_pos_weight and optimising PR AUC produces a model that flags fraud. The test set keeps its natural 0.2% rate so the reported precision reflects production. Oversampling the test set would make precision look far better than it will be.

Check yourself

How is scale_pos_weight set in XGBoost for imbalanced data?

Show answer

To the number of negative examples divided by the number of positive examples.

Exam Tips

Remember for the exam

  • Accuracy is misleading for imbalanced classes; use recall, precision, F1 or PR AUC.
  • XGBoost: scale_pos_weight = negatives / positives.
  • SMOTE synthesizes numeric minority examples.
  • Augment text with back-translation and images with transformations.
  • Never resample or augment the test set.