liveBook · Manning

Class weights

As explained in Fight Fraud with Machine Learning

A class weight is a training setting that assigns a relative importance factor to a particular target class.

Class weights assign relative importance to target classes during training. They address class imbalance by penalizing mistakes on rare classes more heavily, helping fraud or forgery models pay attention to minority examples. section 3.4.2 section 6.3.2

Use class weights as a tunable part of the training workflow:

  • Inspect class frequencies before training so the imbalance and minority class are explicit. section 3.9
  • Choose uniform weights, inverse-frequency balancing, or an explicit ratio such as {0: 1, 1: 2}. section 3.4.2
  • Apply the weights to the loss function, increasing the cost of mistakes on the selected class. section 3.4.2 section 6.3.2
  • Tune the values with validation metrics such as AUPRC, because excessive weighting can cause the model to predict the minority class too often. section 3.4.2 section 6.3.2

Fraud and receipt-forgery settings

For a fraud-to-genuine ratio of 1:100, balanced weighting assigns approximately {0: 1, 1: 100}, while uniform weighting uses {0: 1, 1: 1}. A random forest search selected {0: 1, 1: 2}; a segmentation example used frequencies [1.2, 1] to derive inverse weights for forged and genuine pixels. section 3.4.2 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

Shows inverse-frequency weights passed directly to PyTorch CrossEntropyLoss for segmentation training. section 6.3.2

How Fight Fraud with Machine Learning covers it

How deep it goes
Explains inverse-frequency weighting, loss integration, tuning trade-offs, and concrete configurations across forests and segmentation models.
Examples
Uses fraud ratios, random-forest weight searches, and forged-versus-genuine receipt pixels as concrete demonstrations.
What it assumes
Assumes supervised classification or segmentation and basic loss functions, while teaching imbalance compensation from class frequencies.
What you can do after
Operate/configure class weights in scikit-learn random forests or PyTorch cross-entropy training and evaluate their imbalance trade-offs.
How it connects
Connects class imbalance with cross-entropy loss, random forest, semantic segmentation, hyperparameter tuning, and AUPRC.

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.