Logistic Regression
A linear classification model that estimates the probability of a class through the sigmoid function.
Model
$$p(y=1 \mid \mathbf{x}) = \sigma(\mathbf{w}^\top \mathbf{x} + b), \qquad \sigma(z) = \frac{1}{1 + e^{-z}}$$
The natural logarithm of the odds (log-odds) is linear in the features:
$$\log \frac{p}{1-p} = \mathbf{w}^\top \mathbf{x} + b$$
Training
We minimize the log loss:
$$L = -\frac{1}{n}\sum_{i=1}^{n}\Big[y_i \log p_i + (1-y_i)\log(1-p_i)\Big]$$
There is no closed-form solution, so numerical optimization methods are used. scikit-learn applies L2 regularization by default, and its strength is controlled by the parameter C (smaller values mean stronger regularization).
Example
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
X, y = load_breast_cancer(return_X_y=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)
clf = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
clf.fit(X_tr, y_tr)
print(classification_report(y_te, clf.predict(X_te)))
proba = clf.predict_proba(X_te)[:, 1] # probability of class 1We put the scaling inside Pipeline so that its statistics are computed from the training data only.
The threshold
predict uses a threshold of 0.5. Changing the threshold changes the balance between precision and recall, and it must be chosen on the validation data according to the cost of errors in the domain.
Multiple classes
With more than two classes, the model uses the softmax function:
$$p(y=k \mid \mathbf{x}) = \frac{e^{\mathbf{w}_k^\top \mathbf{x}}}{\sum_j e^{\mathbf{w}_j^\top \mathbf{x}}}$$
Interpretability
After scaling the features, the size and sign of a coefficient indicate the strength and direction of the feature's association with the odds, with the usual warning that association is not causation.
Lesson terms
- Log loss
- A loss function that heavily penalizes confident wrong probabilities, equal to the negative logarithm of the probability of the correct class.
- Sigmoid function
- The function σ(z) = 1/(1+e^-z), which maps any real number to a value between 0 and 1.
- Decision threshold
- The probability value above which an example is classified in the positive class.
Abstract
Logistic regression is a classification model that computes a linear combination of the features and then passes it through the sigmoid function to get a probability between 0 and 1. It is trained by minimizing the log loss (binary cross-entropy), and its decision boundary between the classes is linear. It extends to several classes through softmax, and it is used as a strong, interpretable baseline.
Review questions
-
Why don't we use linear regression directly for binary classification?
Because it may give values outside the range [0, 1] and does not estimate the probability appropriately.
-
When do we lower the threshold below 0.5?
When the cost of missed positive cases is high, as in initial medical screening, while accepting more false alarms.
Sources and references
Complete the lesson
You have completed all the lessons in the track! You can now request the track completion certificate.
Request the certificate