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.

Resample or reweight?
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.

Set the class weight
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.
| Technique | Data type | Trade-off |
|---|---|---|
| Undersampling | Any | Loses majority information |
| Oversampling | Any | Risk of overfitting duplicates |
| SMOTE | Numeric | Synthetic points may be unrealistic near class overlap |
| Class weights / scale_pos_weight | Any (algorithm setting) | No data change; needs tuning |
| Back-translation, paraphrasing | Text | Check that labels stay correct |
| Geometric and colour augmentation | Images | Keep transformations realistic |
| Threshold tuning | Any (after training) | Shifts precision-recall balance |
Balance text and images
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.
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.