MoonSay /Tutorials/Machine Learning Interview #15
Contents
  1. 01Introduction
  2. 02From a line to a probability
  3. 03Maximum likelihood
  4. 04The push
  5. 05One bowl
  6. 06Interview follow-ups
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References
Logistic Regression/Chapter 08 of 09

08Interview questions and model answers

9 questions · click a question to see a brief answer
Q1Why does logistic regression use cross-entropy instead of mean squared error?
Brief answer

Three reasons. Cross-entropy is the negative log-likelihood of the model (each label a coin flip with probability p=\sigma(w^\top x)), so minimizing it is maximum likelihood. Its gradient on the score is p-y, so a confident mistake gets the strongest push; squared error multiplies that by 2p(1-p), which vanishes when the model is confidently wrong. And cross-entropy is convex (Hessian X^\top SX\succeq0), one bowl, while squared error on a sigmoid is not convex, with plateaus and possibly many local minima.

Q2Why not fit linear regression to the 0/1 labels?
Brief answer

The line leaves [0,1] for inputs far from the middle, so its outputs are not probabilities, and examples with extreme inputs, the easiest to classify, pull it the most. Logistic regression keeps the linear score and squashes it with the sigmoid.

Q3How do you interpret a coefficient?
Brief answer

The log-odds \log\frac{p}{1-p} is linear in the inputs, so one more unit of x_j adds w_j to the log-odds and multiplies the odds by e^{w_j}, holding the other inputs fixed. In our data, one more unit of nucleus radius multiplies the odds of malignancy by 2.8.

Q4Derive the gradient of the logistic loss.
Brief answer

\partial J_i/\partial p=-\frac{y}{p}+\frac{1-y}{1-p}=\frac{p-y}{p(1-p)} and \partial p/\partial z=p(1-p), so \partial J_i/\partial z=p-y; with z=w^\top x, \nabla_wJ=\sum_i(p_i-y_i)x_i=X^\top(p-y), the same form as the least-squares gradient.

Q5Is the logistic loss convex? Prove it.
Brief answer

The Hessian is \sum_ip_i(1-p_i)\,x_ix_i^\top=X^\top SX, and v^\top X^\top SXv=\sum_ip_i(1-p_i)(x_i^\top v)^2\ge0. Strictly convex when the columns of X are linearly independent.

Q6Is there a closed-form solution? How is the model fitted?
Brief answer

No closed form: setting the gradient to zero gives equations that are nonlinear in w. Fit by gradient descent (or its stochastic version) or by Newton’s method, w\leftarrow w-(X^\top SX)^{-1}X^\top(p-y), also called iteratively reweighted least squares.

Q7What happens when the classes are perfectly separable?
Brief answer

Scaling up any separating w lowers the loss forever, so the maximum likelihood estimate does not exist and the weights diverge. Add L_2 regularization (a Gaussian prior, MAP estimation), or stop early.

Q8How do you extend logistic regression to more than two classes?
Brief answer

One score per class, z_k=w_k^\top x, turned into probabilities by the softmax e^{z_k}/\sum_je^{z_j}; the loss is still cross-entropy, -\log p_{\text{true class}}, and the gradient is still prediction minus truth. Two classes give back the sigmoid of z_1-z_2.

Q9Is logistic regression a linear classifier? Can it learn curved boundaries?
Brief answer

The boundary p=\frac12 is w^\top x=0, a hyperplane, so it is linear in the inputs. Curved boundaries come from transformed features such as x^2 or x_1x_2, with the model still linear in the weights.