∑GATE DA Atlas

Logistic Regression

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.

Section 610 practice questions

From linear scores to probabilities

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)=11+e−(w⊤x+b).P(y = 1 \mid \mathbf x) = \sigma(\mathbf w^\top\mathbf x + b) = \frac{1}{1 + e^{-(\mathbf w^\top\mathbf x + b)}}.
10.50 z = 0σ(z) = 1 / (1 + e⁻ᶻ)
σ maps any real score to (0, 1); σ(0) = 0.5, σ(−z) = 1 − σ(z).

Properties: σ(0)=12\sigma(0) = \frac12; σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z); σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)); the inverse is the logit ln⁡p1−p\ln\frac{p}{1-p}.

Odds and log-odds

odds=p1−p,log⁡p1−p=w⊤x+b.\text{odds} = \frac{p}{1 - p}, \qquad \log\frac{p}{1 - p} = \mathbf w^\top\mathbf x + b.

Logistic regression is linear in the log-odds. Each coefficient is an additive change in log-odds; ewje^{w_j} is the odds ratio for a one-unit increase in xjx_j (holding the others fixed).

Decision rule

Predict class 1 if p≥tp \ge t. With t=0.5t = 0.5 this is w⊤x+b≥0\mathbf w^\top\mathbf x + b \ge 0 — a linear decision boundary. Changing tt moves the boundary parallel to itself, trading precision against recall (see evaluation).

Training by maximum likelihood

With pi=σ(w⊤xi+b)p_i = \sigma(\mathbf w^\top\mathbf x_i + b) and labels yi∈{0,1}y_i \in \{0, 1\}, the likelihood is ∏ipiyi(1−pi)1−yi\prod_ip_i^{y_i}(1 - p_i)^{1 - y_i}. Maximising its log is the same as minimising the cross-entropy (log) loss:

J(w)=−1n∑i=1n[yiln⁡pi+(1−yi)ln⁡(1−pi)].J(\mathbf w) = -\frac1n\sum_{i=1}^n\left[y_i\ln p_i + (1 - y_i)\ln(1 - p_i)\right].

Its gradient has the clean form

∇wJ=1n∑i=1n(pi−yi) xi=1nX⊤(p−y).\nabla_{\mathbf w}J = \frac1n\sum_{i=1}^n(p_i - y_i)\,\mathbf x_i = \frac1nX^\top(\mathbf p - \mathbf y).
  • JJ 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\mathbf w up forever; add L2 regularisation λ2∥w∥2\frac{\lambda}{2}\lVert\mathbf w\rVert^2 (as in ridge) to keep weights finite.
Python
import numpy as np

def fit_logistic(X, y, lr=0.1, epochs=1000, lam=0.0):
    w = np.zeros(X.shape[1]); b = 0.0
    for _ in range(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 KK classes, keep one weight vector per class:

P(y=k∣x)=exp⁡(wk⊤x+bk)∑j=1Kexp⁡(wj⊤x+bj),P(y = k \mid \mathbf x) = \frac{\exp(\mathbf w_k^\top\mathbf x + b_k)}{\sum_{j=1}^K\exp(\mathbf w_j^\top\mathbf x + b_j)},

trained with the categorical cross-entropy −∑iln⁡P(yi∣xi)-\sum_i\ln P(y_i\mid\mathbf x_i). The decision regions are separated by linear boundaries. Binary logistic regression is the case K=2K = 2.

Logistic regression in context

Compared withDifference
Linear regressionpredicts a probability via σ\sigma; trained by log loss, not squared error
Naive Bayes / LDAdiscriminative (models P(y∣x)P(y\mid\mathbf x) directly) rather than generative; LDA with shared covariance gives the same form of boundary, fitted differently
SVMuses log loss (smooth, probabilistic) instead of hinge loss (margin-based, sparse support vectors)
Neural networka 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.5xz = -3 + 0.5x. The predicted probability P(y=1∣x=8)P(y = 1 \mid x = 8) is ______ (round to two decimals).

Q2NAT2 marks

In a logistic regression model, the coefficient of feature xx is 0.50.5. Increasing xx by one unit multiplies the odds of y=1y = 1 by ______ (round to two decimals).

Q3MCQ1 mark

With threshold 0.5, the decision boundary of a logistic regression classifier on raw features x\mathbf x is

Q4NAT2 marks

For one training example with true label y=1y = 1 and predicted probability p=0.8p = 0.8, the log (cross-entropy) loss −[yln⁡p+(1−y)ln⁡(1−p)]-[y\ln p + (1 - y)\ln(1 - p)] is ______ (round to two decimals).

Q5MCQ2 marks

For a single example (x,y)(\mathbf x, y) with p=σ(w⊤x)p = \sigma(\mathbf w^\top\mathbf x), the gradient of the log loss with respect to w\mathbf w is

Q6NAT2 marks

Logistic regression with a single weight ww (no bias) starts at w=0w = 0. One stochastic gradient descent step with learning rate 0.50.5 on the example x=2x = 2, y=1y = 1 gives the new w=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>2K > 2 classes by

Q9NAT2 marks

If a logistic regression model predicts P(y=1∣x)=0.9P(y = 1 \mid \mathbf x) = 0.9, the corresponding log-odds z=w⊤x+bz = \mathbf w^\top\mathbf 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)\sigma(\mathbf w^\top\mathbf x)?