Modelling class probabilities with the sigmoid, odds and log-odds, interpreting coefficients as odds ratios, the linear decision boundary, maximum likelihood and the cross-entropy (log) loss, its gradient and gradient-descent training, convexity, separable data and regularisation, multinomial (softmax) regression, and threshold choice.
For binary classification, model the probability of class 1 by squashing a linear score through the sigmoid (logistic) function:
P(y=1∣x)=σ(w⊤x+b)=1+e−(w⊤x+b)1.σ maps any real score to (0, 1); σ(0) = 0.5, σ(−z) = 1 − σ(z).
Properties: σ(0)=21; σ(−z)=1−σ(z); σ′(z)=σ(z)(1−σ(z)); the inverse is the logitln1−pp.
Odds and log-odds
odds=1−pp,log1−pp=w⊤x+b.
Logistic regression is linear in the log-odds. Each coefficient is an additive change in log-odds; ewj is the odds ratio for a one-unit increase in xj (holding the others fixed).
Decision rule
Predict class 1 if p≥t. With t=0.5 this is w⊤x+b≥0 — a linear decision boundary. Changing t moves the boundary parallel to itself, trading precision against recall (see evaluation).
Training by maximum likelihood
With pi=σ(w⊤xi+b) and labels yi∈{0,1}, the likelihood is ∏ipiyi(1−pi)1−yi. Maximising its log is the same as minimising the cross-entropy (log) loss:
J(w)=−n1i=1∑n[yilnpi+(1−yi)ln(1−pi)].
Its gradient has the clean form
∇wJ=n1i=1∑n(pi−yi)xi=n1X⊤(p−y).
J is convex, so gradient descent (or Newton's method, called IRLS here) reaches the global minimum.
There is no closed-form solution.
If the classes are perfectly separable, the likelihood can be pushed towards 1 by scaling w up forever; add L2 regularisation 2λ∥w∥2 (as in ridge) to keep weights finite.
Python
import numpy as np
deffit_logistic(X, y, lr=0.1, epochs=1000, lam=0.0):
w = np.zeros(X.shape[1]); b = 0.0for _ inrange(epochs):
p = 1 / (1 + np.exp(-(X @ w + b)))
w -= lr * (X.T @ (p - y) / len(y) + lam * w)
b -= lr * np.mean(p - y)
return w, b
Multi-class: softmax regression
For K classes, keep one weight vector per class:
P(y=k∣x)=∑j=1Kexp(wj⊤x+bj)exp(wk⊤x+bk),
trained with the categorical cross-entropy −∑ilnP(yi∣xi). The decision regions are separated by linear boundaries. Binary logistic regression is the case K=2.
Logistic regression in context
Compared with
Difference
Linear regression
predicts a probability via σ; trained by log loss, not squared error
Naive Bayes / LDA
discriminative (models P(y∣x) directly) rather than generative; LDA with shared covariance gives the same form of boundary, fitted differently
SVM
uses log loss (smooth, probabilistic) instead of hinge loss (margin-based, sparse support vectors)
Neural network
a single neuron with sigmoid activation is logistic regression
Quick revision
Practice questions
GATE-style questions: 3 one-mark and 7 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.
Q1NAT1 mark
A logistic regression model has log-odds z=−3+0.5x. The predicted probability P(y=1∣x=8) is ______ (round to two decimals).
Q2NAT2 marks
In a logistic regression model, the coefficient of feature x is 0.5. Increasing x by one unit multiplies the odds of y=1 by ______ (round to two decimals).
Q3MCQ1 mark
With threshold 0.5, the decision boundary of a logistic regression classifier on raw features x is
Q4NAT2 marks
For one training example with true label y=1 and predicted probability p=0.8, the log (cross-entropy) loss −[ylnp+(1−y)ln(1−p)] is ______ (round to two decimals).
Q5MCQ2 marks
For a single example (x,y) with p=σ(w⊤x), the gradient of the log loss with respect to w is
Q6NAT2 marks
Logistic regression with a single weight w (no bias) starts at w=0. One stochastic gradient descent step with learning rate 0.5 on the example x=2, y=1 gives the new w= ______.
Q7MSQ2 marks
Which statements about (binary) logistic regression are correct?
One or more options are correct.
Q8MCQ1 mark
Logistic regression is extended to K>2 classes by
Q9NAT2 marks
If a logistic regression model predicts P(y=1∣x)=0.9, the corresponding log-odds z=w⊤x+b is ______ (round to two decimals).
Q10MCQ2 marks
Why is logistic regression trained with the log loss instead of the mean squared error between labels and σ(w⊤x)?