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
Machine Learning Interview #15 · Tutorial

Logistic RegressionWhy Cross-Entropy, Not Squared Error

Three reasons: the likelihood, the push, and one bowl

A tutorial companion to the video Machine Learning Interview #15: Logistic Regression: Why Cross-Entropy, Not Squared Error PDF

01Introduction

1.1The question

Least squares fits numbers, such as the disease progression of our linear regression episode (Episode 14). But many answers are just yes or no: is this tumor malignant, will this customer leave, is this email spam? For those, the model should give a probability, and logistic regression is the classic way to get one. It trains with a different loss, cross-entropy, and interviewers like to ask why: “Why does logistic regression use cross-entropy instead of mean squared error?” A good answer has three parts, and each one is a result you can derive on a whiteboard.

1.2Prerequisites and notation

Prerequisites. Calculus (the chain rule) and linear algebra (matrix products, transpose). Maximum likelihood (Episode 5), cross-entropy (Episode 8) and gradient descent (Episode 9) help but are explained where they are used.

Notation. We have n examples. Example i has a label y_i\in\{0,1\} (1 = malignant) and an input vector x_i=(1,x_{i1},\dots,x_{id})^\top, whose leading 1 carries the intercept; the matrix X has the x_i^\top as its rows. The weights are w=(w_0,w_1,\dots,w_d)^\top, the score (or logit) is z_i=w^\top x_i, and the predicted probability is p_i=\sigma(z_i), the probability the model gives to y_i=1. Vectors p and y collect the p_i and y_i. We write \log for the natural logarithm.

1.3A short history

The logistic curve was first written down by Pierre-François Verhulst in 1838 to describe population growth, and he named it logistique in 1845 (Cramer, 2004). A century later it reached statistics through bio-assay: Berkson (1944) used it to model the chance that an animal responds to a dose, and coined the word logit for the log-odds. Cox (1958) turned it into regression for binary outcomes with several inputs, and Nelder and Wedderburn (1972) placed it in the family of generalized linear models, where the logit is the natural (canonical) link for a yes/no outcome. Textbook treatments are Murphy (2022, Ch. 10) and Bishop (2006, §4.3).

All examples use one real data set, the breast tumors of Street et al. (1993): a thin needle draws cells from each of 569 breast lumps (212 malignant, 357 benign), and a computer outlines the cell nuclei in a digitized image and measures them. We use one input throughout, the mean nucleus radius. All numbers in this tutorial were computed for this tutorial, with the same data as the companion video.