- A classifier predicts a class. Logistic regression first predicts a probability.
- The sigmoid turns any score into a number between 0 and 1.
- The boundary is where the probability equals 0.5.
This lesson follows Linear Regression & Loss. For probability and likelihood, review Probability & Statistics.
1. A yes/no question
Regression predicts a number: a price, a temperature, a time. Classification predicts a class: spam or not, pass or fail, cat or dog. A linear regression line is a poor classifier because its output can be any real number, including values far outside the two classes.
We still want a linear score. Let x be the input and w, b the parameters:
The score z is not a probability. A score of 3 does not mean 3; it means “further toward class 1 than a score of 1.” The sigmoid converts that score into a probability.
2. Score to probability
The logistic function, or sigmoid, squashes the whole number line into (0, 1):
Large positive scores approach 1, large negative scores approach 0, and a score of 0 gives 0.5. The curve has no sharp jump; every score gets a probability.
Worked example: sigmoid values
| score z | probability p = σ(z) |
|---|---|
| -2 | 0.1192 |
| -1 | 0.2689 |
| 0 | 0.5000 |
| 1 | 0.7311 |
| 2 | 0.8808 |
At z = 0 the model is exactly on the fence. Positive scores lean toward class 1; negative scores lean toward class 0.
To make a hard class decision, compare p with a threshold. The default is 0.5:
The threshold is a decision rule, not part of the model. If a false positive is much more expensive than a false negative, you may choose a threshold above 0.5.
3. Loss for a probability
Squared error is not the usual loss for a probability. If the label is 1 and the model says 0.01, the miss is enormous in log terms. Logistic regression uses cross-entropy, also called log loss:
If y = 1, the second term disappears and the loss is −ln p. If y = 0, the first term disappears and the loss is −ln(1−p). A confident correct answer is cheap; a confident wrong answer is expensive.
| p | label y | cross-entropy |
|---|---|---|
| 0.8 | 1 | 0.223 |
| 0.2 | 1 | 1.609 |
| 0.8 | 0 | 1.609 |
| 0.2 | 0 | 0.223 |
4. The decision boundary
The boundary is the set of inputs where the model is exactly on the fence:
With two inputs, the boundary is a line. Take the parameters w = (1, 1) and b = −3. The boundary is:
The point (2, 2) gives z = 1 and p ≈ 0.731, so the model leans positive. The point (0, 0) gives z = −3 and p ≈ 0.047, so it leans negative:
The line does not have to pass through the origin, and it does not have to be at 45 degrees. The weights set its angle; the bias shifts it.
5. Training
The gradient of cross-entropy has a clean shape. For one example, the error is p − y:
If the label is 1 and the model says 0.8, the error is −0.2. With x = (2, 2), the weight gradient is (−0.4, −0.4) and the bias gradient is −0.2. Gradient descent subtracts a multiple of that gradient, moving the score up toward the correct label.
The average over the training set gives the full-batch gradient. The Gradient Descent lesson covers the step size and the path; the decision-boundary playground lets you train this model directly.
6. Where it struggles
Logistic regression draws a straight boundary. If the classes form a ring or an XOR pattern, no line can separate them; more training will not bend it. The next lesson adds features that make a curved boundary possible.
Class imbalance is another trap. If 99% of examples are class 0, a model that always says 0 gets 99% accuracy while missing every positive case. Accuracy hides the failure; look at per-class errors and the cost of each mistake.
The model can also be overconfident. Regularization and more data can help, but a probability is still a model output, not a guarantee. Check calibration before using probabilities for decisions with asymmetric costs.
7. Going deeper
Why does the sigmoid appear? Solve p = σ(z) for z. The inverse is the log-odds:
A linear model in z is a linear model in log-odds. The odds p/(1−p) multiply by e for every one-unit increase in z. That is a useful interpretation, but it does not make the probability a physical law.
Cross-entropy comes from maximizing the likelihood of the observed labels. For independent examples, the likelihood is a product of p for positive labels and 1−p for negative labels. Taking the negative log turns the product into the sum above. The loss is convex for a linear score, so gradient descent does not get trapped in a local minimum here.
A regularizer adds a penalty on large weights. It can keep the boundary from becoming too confident on separable data, where the unregularized optimum would push the weights toward infinity.
8. Practice
What is σ(2), and what class does it predict at a 0.5 threshold?
The label is 1. Which is worse under cross-entropy: a prediction of 0.2 or a prediction of 0.8?
A model with w = (1, 1) and b = −3 sees the point (1, 1). What is z, what is p, and which class does it predict?
9. Resources
The MIT chapter derives the logistic classifier from likelihood and covers the gradient. The Crash Course connects the sigmoid and threshold to a working classifier. Both links were opened and checked for this lesson.
Continue the track
Open the playground for this topic
Decision Boundary Playground →Add and drag points, switch between linear, quadratic, and neural models, and watch the boundary and both accuracy scores respond.
The mathematics behind this topic: Probability & statistics.