Gradient LabStart the lesson

Lessons › Chapter 4: Classification

Logistic regression

Some questions have only two answers. Will this student pass? Is this email spam? This kind of problem is called classification. Here, students who passed sit up top and students who failed sit on the floor.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

Will this loan be repaid?

The real question is yes or no, but the board still wants a probability attached to each decision.

Concept

Yes-or-no questions

Some questions have only two answers. Will this student pass? Is this email spam? This kind of problem is called classification. Here, students who passed sit up top and students who failed sit on the floor.

Problem

A straight line doesn’t fit

Try the straight line from linear regression. It slices through the middle, but it keeps going: below 0 and above 1. A “120% chance of passing” makes no sense.

Key idea

Bend the line into an S

Logistic regression feeds the straight line into a sigmoid, an S-shaped squasher. Very low scores flatten out near 0, very high ones near 1. Now every output is a sensible chance.

Build

Read off a chance

A new student studied 4 hours. Go up to the curve and read the height: about a 75% chance of passing.

Build

Pick a cutoff

To give a plain yes or no, choose a threshold, usually 50%. Above it we guess “pass”, below it “fail”. Here the switch happens at about 3.3 hours of study.

You can move the cutoff. A stricter cutoff means fewer “yes” answers, but more confident ones.

Real life

In real life: loan approval

Lenders estimate the chance that a loan will be repaid from things like income, existing debts and repayment history. If the chance is above the cutoff they’ve chosen, the application is approved.

Quick check: Why does logistic regression use the S-shaped sigmoid?

The sigmoid squashes any score into the 0 to 1 range, so it can be read as a probability.

Pseudocode

# question: will a student pass? (yes or no)
score = w × hours + b             # a straight line, like before
chance = sigmoid(score)           # squash it between 0 and 1

if chance > 0.5:
    guess "pass"
else:
    guess "fail"

# learning adjusts w and b so the chances match reality

Key terms

classification
Predicting a category (spam or not, cat or dog) instead of a number.
sigmoid
An S-shaped squasher that turns any number into a value between 0 and 1.
threshold
The cutoff. Above it we say “yes”, below it “no”.

Build it: A screening classifier that doesn’t miss cases (35 min, runs in your browser)

See the project