A · One neuron
Logistic Regression
A linear model with a sigmoid on top: it outputs a probability, and is trained by minimising cross-entropy with gradient descent.
Try it: A5 · Logistic Regression
Intuition
Logistic regression answers yes/no questions with a probability. Will this email be spam: 0.93. Will this patient be readmitted: 0.12. It is the perceptron with its hard step replaced by a sigmoid: compute a weighted sum of the inputs, then squash it into a number between 0 and 1.
The decision boundary is still a straight line, where the weighted sum is zero and the probability is exactly 0.5. But now the model also says how sure it is: points near the line get probabilities near 0.5, points far from it get probabilities near 0 or 1. And the weights' size matters: larger weights make the probability change faster as you cross the line, a more confident model.
The math
The model
\[ p = P(y = 1 \mid \mathbf{x}) = \sigma(\mathbf{w}^\top \mathbf{x} + b) = \frac{1}{1 + e^{-(\mathbf{w}^\top\mathbf{x} + b)}} \]Undo the sigmoid and you see what the model assumes: the log-odds are linear in the inputs.
\[ \log\frac{p}{1 - p} = \mathbf{w}^\top\mathbf{x} + b \]So each weight has a clear reading: increasing \(x_j\) by one multiplies the odds of class 1 by \(e^{w_j}\). A weight of 0.7 roughly doubles the odds per unit.
The loss: cross-entropy
Treat each label as a coin flip with probability \(p_i\), and maximise the probability of all the observed labels. Taking the negative log gives the binary cross-entropy (see Loss functions):
\[ L(\mathbf{w}, b) = -\frac{1}{n}\sum_{i=1}^{n} \Big( y_i \log p_i + (1 - y_i)\log(1 - p_i) \Big) \]The gradient: surprisingly clean
Combining the derivative of the log, \(1/p\), with the derivative of the sigmoid, \(p(1 - p)\), almost everything cancels:
\[ \frac{\partial L}{\partial \mathbf{w}} = \frac{1}{n}\sum_i (p_i - y_i)\,\mathbf{x}_i, \qquad \frac{\partial L}{\partial b} = \frac{1}{n}\sum_i (p_i - y_i) \]Each example pushes the weights in proportion to its error \(p_i - y_i\): the same shape as the perceptron learning rule and as linear regression's gradient, but graded instead of all-or-nothing. There is no formula for where this gradient is zero, so gradient descent finds it. The good news: the cross-entropy loss of logistic regression is convex, a single bowl with no false valleys, so gradient descent reliably reaches the best weights.
Worked example. One input, \(w = 2\), \(b = -1\). For \(x = 1\): \(z = 1\), \(p = \sigma(1) \approx 0.73\). If the label is 1, the loss is \(-\log 0.73 \approx 0.31\) and the gradient for \(w\) is \((0.73 - 1)\cdot 1 = -0.27\), so gradient descent increases \(w\). If the label is 0, the loss is \(-\log 0.27 \approx 1.31\), the gradient is \(+0.73\), and \(w\) decreases, more strongly, because the model was more wrong.
Variants & extensions
- Regularization. Practical logistic regression almost always adds an L2 penalty \(\tfrac{\lambda}{2}\lVert\mathbf{w}\rVert^2\). Besides preventing overfitting, it fixes a real problem described below.
- Many classes. Replace the sigmoid by a softmax over one score per class: multinomial logistic regression.
- Curved boundaries. The boundary is linear in the inputs you give it; give it \(x_1^2, x_1x_2, \dots\) and it curves. That is feature engineering, and a neural network is a logistic regression whose features are learned.
When it fails
- Perfectly separable data. If a line separates the classes with no errors, the loss keeps falling as the weights grow, forever: the sigmoid becomes a step and the weights run off to infinity. Regularization or early stopping keeps them finite.
- Curved classes. A disc of one class inside a ring of the other has no straight boundary, and the model can only shrug: about 0.5 everywhere.
- Calibration and imbalance. Probabilities are meaningful only if training data resemble the data you predict on. With 1% positives, a threshold of 0.5 may never fire; pick the threshold from the costs of each kind of mistake.
History
The logistic curve comes from Verhulst's population models (1838). Joseph Berkson named and promoted the "logit" for biological assays in 1944, and David Cox's 1958 paper turned logistic regression into the general tool of statistics. It remains one of the most used models in medicine, credit scoring and online advertising, because its weights are easy to read and its probabilities are usually well calibrated.
Connections
Builds on
- The Sigmoid FunctionLogistic regression is a weighted sum passed through a sigmoid, read as the probability of class 1.
- Loss FunctionsIts loss is cross-entropy, the loss that follows from treating the output as a probability.
- Gradient DescentThere is no formula for its best weights, so gradient descent finds them.
Leads to
- Feature EngineeringLogistic regression draws a straight boundary; new features let that same straight boundary curve in the original space.
- Softmax & Multi-class ClassificationSoftmax generalises logistic regression from two classes to many; with two classes it is exactly the sigmoid.
Further reading
- Logistic Regression (MLU-Explain)An interactive tour of the sigmoid, the decision boundary and the log loss.
- Logistic regression: Loss and regularization (Google Machine Learning Crash Course)Why logistic regression uses log loss instead of squared error.
- Logistic Regression (StatQuest)A friendly video on what the S-shaped curve means and how it's read as a probability.