Logistic Regression And Linear Models
Asked of: Machine Learning Engineer
Last updated

What's being tested
Interviewers are probing whether you understand linear models as trainable probabilistic systems, not just as `sklearn.linear_model.LogisticRegression` calls. For a Machine Learning Engineer, this matters because these models are still common in production ranking, ads, fraud, demand forecasting, and safety systems where latency, interpretability, calibration, and online/offline parity matter. Expect to derive losses and gradients, explain assumptions, choose regularization, diagnose failure modes like miscalibration or class imbalance, and connect model math to deployment behavior. A strong answer shows both mathematical fluency and production judgment: how the model is trained, evaluated, served, monitored, and fixed when data shifts.
Core knowledge
-
Linear regression models a continuous target as and usually minimizes mean squared error: Its gradient is , which is the basis for batch, mini-batch, or stochastic gradient descent.
-
Logistic regression models a Bernoulli probability using the sigmoid link: The linear score is unconstrained, while the sigmoid maps it to , making it suitable for binary classification and probability scoring.
-
The logit link is . This means each coefficient changes the log-odds additively; is the multiplicative odds ratio for a one-unit increase in feature , assuming other features are fixed.
-
Logistic regression is trained by maximizing the Bernoulli likelihood, equivalently minimizing binary cross-entropy: The key gradient is simple: , often plus regularization terms.
-
L2 regularization adds and shrinks coefficients smoothly, improving generalization under correlated or noisy features. L1 regularization adds and can produce sparse weights, useful for high-dimensional sparse features such as hashed categorical IDs.
-
For logistic regression with L2, the gradient becomes often excluding the bias term from regularization. Interviewers commonly check whether you regularize
`b`; the usual answer is “no” unless there is a specific prior. -
Gradient descent choices matter operationally. Full-batch methods like L-BFGS are stable for smaller dense datasets; mini-batch SGD scales better for millions to billions of examples and sparse features. In production training pipelines, learning-rate schedules, shuffling, feature scaling, and checkpointing often matter more than the exact optimizer name.
-
Feature scaling is critical for gradient-based training. Without standardization or normalization, large-scale features dominate updates, convergence slows, and regularization penalizes coefficients unevenly. Sparse binary/categorical features may not need standardization, but dense numerical features usually should.
-
Calibration means predicted probabilities match empirical frequencies: among examples scored near , roughly 80% should be positive. Logistic regression is often well-calibrated under correct specification, but imbalance, regularization, sampling bias, or distribution shift can require Platt scaling, isotonic regression, or post-training calibration on a holdout set.
-
Evaluation metrics depend on serving use case.
`ROC-AUC`measures ranking over thresholds and can look strong under class imbalance;`PR-AUC`is more informative when positives are rare.`LogLoss`evaluates probability quality, while calibration curves and expected calibration error catch probability miscalibration missed by AUC. -
Class imbalance can be handled with class weights, downsampling, threshold tuning, or loss reweighting, but each changes interpretation. If you train on sampled negatives, raw model probabilities may be biased; you may need prior correction or calibration against the true production distribution.
-
Production failure modes include feature drift, label delay, training-serving skew, exploding logits from unbounded numerical inputs, and silent changes in feature distributions. MLEs should monitor
`LogLoss`,`AUC`, calibration, prediction distribution, feature null rates, and online business guardrail metrics without owning raw ingestion infrastructure.
Worked example
For Explain Logistic Regression Fundamentals, a strong candidate starts by clarifying the setting: “Are we discussing binary classification, calibrated probability estimation, or thresholded decisions?” Then they state assumptions: labels are Bernoulli, features are fixed inputs, and the model uses a linear log-odds function passed through a sigmoid. The answer should be organized around four pillars: the probabilistic model, the loss derived from maximum likelihood, the optimization gradient, and practical evaluation/calibration.
The candidate would write and derive cross-entropy from the Bernoulli likelihood rather than presenting it as a memorized loss. They should mention the gradient , because it explains why the update pushes probabilities down for false positives and up for false negatives. Next, they should discuss regularization: L2 for stability and lower variance, L1 for sparsity, and the bias term usually excluded. A concrete tradeoff to flag is that optimizing `LogLoss` improves probability quality, while selecting a threshold for precision/recall is a separate deployment decision. They can close by saying: “If I had more time, I’d validate calibration on a holdout set, compare `ROC-AUC` and `PR-AUC`, and check for training-serving skew or drift before deployment.”
A second angle
For Implement SGD for linear regression and derive gradients, the same foundation shifts from probabilistic classification to continuous regression and optimization mechanics. The candidate should derive MSE gradients, then show how mini-batch updates approximate the full gradient: . The interviewer is less focused on sigmoid/log-odds and more on whether the candidate understands update loops, batch size, convergence, learning-rate sensitivity, and vectorized implementation. The production angle is similar: feature scaling, validation loss monitoring, checkpointing, and reproducibility are essential whether the model is linear or logistic. A good answer also notes numerical stability and stopping criteria rather than only writing the formula.
Common pitfalls
Pitfall: Treating logistic regression as “linear regression plus sigmoid.”
That answer is tempting but incomplete. What lands better is explaining that the sigmoid comes from modeling the log-odds linearly and fitting via Bernoulli maximum likelihood, which yields cross-entropy rather than MSE as the natural objective.
Pitfall: Confusing ranking quality with probability quality.
Saying “the model has high `ROC-AUC`, so probabilities are good” is analytically wrong. `ROC-AUC` can be high while calibration is poor; for probability-serving systems, mention `LogLoss`, calibration curves, `ECE`, and post-hoc calibration methods.
Pitfall: Giving only math and ignoring deployment constraints.
A derivation-only answer can sound academic for an MLE loop. Add production checks: feature scaling, sparse feature handling, train/serve parity, label leakage, drift monitoring, latency constraints, and whether thresholds or calibration are recomputed after retraining.
Connections
Interviewers may pivot from here to bias-variance tradeoff, regularization paths, online learning, feature engineering, or ranking metrics such as `NDCG`, `ROC-AUC`, and `PR-AUC`. They may also compare linear models with `XGBoost`, random forests, or neural networks, asking when the simpler model is preferable for interpretability, speed, or calibration.
Further reading
-
The Elements of Statistical Learning — strong treatment of linear models, logistic regression, regularization, and model evaluation.
-
[Pattern Recognition and Machine Learning, Bishop] — clear probabilistic view of generalized linear models and maximum likelihood.
-
Probabilistic Machine Learning, Murphy — modern reference for probabilistic modeling, optimization, and calibration concepts.
Featured in interview prep guides
Practice questions
- Explain Core ML Interview ConceptsAmazon · Machine Learning Engineer · Technical Screen · hard
- Implement SGD for linear regression and derive gradientsAmazon · Machine Learning Engineer · Technical Screen · medium
- Explain Logistic Regression FundamentalsAmazon · Machine Learning Engineer · Onsite · hard
- Explain ML statistics and model design conceptsAmazon · Machine Learning Engineer · Technical Screen · hard
- Explain core ML fundamentalsAmazon · Machine Learning Engineer · Onsite · medium
Related concepts
- Logistic Regression, Regularization, And Imbalanced ClassificationMachine Learning
- Linear Regression And OLS InferenceStatistics & Math
- Supervised ML, Imbalance, Overfitting, And OptimizationMachine Learning
- ML Fundamentals: Backprop, Attention, And RLMachine Learning
- Supervised ML Fundamentals, Evaluation And Feature EngineeringMachine Learning
- Statistical Inference, Regression, And ProbabilityStatistics & Math