Logistic Regression Explained With a Worked Example

Logistic Regression cover with an S-shaped lime cord and two rows of counters on dark green paper.

A warehouse wants to flag packages that may arrive late. It can record a risk score for each package or make a yes-or-no decision about sending an alert. Logistic regression connects those two steps by estimating a probability that the warehouse can compare with a cutoff.

Binary logistic regression estimates the probability of one of two outcomes from a weighted sum of input features. It applies a sigmoid function to the sum. The resulting number falls between 0 and 1, and a separate decision threshold can turn it into a class label.

Follow a package from inputs to probability

Suppose a fitted model uses route distance and whether the depot is congested. To keep the arithmetic small, measure distance in tens of kilometers and code congestion as 0 for no and 1 for yes. Use an intercept of −3, a distance coefficient of 0.4, and a congestion coefficient of 1. These numbers are illustrative.

For a package traveling 60 kilometers from a congested depot, the linear score is −3 + (0.4 × 6) + (1 × 1) = 0.4. Apply the sigmoid, 1 ÷ (1 + e−score), to get about 0.599. The model estimates roughly a 60% chance of arriving late.

S-shaped sigmoid curve rising from near zero to near one and crossing 0.5 at a score of zero.
The sigmoid maps a score of zero to 0.5 and flattens near zero and one. Google Machine Learning Crash Course. Licensed under CC BY 4.0.
RouteLinear scoreEstimated late probability
40 km, no congestion−1.419.8%
60 km, no congestion−0.635.4%
60 km, congestion0.459.9%

A score of zero maps to 50%. Positive scores map above 50% and negative scores below it. The curve flattens near each end, so a one-unit increase in score changes the probability more near the middle than near the extremes. Google Machine Learning Crash Course on the sigmoid function.

Read coefficients in odds

The linear score represents log-odds, where odds equal probability divided by one minus probability. Holding distance fixed, congestion adds one unit to the log-odds. That multiplies the odds by e, or about 2.72. The probability change depends on the starting score.

In the table, congestion moves the 60-kilometer estimate from about 35% to 60%. The same coefficient can produce a smaller percentage-point change on a route where the initial probability is already very low. Treat these coefficients as associations learned from the data.

Choose a threshold after training

At a cutoff of 0.50 the example package is flagged. At 0.70 it is left unflagged. The probability stays about 60% in both cases. A warehouse that can review only ten shipments per hour might choose a threshold that keeps the expected alert volume within that limit.

On validation data, compare how many late packages each threshold catches with how many on-time packages it flags. Lowering the cutoff can catch more late shipments and increase the review workload. A precision–recall curve lets you compare those outcomes across cutoffs. Scikit-learn precision–recall example.

Precision–recall curve for a binary linear SVM with a dashed baseline.
This binary SVM example illustrates how precision and recall vary across cutoffs for a scored classifier. Scikit-learn developers.

Keep the final test set separate from threshold selection. Then report the confusion matrix at the chosen cutoff so readers can see the false alarms and misses. Unlimited AI guide to reading a confusion matrix.

When logistic regression is a good baseline

Try it when you have labeled examples, a binary outcome, and a need for a model you can inspect. Training estimates coefficients by reducing log loss, typically with regularization to limit large weights. If the raw features are on very different scales, consider scaling continuous inputs before fitting and learn the scaling from the training data.

With the same feature representation, the model’s boundary is linear. If a package is unusually risky only when a long route coincides with congestion, you can test an interaction feature. Compare the revised model against your baseline on identical validation splits. Scikit-learn LogisticRegression reference.

If you need to interpret 60% as a frequency, check calibration. Among cases given about 60% risk, roughly 60% should be positive in a well-calibrated model. Plot observed rates against predicted probabilities on held-out cases, especially when training data was resampled or conditions differ from deployment. Scikit-learn calibration example.

Common questions

Why is it called regression if it classifies?

The model estimates a continuous probability by fitting a relationship between features and log-odds. You can use the probability directly or apply a threshold to produce a class.

Can it handle more than two classes?

Yes. Multinomial logistic regression estimates a probability distribution across several classes. Binary logistic regression uses a single sigmoid for the positive-class probability.

Is 0.50 the best cutoff?

Choose a cutoff based on the trade-off between missed positives and false alarms. Use validation data that reflects the cases and workload you expect, then test the chosen rule on separate examples.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top