Classical ML

Logistic Regression

The baseline classifier every ML candidate is expected to derive on a whiteboard.

🟢 beginner4 min readsupervisedmust-know
Logistic Regression models binary probabilities using the Sigmoid activation: P(y=1|x) = σ(wᵀx + b). It maps linear log-odds to [0, 1]. Because OLS yields non-convex loss for probabilities, we use Binary Cross-Entropy (Log Loss), optimized iteratively via Gradient Descent or L-BFGS. Key interview topics include log-odds interpretation, loss derivation, linear decision boundaries, and multi-class extension via Softmax.

Log-odds and the Sigmoid function

Linear regression outputs unbounded continuous numbers (-∞, ∞). Logistic regression wraps the linear combination in the Sigmoid (logistic) function to constrain outputs to (0, 1):

σ(z) = 1 / (1 + e⁻ᶻ),   where z = wᵀx + b

Derivative of Sigmoid has a clean closed form: σ'(z) = σ(z)(1 - σ(z)).

The Log-Odds interpretation

The ratio of probability of occurrence to non-occurrence is the Odds:

Odds = P(y=1|x) / P(y=0|x) = P / (1 - P) = e^(wᵀx + b)

Taking the natural log gives Log-Odds (Logit):

ln( Odds ) = ln( P / (1 - P) ) = wᵀx + b

Logistic regression is simply linear regression on the log-odds of the positive class.

Loss function: Binary Cross-Entropy (Log Loss)

Under Maximum Likelihood Estimation (MLE), assuming independent Bernoulli trials:

L(w) = - 1/N ∑ [ y_i · log(p_i) + (1 - y_i) · log(1 - p_i) ]

where p_i = σ(wᵀx_i + b).

Gradient of Log Loss

Taking the partial derivative with respect to weight w_j:

∂L / ∂w_j = 1/N ∑ (p_i - y_i) x_ij

Notice how clean this gradient is — the error term (p_i - y_i) multiplied by feature x_ij. This is identical in form to linear regression MSE gradient, making gradient descent updates fast and intuitive.

Why no closed-form solution?

Equating ∂L / ∂w_j = 0 produces a system of transcendental non-linear equations due to σ(z). There is no closed-form algebraic matrix inverse like OLS β = (XᵀX)⁻¹Xᵀy.

Instead, we use iterative optimization algorithms:

Say this out loud

"Logistic regression models the log-odds of a positive outcome as a linear combination of features. We pass the linear logit through a Sigmoid activation to get calibrated probabilities. Loss is optimized via Binary Cross-Entropy, which is strictly convex, guaranteeing convergence to a global minimum via gradient descent. Weight w_i represents the additive change in log-odds (or multiplicative change in odds via e^w_i) per unit feature change."

Follow-ups to expect

Check yourself

Question 1 of 3

If feature weight w_i = 0.693 in a logistic regression model, increasing feature x_i by 1 unit increases the odds of positive class by a factor of approximately

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min