Notes

Weighted Loss Function

When a model makes mistakes, not all mistakes hurt equally. Predicting “not fraud” for a fraudulent transaction can be far more expensive than flagging a legitimate purchase, and a weighted loss function is how you teach that reality to the training process.

What it is and how it works

A loss function turns each prediction error into a number the optimizer tries to minimize. A weighted loss function multiplies each example’s (or each class’s) loss by a weight, so some errors contribute more to the total objective. In classification, this is commonly done by assigning higher weight to the minority class or to the class where false negatives are costly. In regression, weights can emphasize high-value customers, high-risk regions, or more reliable measurements.

Intuition: steering the optimizer

Gradient-based training follows the loss: bigger weighted errors create bigger gradients, which pull the model parameters harder. It’s like giving the optimizer a “louder signal” on the cases you care about most. This changes the learned decision boundary: the model becomes more willing to sacrifice performance on low-cost cases to reduce high-cost mistakes.

Where you see it in practice
  • Fraud detection: upweight fraud cases to reduce missed fraud (false negatives).
  • Medical screening: penalize missing a disease more than a false alarm.
  • Churn prediction: weight high-revenue customers more heavily than low-revenue ones.

In scikit-learn you’ll encounter this as class_weight (e.g., LogisticRegression(class_weight="balanced")) or sample_weight passed to fit. If you ignore weighting when costs are unequal, you can get a model with a great overall accuracy that still performs badly where it matters most.

A Weighted Loss Function modifies a model’s training objective by multiplying per-example or per-class loss terms by specified weights, so some errors contribute more to the total loss than others. It encodes unequal misclassification or prediction costs directly into optimization (e.g., higher weight on false negatives for a rare but critical class). It matters because it steers learning toward cost-aligned performance under class imbalance or asymmetric error consequences.

Imagine you’re training a new cashier. If they accidentally give the wrong change by a few cents, it’s annoying. But if they accidentally accept a fake $100 bill, that’s a much bigger problem. You’d want training to focus more on avoiding the costly mistake.

A Weighted Loss Function does the same thing for AI. A “loss function” is just a score that tells the model how bad its mistakes are. “Weighted” means some mistakes count more than others. This is useful in things like fraud detection or medical screening, where missing a rare but serious case should be treated as a bigger deal than a harmless false alarm.