liveBook · Manning

Class imbalance

As explained in Fight Fraud with Machine Learning

Class imbalance is a condition in which the classes in a labeled dataset have substantially unequal frequencies.

Class imbalance occurs when labeled classes have substantially unequal frequencies. In fraud detection, genuine transactions usually dominate, so accuracy can hide poor fraud recognition; precision, recall, and F-score become more useful. section 2.1.1 section 3.3.2

Handle imbalance through an explicit inspection-and-training workflow:

  • Count each target class and calculate its percentage before training. section 3.3.2
  • Compare numerical and categorical feature distributions across fraudulent and genuine samples. section 3.3.2
  • Use class weights or a weighted loss so minority-class mistakes receive greater penalty. section 3.3.2 section 6.3.2
  • Tune the weight carefully: low values can neglect the minority class, while excessive values can encourage predicting every example as minority. section 6.3.2

Fraudulent transactions

A transaction dataset contains 8,213 fraudulent records among 6.36 million transactions, approximately 0.13% fraud. Training with a weighted loss gives rare fraudulent cases more influence than their raw frequency would provide. section 3.3.2

Forged receipt pixels

Receipt forgery regions occupy only a small part of each image. A DeepLabV3 segmentation model uses inverse-frequency class weights with PyTorch CrossEntropyLoss; the weight remains a tunable hyperparameter. section 6.3.2

class_frequencies = [1.2, 1]  #A
weights = 1.0 / torch.tensor(class_frequencies, dtype=torch.float32)  #B
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)  #C
loss_function = torch.nn.CrossEntropyLoss(weight=weights)  #C

Inspect the PyTorch implementation that derives inverse-frequency weights and passes them to CrossEntropyLoss. section 6.3.2

How Fight Fraud with Machine Learning covers it

How deep it goes
Defines unequal frequencies, quantifies a 0.13% fraud rate, and explains inverse-frequency weighting while deferring broader strategies to an appendix.
Examples
Uses phishing webpages, 0.13% fraudulent transactions, and forged-versus-genuine receipt pixels as imbalance examples.
What you can do after
Apply it via PyTorch CrossEntropyLoss with inverse-frequency class weights and inspect class distributions before training.
How it connects
Connects dataset distributions to weighted loss functions, cross-entropy loss, model training, and precision-recall metrics.

Also explained in

Compare how 13 books explain class imbalance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fight Fraud with Machine Learning. The section links point to the parts of the book it draws on.