What does class_weight="balanced" do in scikit-learn classifiers such as LogisticRegression?
Options
- It duplicates rows of the minority class
- It removes rows of the majority class
- It weights each class inversely to its frequency, so mistakes on the rare class cost more (Correct option)
- It sets the decision threshold to match the class ratio
Answer
It weights each class inversely to its frequency, so mistakes on the rare class cost more
Why
The data stays the same. Each sample gets a weight of n_samples / (n_classes × n_samples_of_its_class), so errors on rare examples count more in the loss.
This often gives results close to oversampling without copying rows.
It changes the model's probabilities, so recalibrate them if you need real probabilities.
Remember
Class weights make rare-class errors cost more.