Contents
06Interview follow-ups
6.1What if the classes separate perfectly?
Suppose some line (or plane) puts every malignant tumor on one side and every benign one on the other. Take weights w whose boundary does this, and multiply them by a number c>1. Every score moves away from zero, so every p_i moves closer to its label y_i and every term of (5) gets smaller. The loss keeps falling as c grows, approaching 0 but never reaching it: there is no minimum, and the maximum likelihood estimate does not exist (Albert and Anderson, 1984). Gradient descent then makes the weights grow forever, and the fitted probabilities jump from 0 to 1 at the boundary (Bishop, 2006, §4.3.2). This is the one case where Theorem 1 promises a bowl without a bottom.
The fix is L_2 regularization (Episode 4): minimize J(w)+\lambda\|w\|^2, usually leaving the intercept out of the penalty. The penalty is the negative log of a Gaussian prior on the weights, so this is the maximum a posteriori estimate of Episode 5 (Murphy, 2022, §10.2.7). It adds 2\lambda I to the Hessian, making the loss strictly convex with a unique, finite minimum.
Worked example 4 (Iris flowers that never overlap). In Fisher’s iris data, the longest setosa petal is 1.9 cm and the shortest versicolor petal is 3.0 cm, so petal length separates the two species perfectly. Fit logistic regression by gradient descent, and the size of the weights \|w\| is 2.2 after 10 steps, 8.1 after 1{,}000 and 17.3 after 100{,}000, still growing (Figure 5a). With L_2 regularization it settles at 3.5.
6.2What about more than two classes?
Give each class k its own weights and score z_k=w_k^\top x, and turn the scores into probabilities with the softmax,
which makes them positive and sum to one (Murphy, 2022, §10.3). The loss is still cross-entropy, -\log p_{\text{true class}}, and the gradient for class k is again prediction minus truth, \sum_i(p_{ik}-y_{ik})\,x_i. With two classes, p_1=e^{z_1}/(e^{z_1}+e^{z_2})=\sigma(z_1-z_2): softmax is the sigmoid of the difference of the scores, and binary logistic regression is the special case. For example, a softmax model of the three iris species on petal length and width gives a versicolor flower with petals of 4.6\times1.5 cm the probabilities 1\%, 71\% and 28\% for setosa, versicolor and virginica.
6.3Is logistic regression a linear classifier?
Yes. With equal costs for the two kinds of error, we predict malignant when p>\frac12, that is when the score is positive, so the boundary is where
a point for one input (radius 14.75), a straight line for two (Figure 5b), a flat plane in general (Murphy, 2022, §10.2.1). For curved boundaries, add features such as x_1^2, x_2^2 and x_1x_2: the model stays linear in the weights, so everything above still applies, but the boundary curves in the original inputs (Murphy, 2022, §10.2.2). When the two kinds of error cost differently, move the threshold away from \frac12, as in Episode 13.