Logistic Regression and Probabilities
Turn a weighted sum into a probability with the sigmoid, read log-odds, choose a decision threshold, and train with log loss instead of MSE.
Welcome back!
Step 1 of 12Learn
From lines to probabilities
A bank wants to predict whether a loan applicant will default: yes or no. You could try linear regression with y = 1 for default and 0 otherwise, but its predictions would run below 0 and above 1, which make no sense as probabilities.
Logistic regression fixes this. It computes the same weighted sum as linear regression,
z = w₁x₁ + w₂x₂ + … + b
and then passes z through the sigmoid (logistic) function:
p = σ(z) = 1 ÷ (1 + e−z)
The sigmoid squeezes any number into the range 0 to 1:
- z = 0 gives p = 0.5.
- Large positive z gives p close to 1.
- Large negative z gives p close to 0.
So z is a score and p is the predicted probability of the positive class.
Example: a model for default risk has z = 0.8 × (debt-to-income ratio, in tenths) − 0.02 × (credit score − 600) − 1. An applicant with debt-to-income of 0.5 (so 5 tenths) and a credit score of 650 gets z = 0.8 × 5 − 0.02 × 50 − 1 = 4 − 1 − 1 = 2, and p = 1 ÷ (1 + e−2) ≈ 0.88. The model estimates an 88% chance of default.
Despite its name, logistic regression is a classification model. It remains one of the most widely used models in medicine, credit scoring and the social sciences because it is fast, well understood, and produces probabilities with interpretable weights.
Step 2 of 12Practice · Work it out
A logistic regression model computes z = 0 for an applicant. What probability of the positive class does it predict?
Step 3 of 12Learn
Odds, log-odds and what weights mean
Logistic regression weights have a precise interpretation through odds.
If the probability of an event is p, the odds are p ÷ (1 − p). A probability of 0.8 means odds of 0.8 ÷ 0.2 = 4, or "4 to 1." A probability of 0.5 means odds of 1.
It turns out that the score z is exactly the log-odds:
z = ln(p ÷ (1 − p))
So each weight tells you how much the log-odds change when its feature increases by one unit. Exponentiating a weight gives an odds ratio: ew is the factor by which the odds are multiplied for each one-unit increase.
Example: in a model predicting whether a customer buys after visiting a website, the weight for "number of previous purchases" is 0.7. Then e0.7 ≈ 2.0, so each previous purchase roughly doubles the odds of buying, holding other features fixed. A negative weight, such as −0.4 for "seconds of page load time," gives e−0.4 ≈ 0.67: each extra second multiplies the odds by about two-thirds.
Note that doubling the odds is not the same as doubling the probability. Going from odds of 1 (p = 0.5) to odds of 2 raises p to about 0.67, not 1.0.
The decision boundary. To turn probabilities into class predictions, you pick a threshold, by default 0.5. Since σ(z) ≥ 0.5 exactly when z ≥ 0, the boundary is the set of points where the weighted sum equals zero. For two features, that's a straight line; logistic regression is a linear classifier. To separate classes with curved boundaries, you need transformed features or a different model.
Step 4 of 12Practice · Chart
Probability vs. odds
Odds = p ÷ (1 − p). They grow slowly at first, then explode as p approaches 1.
Tap the probability where the odds are exactly 1 (even odds).
Tap the point that answers the question, then press Check.
| Item | Odds |
|---|---|
| p 0.1 | 0.11 |
| 0.25 | 0.33 |
| 0.5 | 1 |
| 0.75 | 3 |
| 0.8 | 4 |
| 0.9 | 9 |
Step 5 of 12Practice · Code challenge
A one-feature logistic regression predicts class 1 when its probability is at least 0.5. Write predictClass(w, b, x) that returns 1 or 0. You do not need to compute the sigmoid: use the fact that σ(z) ≥ 0.5 exactly when z = w·x + b ≥ 0.
predictClass(2, -4, 3)predictClass(2, -4, 1)predictClass(-1, 5, 5)predictClass(0.5, -1, 1)
Step 6 of 12Learn
Why not mean squared error? Log loss
Logistic regression is trained with a loss called log loss (binary cross-entropy):
Loss = −[ y × ln(p) + (1 − y) × ln(1 − p) ]
averaged over all examples, where y is the true label (0 or 1) and p is the predicted probability of class 1.
It looks intimidating, but only one term matters at a time:
- If the true label is 1, the loss is −ln(p). Predicting p = 0.9 costs −ln(0.9) ≈ 0.105. Predicting p = 0.1 costs −ln(0.1) ≈ 2.30.
- If the true label is 0, the loss is −ln(1 − p).
The key property: confident wrong answers are punished severely. As p for a true positive approaches 0, the loss heads to infinity. That pushes the model to be well calibrated and never absolutely certain without reason.
Why not MSE? Combined with the sigmoid, MSE produces a loss surface with flat regions where gradients vanish, so training can stall. Log loss with the sigmoid gives a convex loss with useful gradients everywhere, and it corresponds to the statistical principle of maximum likelihood.
Multiple classes. For more than two classes, such as classifying an image as cat, dog or bird, the sigmoid is replaced by the softmax function, which turns a vector of scores into probabilities that sum to 1, and log loss generalizes to categorical cross-entropy. This same combination sits at the output of most neural network classifiers, including language models choosing the next token.
Step 7 of 12Practice · Estimate
A model predicts p = 0.2 for an example whose true label is 1. Using log loss = −ln(p), estimate the loss for this example.
Step 8 of 12Chart
Log loss when the true label is 1
Loss = −ln(p). Small for confident right answers, huge for confident wrong ones.
| Item | Log loss |
|---|---|
| p 0.01 | 4.61 |
| 0.05 | 3 |
| 0.1 | 2.3 |
| 0.2 | 1.61 |
| 0.5 | 0.69 |
| 0.9 | 0.11 |
| 0.99 | 0.01 |
Step 9 of 12Note
Step 10 of 12Practice · Diagram
Logistic regression, end to end
Which step turns an unbounded score into a number between 0 and 1?
Tap the shape that answers the question, then press Check.
- Features
For example income, debt ratio, number of late payments.
- Weighted sum z
z = w1x1 + w2x2 + ... + b; also the log-odds.
- Sigmoid
p = 1 / (1 + e^-z), squashed between 0 and 1.
- Probability p
- Threshold
Default 0.5, but choose it from the costs of each error.
- Class 0 or 1
Step 11 of 12Practice · Select all that apply
Which statements about logistic regression are true? Select all that apply.
Select all that apply.
Step 12 of 12Practice · Choose one
In a logistic regression model, the weight for "has a premium account" is 1.1. Since e1.1 ≈ 3, what does this mean?
Lesson complete!