M2-006M2: Classical Machine LearningLogistic Regression & GLMEasy
⚡Flashcard Drill➔Mastery:

Formulate Logistic Regression, Sigmoid Activation, and Binary Cross-Entropy Loss

📐 Mathematical Definition
p=σ(w⊤x),L=−∑[ylog⁡p+(1−y)log⁡(1−p)]p=\sigma(w^\top x),\qquad \mathcal L=-\sum\big[y\log p+(1-y)\log(1-p)\big]
⚡ One-Sentence Core Answer
Logistic regression models class probability via the Sigmoid function: P(y=1∣x)=σ(wTx)=11+e−wTxP(y=1\mid x) = \sigma(w^T x) = \frac{1}{1 + e^{-w^T x}}, and is optimized using negative log-likelihood (Binary Cross-Entropy Loss).

📌 Key Takeaways

  • •
    Hypothesis: hw(x)=σ(wTx)=11+e−wTx∈(0,1)h_w(x) = \sigma(w^T x) = \frac{1}{1 + e^{-w^T x}} \in (0, 1).
  • •
    Sigmoid derivative: σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)(1 - \sigma(z)), enabling elegant gradient cancellation.
  • •
    Loss function: L(w)=−1N∑i=1N[yilog⁡hw(xi)+(1−yi)log⁡(1−hw(xi))]\mathcal{L}(w) = -\frac{1}{N}\sum_{i=1}^N [y_i \log h_w(x_i) + (1-y_i)\log(1 - h_w(x_i))].
  • •
    Gradient: ∇wL=1N∑i=1N(hw(xi)−yi)xi\nabla_w \mathcal{L} = \frac{1}{N}\sum_{i=1}^N (h_w(x_i) - y_i) x_i, mathematically identical in form to linear regression.

📐 Mathematical Derivations

Likelihood derivation under Bernoulli assumption: Let P(yi=1∣xi)=piP(y_i=1\mid x_i) = p_i and P(yi=0∣xi)=1−piP(y_i=0\mid x_i) = 1 - p_i. The likelihood for NN independent observations is L(w)=∏i=1Npiyi(1−pi)1−yiL(w) = \prod_{i=1}^N p_i^{y_i} (1 - p_i)^{1 - y_i}. Taking the negative natural log and dividing by NN: L(w)=−1N∑i=1N[yilog⁡pi+(1−yi)log⁡(1−pi)]\mathcal{L}(w) = -\frac{1}{N}\sum_{i=1}^N [y_i \log p_i + (1-y_i)\log(1 - p_i)]. To compute the gradient with respect to wjw_j: By the chain rule, ∂L∂wj=∂L∂pi∂pi∂zi∂zi∂wj\frac{\partial \mathcal{L}}{\partial w_j} = \frac{\partial \mathcal{L}}{\partial p_i} \frac{\partial p_i}{\partial z_i} \frac{\partial z_i}{\partial w_j}. Note that ∂Li∂pi=−yipi+1−yi1−pi=pi−yipi(1−pi)\frac{\partial \mathcal{L}_i}{\partial p_i} = -\frac{y_i}{p_i} + \frac{1-y_i}{1-p_i} = \frac{p_i - y_i}{p_i(1-p_i)}, and ∂pi∂zi=σ′(zi)=pi(1−pi)\frac{\partial p_i}{\partial z_i} = \sigma'(z_i) = p_i(1-p_i), and ∂zi∂wj=xij\frac{\partial z_i}{\partial w_j} = x_{ij}. Multiplying together: ∂Li∂wj=pi−yipi(1−pi)⋅pi(1−pi)⋅xij=(pi−yi)xij\frac{\partial \mathcal{L}_i}{\partial w_j} = \frac{p_i - y_i}{p_i(1-p_i)} \cdot p_i(1-p_i) \cdot x_{ij} = (p_i - y_i) x_{ij}. The non-linear terms cancel out completely, yielding the clean gradient ∇wL=1NXT(y^−y)\nabla_w \mathcal{L} = \frac{1}{N} X^T (\hat{y} - y).

