Why imbalance breaks accuracy
When one class dominates — say 95% negative, 5% positive — a model can hit 95% accuracy by doing nothing useful: always guess "negative." The loss barely punishes it, because the rare class contributes so few examples to the gradient. This is the classic fraud/disease/defect-detection trap, and it's why accuracy is a liar on imbalanced data. You have to measure precision and recall on the minority class to see what's actually happening.
Three levers, in order of cost
Reach for these from cheapest to most involved. (1) class_weight — a dict passed to fit() that scales each class's loss contribution, so a mistake on the rare class hurts more. Free, one line, try it first. (2) sample_weight — a per-example weight array when you need finer control than per-class. (3) Resampling — oversample the minority (duplicate or synthesize with SMOTE) or undersample the majority, done in the data pipeline before fit() ever sees the data.
One more lever at inference
Even after weighting, you can tune the decision threshold. A binary classifier defaults to calling anything above 0.5 "positive," but on imbalanced data lowering that to, say, 0.3 trades precision for recall — useful when missing a positive (an undiagnosed disease) costs far more than a false alarm. Threshold tuning is post-hoc and free, so it pairs well with the training-time levers above.