Regularized logistic regression
Quick answer
Regularized logistic regression fits class probabilities while discouraging excessively large weights. It can reduce instability, especially when features are correlated or the training classes are almost perfectly separated. Examples run locally with Python 3.13.5, scikit-learn 1.8.0, NumPy 2.3.5 and SciPy 1.17.0 when imported. ByteMind library execution is unverified.
Compute a penalized objective
loss_sum=3w=[2,-1]C=0.5print(loss_sum+sum(v*v for v in w)/(2*C))Expected output:
8.0The L2 contribution is 5; the fitted intercept is not in the vector.
How it works
With n unweighted samples, a teaching objective matching scikit-learn’s L2 convention is mean binary logit loss + ||w||²/(2nC), where C is positive. This is equivalent to sum loss + ||w||²/(2*C), and the intercept is excluded for lbfgs. Other objective conventions may absorb n or the factor 2 into their penalty parameter; compare formulas before comparing strengths. For example, a sum loss of 3, weights [2,-1] and C=0.5 produce total 8. In version 1.8, use l1_ratio=0 for L2, 1 for L1, and an intermediate value for elastic net. Smaller C increases regularization; C=inf requests no penalty when supported. lbfgs handles L2, while saga supports mixed penalties; the liblinear solver has a distinct intercept-regularization convention and does not directly fit a multinomial model. With separable training data, unpenalized logistic likelihood may lack a finite optimum. A regularized fit with both classes represented can avoid diverging weight magnitude in that situation. Scaling features before fitting makes the weight penalty less dependent on measurement units; fit the scaler only on training data. Monitor convergence warnings, increase the iteration budget or revisit scaling/solver rather than assuming convergence. Choose C and penalty mix on validation or cross-validation, then lock the decision before evaluating the final test set. Class weighting changes the objective and can change probability calibration; never equate it with threshold tuning.
Compare two regularized fits
from sklearn.linear_model import LogisticRegressionfrom sklearn.pipeline import make_pipelinefrom sklearn.preprocessing import StandardScalerX=[[-3],[-2],[-1],[1],[2],[3]]y=[0,0,0,1,1,1]weights=[]for C in [0.1,10]: m=make_pipeline(StandardScaler(),LogisticRegression(C=C,l1_ratio=0.0,solver="lbfgs",max_iter=1000)).fit(X,y) weights.append(abs(m[-1].coef_[0,0]))print(bool(weights[0]<weights[1]))Expected output:
TrueOn this separable fixture the stronger penalty yields a smaller coefficient; it does not prove better held-out performance.
Common mistakes
Do not confuse C with the alpha used in many regression APIs. In scikit-learn 1.8 penalty is deprecated; older versions have different signatures. Do not judge model quality by training accuracy alone or scale using final test rows.
Practice and next steps
For the worked objective, calculate the total at C=2 and explain the effect of increasing C. In the lab compare several C values on validation only. Complete the knowledge check and coding exercise. Next: Probability versus threshold.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Regularized logistic regression — knowledge check
Apply regularized logistic regression to new examples.
In scikit-learn logistic regression, what happens as C decreases?
Which l1_ratio specifies L2 in scikit-learn 1.8?
Which solver supports elastic-net mixing in scikit-learn 1.8?
Why can unpenalized fitting be problematic on separated data?
Where should the regularization strength be selected?