🏭 Production Trade-offs

No closed-form solution exists because y^=σ(Xw)\hat{y} = \sigma(Xw) is non-linear in ww. Optimized using: (1) Newton-Raphson / IRLS (Iteratively Reweighted Least Squares): w(t+1)=w(t)−(XTWX)−1XT(p−y)w^{(t+1)} = w^{(t)} - (X^T W X)^{-1} X^T (p - y) with weight matrix Wii=pi(1−pi)W_{ii} = p_i(1-p_i), achieving quadratic convergence for small-to-medium dd. (2) L-BFGS or SGD: Used in industrial production (e.g. Ad click prediction in FTRL-Proximal) for high-dimensional sparse features.
⚠️ Common Interview Pitfalls
  • ✕
    Using Mean Squared Error (MSE) loss with Sigmoid activation (produces a non-convex loss surface with flat plateaus where gradients vanish when predictions are confidently wrong).
  • ✕
    Confusing probabilities p∈[0,1]p \in [0, 1] with binary classifications y^∈{0,1}\hat{y} \in \{0, 1\} (the 0.5 decision threshold can be tuned for precision/recall tradeoffs).
🎯 Interviewer Follow-up Inquiries
  • ?
    Why does combining Sigmoid with Mean Squared Error (MSE) cause vanishing gradients, whereas Cross-Entropy does not?
  • ?
    How does Iteratively Reweighted Least Squares (IRLS) derive from the Newton-Raphson update in Logistic Regression?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM2-005: Derive the Least Squares Solution for Linear Regression and Explain Its Orthogonal Projection Geometry📋Back to BankNext →M2-007: Explain Odds, Log-Odds (Logit), and the Exact Multiplicative Interpretation of Logistic Regression Coefficients
中文速览与面试要点

写出逻辑回归的模型形式与损失函数。

sigmoid 输出概率,用交叉熵(对数似然)训练;是 GLM 中 Bernoulli + logit 链接。

核心要点

  • 模型与损失:逻辑回归假设 log(p/(1−p))=wᵀx(logit 是线性的),反解得 p=σ(wᵀx)=1/(1+e^−wᵀx)。损失来自 极大似然 :对 N 个样本,负对数似然为 −Σ[yᵢlog pᵢ+(1−yᵢ)log(1−pᵢ)],即二元交叉熵。关键性质是 梯度极其简洁 :∇_w L=Σᵢ(pᵢ−yᵢ)xᵢ=Xᵀ(p−y)——这与线性回归的梯度 Xᵀ(Xw−y) 形式一致,只是把 Xw 换成 σ(Xw)。这个简洁性来自 sigmoid 导数 σ'=σ(1−σ) 与交叉熵的巧妙配合(链式法则中的分母被约掉),这也是为什么 不用 MSE + sigmoid :后者的梯度含 σ' 因子,在饱和区趋近 0,导致梯度消失。
  • 两个实践要点:① 凸性 ——逻辑回归的负对数似然是 凸函数 (海森 Xᵀdiag(p(1−p))X 半正定),故任何局部最优即全局最优,可用牛顿法/IRLS 快速收敛(通常 5–10 次迭代);② 完全分离问题 ——若数据线性可分,系数会趋向无穷(似然单调上升但永不达到最大值),表现为迭代不收敛、系数爆炸、标准误巨大;解法是加 L2 正则(等价于高斯先验的 MAP)或使用 Firth 惩罚似然。③ 系数解释 ——e^βⱼ 是优势比(odds ratio),表示特征每增 1 单位优势的倍数,这是业务方偏好逻辑回归的主要原因;但要注意这依赖'其他特征不变'的假设,共线时解释不可靠。
  • 线性决策边界(特征空间)
  • 没有闭式解,用梯度/牛顿/IRLS

常见陷阱

  • 用 MSE 训练逻辑回归(梯度在饱和区消失)
  • 对线性可分数据不做正则(系数发散)