Skillversitas

Logistic Regression with scikit-learn

scikit-learn's LogisticRegression wraps the math from the previous chapters in a stable, optimized implementation with multiple solvers, both penalties, and multiclass support. The interface is the standard estimator API - fit, predict, predict_proba - and the parameters worth knowing are penalty, C, solver, class_weight, and max_iter.

Basic Fit and Predict

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler

X, y = make_classification(n_samples=1000, n_features=10, n_informative=4,
                           n_redundant=2, random_state=42)
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.3, random_state=0)

scaler = StandardScaler().fit(Xtr)
model = LogisticRegression(max_iter=2000).fit(scaler.transform(Xtr), ytr)

print("train acc:", round(accuracy_score(ytr, model.predict(scaler.transform(Xtr))), 3))
print("test  acc:", round(accuracy_score(yte, model.predict(scaler.transform(Xte))), 3))
proba = model.predict_proba(scaler.transform(Xte))[:, 1]
print("probability range:", round(proba.min(), 3), "-", round(proba.max(), 3))

Fit the scaler on the training set only, then transform both splits - fitting on the test set leaks information. predict_proba returns the class probabilities; the column you want depends on the order of model.classes_.

The Parameters That Matter

ParameterDefaultEffect
penaltyl2l2 (shrink), l1 (sparse), elasticnet, none
C1.0inverse regularization strength; smaller = more shrinkage
solverlbfgslbfgs (default, l2), liblinear (good for l1, small data), saga (large data, all penalties), newton-cg (l2, large)
class_weightNone"balanced" rescales per-class loss - see the imbalance chapter
max_iter100raise it (1000+) or the solver may stop before converging and warn

Inspecting the Fitted Model

print(model.classes_)            # [0 1]
print("coef shape:", model.coef_.shape)     # (1, n_features) for binary
print("intercept:", model.intercept_)
print("converged:", model.n_iter_)          # iterations the solver needed

coef_ gives the weights per feature, intercept_ the bias, and n_iter_ tells you whether the optimizer reached its tolerance. A converged model with n_iter_ near max_iter is a hint to raise the cap or rescale features.

Pipeline Note

In production code the scaler and model should be combined in a sklearn.pipeline.Pipeline so the transform travels with the model (one fit, one predict, no forgotten step). The dedicated pipelines chapter covers that; for now keep the discipline: scaler fit on train, applied to both.

Summary

The scikit-learn recipe: split, scale (train-fit only), fit LogisticRegression with max_iter raised, evaluate with predict and predict_proba, and inspect coef_, intercept_, and n_iter_. The solver and penalty choices are tuning decisions, covered with cross-validation in the hyperparameter chapter.