Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 7: Classification and Logistic Regression

Not every prediction is a number. Often the question is a yes-or-no one: is this email spam, will this patient develop a condition, will this customer leave? These are classification problems, and the workhorse for them is logistic regression — a cousin of linear regression that has been adapted to answer yes-or-no questions. Despite its name, it is a classifier, not a regression model, and it is one of the most used models in the world, from medicine to banking to marketing.

The problem with using a straight line for a yes-or-no question is that a line runs off to infinity in both directions, while an answer like yes or no has to sit between 0 and 1. Logistic regression solves this with a curve called the sigmoid function, shaped like a stretched-out S. Feed it any number, however large or small, and it returns a value between 0 and 1, which we read as a probability. A value of 0.9 means the model is fairly confident the answer is yes; 0.1 means it is fairly confident the answer is no; 0.5 means it genuinely cannot tell.

From probability to decision

A probability is not yet a decision, and turning one into the other needs a rule. The usual rule is a threshold of 0.5: above it, predict yes; below it, predict no. But the threshold is a choice, not a law of nature, and changing it changes the kind of mistakes the model makes. Imagine a worked example: a clinic uses a model to flag patients who may need a follow-up test. If the threshold is set low, the model flags many people, catching nearly everyone who needs the test but also worrying many healthy people. If the threshold is set high, it flags few people, missing some who needed the test. Which threshold is right depends on which mistake is worse — and that is a human decision, not a mathematical one.

Logistic regression learns in the same spirit as linear regression: it adjusts its settings to make its predictions match the labels, using a loss function suited to probabilities. The fitted settings can also be read as explanations. If the setting for the feature age is positive, then older patients have higher predicted probability; if it is negative, the opposite holds. That readability is a real strength: a doctor or a bank manager can look at the model and understand why it says what it says, which is exactly what you want when a decision affects a person. More complex models often cannot offer that.

More than two classes

Logistic regression handles yes-or-no questions, but many real problems have more than two answers: which of ten digits is written in this image, which of five kinds of flower is this, which of three delivery options will a customer choose? The standard extension trains one yes-or-no model per class — is it a 3 or not, is it a 7 or not — and then picks the class whose model is most confident. A cleaner version trains all the classes together, using a function that turns the model's scores into probabilities that add up to one across all the classes. Either way, the ideas are the same: a probability for each answer, a threshold for deciding, and the same trade-offs between the kinds of mistakes. The confusion matrix grows from four boxes to a grid, but it is read the same way: the diagonal shows the correct predictions, and everything off the diagonal shows exactly which classes the model confuses with each other — information a single accuracy figure could never give you.

Like linear regression, logistic regression has limits. It draws a straight dividing line — a linear decision boundary — between the two classes, so it struggles when the true boundary curves or twists. It also assumes each feature adds its effect independently, which is not always true. Even so, it is the right first model for almost any yes-or-no problem: it is fast, it is honest about its uncertainty through probabilities, and it gives you a baseline that every fancier model must beat before it earns its place.

The sigmoid curve turns any number into a probability

1.0 0.5 0.0 0.5 — the usual threshold model's raw score The S-curve squeezes any score into a probability

The curve never quite reaches 0 or 1, but it gets very close. The middle of the S is where the model is most uncertain.

Key Terms

Classification
A supervised task where the answer to predict is a category, such as spam or not spam.
Logistic regression
A model for yes-or-no questions that turns a weighted sum of features into a probability between 0 and 1.
Sigmoid function
An S-shaped curve that squeezes any number into a value between 0 and 1, read as a probability.
Probability
A number between 0 and 1 measuring how likely something is; logistic regression outputs one for each prediction.
Threshold
The probability cut-off, usually 0.5, above which the model predicts yes and below which it predicts no.
Decision boundary
The line or surface that separates the regions where a classifier predicts one class from those where it predicts another.
False positive
A case the model wrongly flags as yes, such as a genuine email sent to spam.

Discussion Questions

  1. A clinic's model flags patients for a follow-up test. Why might a doctor prefer a low threshold, even though it causes more false alarms?
  2. Logistic regression outputs probabilities rather than plain yes-or-no answers. Why is that useful for a person making a decision?
  3. When would a straight dividing line between two groups be a bad description of the real boundary?

Practice Questions

  1. Why can't a straight line be used directly for a yes-or-no question?
  2. What does a logistic regression output of 0.9 mean?
  3. What happens at the usual threshold of 0.5?
  4. Lowering the threshold in the clinic example catches more patients who need the test. What is the cost?
  5. Why is logistic regression easy to explain to a doctor or bank manager?
Show answers
  1. Because a line runs to infinity, while a yes-or-no answer must sit between 0 and 1. Logistic regression uses the sigmoid curve to squeeze the line's output into a probability.
  2. The model estimates a 90% chance the answer is yes. Outputs are probabilities, so 0.9 is fairly confident yes and 0.1 is fairly confident no.
  3. Probabilities above it are predicted yes; below it, no. The threshold turns a probability into a decision, and moving it changes which mistakes the model makes.
  4. More healthy people are wrongly flagged. A low threshold trades false alarms for fewer missed cases, which is the right trade when missing a case is worse.
  5. Because each feature's setting shows whether it pushes the probability up or down. A positive setting means higher values of that feature raise the predicted probability, which can be read directly from the model.
Keep reading to complete this lesson