34. Classification in Python with Logistic Regression
34.4 Thresholds and Class-Balance Mistakes
Why. Classification notebooks fail quietly when labels are flipped, classes are tiny, or text cities are fed raw without encoding.
What. Watch for: predicting the majority class and calling it success; skipping stratify on rare fraud labels; using LinearRegression on 0/1 y; forgetting that city names need encoding before most sklearn models.
How.
Quick guards
- Print
y.value_counts()before splitting. - Use
stratify=yfor binary labels when possible. - Keep features numeric (or encode categories explicitly).
- Do not celebrate accuracy alone on 95% “not churn” data — Chapter 37.
print(y.value_counts())
# If one class has only 1–2 rows, get more data before trusting metrics.
Ravindra Bagale's Tip
A model that predicts 0 for every row can still show high accuracy, but such a model is useless. This happens when one class is much bigger than the other. Always check the class balance first, so you know what score a simple guess would get.
Ravindra Bagale's Tip – मराठी
प्रत्येक row साठी 0 predict करणाऱ्या model ची accuracy सुद्धा जास्त दिसू शकते, पण असा model निरुपयोगी असतो. एक class दुसऱ्या class पेक्षा खूप मोठा असेल तेव्हा असं घडतं. म्हणून आधी class balance तपासा, म्हणजे एखादा साधा अंदाज किती score करेल हे तुम्हाला कळेल.
Ravindra Bagale's Tip – हिंदी
हर row के लिए 0 predict करने वाला model भी ऊँची accuracy दिखा सकता है, लेकिन ऐसा model बेकार होता है. ऐसा तब होता है जब एक class दूसरी class से बहुत बड़ी हो. इसलिए पहले class balance जाँचो, ताकि तुम्हें पता चले कि एक आसान अंदाज़ कितना score करेगा.