Skip to content

From Neurons to Networks

Logistic regression

From a line to a probability, a decision

Bend a regression line through an S-shaped curve and it stops predicting numbers and starts predicting probabilities - the leap from "how much?" to "which class?"

Linear regression answers how much - a house price, a temperature, a number on a line. But an enormous share of real questions are yes / no: spam or not, fraud or not, this word or that word. The fix is small and beautiful: take the model's raw score and squeeze it through a smooth S-curve called the sigmoid. Out comes a number pinned between 0 and 1 - a probability - and a probability is one threshold away from a decision.

The one-sentence idea

A straight line can shoot off to and - nonsense as a probability. So we wrap it in a function whose output can only live between 0 and 1, is 0.5 right in the middle, and saturates smoothly toward the ends. That wrapper is the sigmoid, and everything else in this lesson is a consequence of its shape.

From a straight line to a probability

Start with the familiar linear score - the same weighted sum linear regression uses. For a single feature it is just

Score, then squash

This can be any real number. Feed it to the sigmoid (also called the logistic function) to get a probability :

Read off its three defining habits: exactly; as it climbs toward ; as it sinks toward . It never quite reaches either end - the output is always strictly between 0 and 1, which is exactly what a probability needs.

In the demo below it's handier to write the score as . That's the same line - just with - but now the two knobs mean something you can see: is the input value where the score crosses zero (so ), and is the steepness of the S.

Where the decision happens

A probability isn't yet a class. To decide, pick a threshold - the natural default is - and call it class 1 when , else class 0. Because is exactly at , that rule is astonishingly clean:

The decision boundary

The single point where the curve passes through is the decision boundary. Everything to one side is predicted class 1, everything to the other side class 0. Slide and the boundary slides with it.

Now play. Below is a 1-D dataset: blue points are true class 0 (sitting on the bottom, ), amber points are true class 1 (on top, ). Drag the curve left/right to move the boundary , and use the steepness slider to sharpen or soften the S. Each point is filled by the probability the curve assigns it, and ringed green if that prediction is right, red if wrong. Watch the accuracy climb as you separate the colors.

Drag the curve left or right to move the boundary t, and use the steepness slider to sharpen or soften the S. Each point is filled by the probability the curve assigns it and ringed green if right, red if wrong.
00.510246810feature x →ppredict 0predict 1boundary t = 4.0
true class 0 (bottom)true class 1 (top)predicted rightpredicted wrongdecision boundary (p = 0.5)
accuracy81%
correct13 / 16
log loss0.354

Drag the curve to move the boundary and split the two colors.

Steepness ≠ boundary - a subtle, important split

Try it: crank steepness from gentle to sharp. The boundary doesn't budge and the accuracy doesn't change - because is equivalent to no matter how big is. What steepness does change is confidence: a steep S snaps its probabilities toward 0 and 1 (a near-hard "step" decision), while a gentle S leaves everything hovering near 0.5 (an unsure, soft decision). Only the boundary position moves the labels; steepness sets how boldly the model commits - and that shows up not in accuracy but in the log loss.

Grading a probability: log loss

Accuracy only asks "right or wrong?" It can't tell a lucky 0.51 from a confident 0.99. To train with gradient descent we need a smooth score that rewards calibrated confidence. That's log loss, a.k.a. binary cross-entropy. For one labelled point with true label and predicted probability :

Binary cross-entropy (one point)

Only one term ever survives. If the truth is , it collapses to : near-zero loss when , exploding toward when . If , it becomes , punishing a confident-but-wrong just as harshly. Confident and right is cheap; confident and wrong is ruinous - exactly the incentive we want.

This is the cost function you met last lesson, specialized for classification, and it's what gradient descent actually minimizes when it fits and . In the demo, watch the log loss readout react to steepness even while accuracy sits still - that's the model getting more (or less) sure of itself.

Straight boundaries, and bending them

With one feature the boundary is a single point; with two features it becomes a straight line; with many, a flat hyperplane. That's why plain logistic regression is a linear classifier - its dividing surface is always flat. When the classes coil around each other so no straight cut works, you either hand it richer, transformed features (say or ) so a curve becomes possible, or you stack many logistic-style units into a neural network - the very next idea, and the reason the humble sigmoid keeps showing up.

Logistic regression, in six lines of Python:

import math

def sigmoid(z):   # squash any score into (0, 1)
    return 1 / (1 + math.exp(-z))

def prob(x, w, t):    # score z = w*(x - t), then squash
    return sigmoid(w * (x - t))

def predict(x, w, t): # threshold at 0.5 -> a hard class
    return 1 if prob(x, w, t) >= 0.5 else 0

def log_loss(y, p):   # binary cross-entropy for one point
    return -(y * math.log(p) + (1 - y) * math.log(1 - p))
Logistic regression, in six lines of Python.

Check yourself

Feeding a raw score through the sigmoid produces:

Increasing the steepness w makes the S-curve:

Write the sigmoid, and say in one line what it does to a score.

Recall the definition and its effect together. Try to state it, then check.

Lock it in

  • The sigmoid squashes any real score into , turning it into a probability that is 0.5 at .
  • Threshold the probability at 0.5 and the decision boundary is the single point where the curve crosses 0.5.
  • Steepness sets confidence, not the boundary; it moves the log loss but never the accuracy.
  • Log loss (binary cross-entropy) is the smooth cost gradient descent minimizes; plain logistic regression stays a linear classifier.

Primary source

Andrew Ng, Machine Learning Specialization

Andrew Ng's Machine Learning Specialization derives logistic regression, the sigmoid, the decision boundary, and log loss step by step - the canonical first pass. For a slower, picture-first walk through the same ideas (including why cross-entropy is the right cost), StatQuest's Logistic Regression playlist is superb.

Ask your teacher

Here's the thread worth pulling: this exact idea sits under every large language model. A sigmoid turns one score into the probability of one class; softmax is its multi-class sibling - it takes a whole vector of scores (one per word in the vocabulary) and turns them into a probability distribution that sums to 1. That softmax is literally the output layer of every LLM: the next-token probabilities you sample from. Ask me how softmax collapses back into the sigmoid when there are only two classes, why log loss is the natural partner of both, or how the decision boundary grows from a point to a line to a hyperplane as you add features.