Sub topic 18
Assumptions, Perfect Separation, and When to Move On
This chapter assumes you can fit a logistic regression with scikit-learn and that you know log loss, L2 regularization, and odds ratios from earlier chapters. It covers what the model takes for granted, how it fails when classes separate cleanly, and when another model is the better choice.
What the model assumes
Logistic regression makes fewer assumptions than people expect. It does not need normally distributed features. It does need these:
- Linear log-odds. The log-odds of the positive class is a linear function of the features. If the true relationship is curved, the decision boundary is a straight line and will be wrong.
- Independent observations. Rows should not be repeated measurements of the same patient, customer, or device. Otherwise your evaluation scores look better than they are.
- Little multicollinearity. Highly correlated features make individual coefficients unstable. Predictions can still be fine, but you cannot read the coefficients reliably.
- Enough events. A common rule of thumb is at least 10 examples of the rarer class per feature.
You can fix a non-linear relationship by adding transformed features, such as squares or interactions, and still stay inside the same model.
Perfect separation
If a straight line splits the two classes with no errors, the likelihood keeps improving as the weights grow. The best unregularized solution has infinite weights, because the sigmoid can push predicted probabilities ever closer to 0 and 1. Solvers stop only at an iteration limit, and the coefficients they report are arbitrary.
import numpy as np
from sklearn.linear_model import LogisticRegression
X = np.array([[1.0], [2.0], [3.0], [4.0]])
y = np.array([0, 0, 1, 1])
for C in [1.0, 1e6]:
clf = LogisticRegression(C=C, max_iter=10000).fit(X, y)
print(C, clf.coef_[0][0])With C=1.0 the L2 penalty keeps the weight modest. With C=1e6 the penalty is almost gone and the weight is much larger. Raise C further and it keeps growing.
Signs to watch for:
- Coefficients in the tens or hundreds, even after scaling.
- Predicted probabilities that are all extremely close to 0 or 1.
- Convergence warnings from the solver.
When to move on
Stay with logistic regression when you need a fast baseline, interpretable coefficients, or well-behaved probabilities. Consider another model when the evidence points that way:
| Symptom | Likely next step |
|---|---|
| Non-linear patterns that feature engineering does not capture | Gradient-boosted trees or a random forest |
| Many strong feature interactions | Tree ensembles or a neural network |
| Very high-dimensional sparse text | Keep logistic regression with L1 or L2, which works well here |
| Need for a margin-based boundary on small data | Support vector machine |
Always compare against the logistic baseline. If a complex model gains only a little on held-out data, the simpler model is usually the better choice.
Exercise
A logistic regression on standardized features reports a coefficient of 45 for one feature, and the solver warns it did not converge. Name two likely causes and one fix for each.
Show answer
One cause is perfect (or near-perfect) separation. The fix is to lower C to add L2 regularization. The other is a leaking feature that encodes the label. The fix is to remove it or rebuild it so it uses only information available at prediction time. Raising max_iter alone does not help in either case.