Logistic regression
Turn a weighted input score into a probability between zero and one. Despite its name, this model is commonly used for classification.
Start with an example
A parcel's measurements produce a review probability of 0.7. A threshold of 0.5 sends it for review; a threshold of 0.8 does not.
How it learns
An input feature is a measurement about an example. The target is its known answer: a number for regression, or a category for classification. Training means learning from examples whose answers are already known.
- Multiply inputs by learned weights and add a starting value to make a score. An S-shaped conversion, called the sigmoid, turns that score into an estimated probability between zero and one.
- Training adjusts the weights to give known class labels higher probability. A separate decision threshold turns the estimate into an action: for example, a review probability of at least 0.5 could trigger review. Moving that threshold does not change the probability itself.
Try it yourself
The example above gives the idea. The experiment below uses a separate, made-up dataset with unitless measurements so you can focus on how the model works. Read the graph guide first, then change one thing at a time.
1. Understand what you are looking at
See how an input becomes an estimate of which of two classes it belongs to. Here class 1 could mean a parcel needs review, and class 0 could mean it does not.
- Across: the horizontal axis
- The input measurement x, from 0 to 10. Moving right means a larger input.
- Up: the vertical axis
- The estimated probability of class 1, from 0 to 1. A curve height of 0.8 means an estimated 80% chance, not an observed class of 0.8.
Dark dots are observed answers: class 0 at the bottom or class 1 at the top. The green line is the model's estimate, which can lie between 0 and 1.
How to read one result: Start at x on the bottom axis and follow the vertical dashed line to the outlined diamond. Its height is the estimated probability of class 1 at that input; it is not a new observed example.
2. Predict, change, observe
- Reset and inspect x = 5. The curve is halfway up: 0.5 means an estimated 50% chance of class 1. Predict what happens there if Midpoint moves to 7.
- Move Midpoint to 7 and check the lower estimate at x = 5. Then reset and raise Slope: compare how quickly the curve moves from low to high probability.
- Observations
- Model prediction
- Inspected result (diamond)
Training errors: 3 of 16.
Training errors count observed labels that disagree with the decision: estimates of at least 0.5 choose class 1; lower estimates choose class 0.
3. Explain it in your own words
Does a curve height of 0.8 mean the observed class is 0.8, or that class 1 has an estimated probability of 80%?
Compare your reasoning
It is an estimated probability of 80%. An observed example still has class 0 or class 1. In this experiment, estimates of at least 0.5 are assigned to class 1 when counting training errors. Revisit the dots at 0 and 1 to separate observed answers from estimates.
Ready to continue when you can explain the result and why it changes. If the explanation is still unclear, reset and repeat the two steps above with one control at a time.
What this experiment simplifies
The slope and midpoint are model parameters, adjusted by hand here; training would learn them from data. The curve shows the probability of class 1.
These are small teaching implementations, not scikit-learn runs. Training error on these examples does not estimate performance on new data.
View the plotted examples
| Input feature (x) | Observed class |
|---|---|
| 0.30 | 0 |
| 0.92 | 0 |
| 1.54 | 0 |
| 2.16 | 1 |
| 2.78 | 0 |
| 3.40 | 0 |
| 4.02 | 0 |
| 4.64 | 1 |
| 5.26 | 1 |
| 5.88 | 1 |
| 6.50 | 1 |
| 7.12 | 0 |
| 7.74 | 1 |
| 8.36 | 1 |
| 8.98 | 1 |
| 9.60 | 1 |
The math, one step at a time
Read the symbols after the idea makes sense. You do not need to memorize the formula.
z is a weighted score, b is the intercept, and exp is the exponential function. p is the model's estimated probability of class 1, not a guarantee.
What to watch for
- A linear score cannot express every boundary without transformed features.
- Estimated probabilities can be poorly calibrated; threshold choice needs task-specific validation.
Explore a related model
Further reading
The official documentation goes further when you are ready.