The idea: a line that gives probabilities

Logistic regression is a classifier for two classes, y = 1 or y = 0. Like the perceptron it draws a straight line through the inputs, but instead of a hard yes/no it gives a probability: points far on the class-1 side get p close to 1, points far on the other side get p close to 0, and points on the line get 0.5.

Left: ten points in the plane, class 1 upper right and class 0 lower left, a thick blue decision boundary between them labelled p = 0.5, two thin grey parallel lines labelled p = 0.9 and p = 0.1, and a green arrow w pointing toward class 1. Right: the S-shaped sigmoid curve of z, with p = 0.1 at z = −2.2, p = 0.5 at z = 0 and p = 0.9 at z = 2.2
z = w·x + b measures how far a point is from the line; the sigmoid turns that into p. Lines of equal p run parallel to the boundary.

The page trains one on 10 points in the plane (or 12 that overlap), starting from a line that is the wrong way round, w = (1, −1), b = 0. Each panel shows one piece: the points and the line, the sigmoid with one dot per point, and the log loss of each point as a bar.

The sigmoid and the decision boundary

For a point x = (x1, x2) the model computes a score z = w1·x1 + w2·x2 + b and squashes it with the sigmoid σ(z) = 1 / (1 + e^−z) into p = P(y = 1 | x). σ(0) = 0.5, so the decision boundary p = 0.5 is exactly the line z = 0. The weight vector w is perpendicular to it and points toward class 1.

The lines p = 0.9 and p = 0.1 are z = ±ln 9 ≈ ±2.2. They sit at distance 2.2 / |w| from the boundary, so a longer w means a narrower band: |w| is how confident the model is, the direction of w is where the line points. (Demo: one point through the sigmoid.)

Log loss

A probability is judged by the log loss (cross-entropy) of the true class: −ln(p) when y = 1 and −ln(1 − p) when y = 0. The training loss L is the mean over all points. At the start, point 1 (class 1) gets p = 0.20, a loss of 1.61; point 2 (class 0) gets the same p = 0.20, but for it that is a good answer: loss 0.22.

Two curves of loss against p from 0 to 1: −ln(p) for y = 1 falls from very high at p near 0 to 0 at p = 1, and −ln(1 − p) for y = 0 rises from 0 to very high. At p = 0.20 the y = 1 curve is at 1.61, marked wrong, and the y = 0 curve is at 0.22, marked right
The same p = 0.20 costs 1.61 for a class-1 point and 0.22 for a class-0 point.

A model that says p = 0.01 for a point that is really class 1 pays −ln(0.01) = 4.6, so log loss punishes confident mistakes hard. It also comes from maximum likelihood: minimizing it is the same as making the observed labels as probable as possible.

The gradient: every point pushes by p − y

The derivative of the loss works out to something short:

  • ∂L/∂w = mean((p − y)·x)
  • ∂L/∂b = mean(p − y)

Each point pushes the weights by its error p − y times its input. One gradient descent step is w ← w − η·∇w, b ← b − η·∇b, and the line rotates and shifts. (Demo: one gradient step, narrated; Demo: train until every point is on its side.)

PerceptronLogistic regression
outputa class ±1 (step)a probability (sigmoid)
which points move the lineonly mistakesevery point, by p − y
a point far on the right sideno effectalmost none (p − y ≈ 0)
overlapping classesnever settlessettles at the lowest loss

Separable data: w keeps growing

When a line separates the classes, the loss can always get smaller by making w longer: every p moves closer to 0 or 1. So L falls toward 0 but never reaches it, |w| grows without end, and the p = 0.1 / 0.9 lines close in on the boundary. Real libraries stop this with regularization, a penalty λ·|w|² added to the loss (scikit-learn's C = 1/λ). (Demo: separable data, w keeps growing.)

With overlapping classes no line gets every point right. The pushes of the misplaced points balance the others, w stops growing and L levels off above 0. (Demo: overlapping classes, the loss levels off.)

The learning rate

The log loss of logistic regression is convex: one bowl, no local minima, so gradient descent always heads to the same answer. η only sets the speed: after 20 steps η = 0.1 is still far from it while η = 2 is much closer. (Demo: learning rate 0.1 vs 2.) See Gradient Descent for steps that are too large and zig-zag.

Related models

ModelOutputLoss
linear regressiona number w·x + bmean squared error
logistic regressionp = σ(w·x + b)log loss
softmax regressionone probability per classcross-entropy
neural networkmany logistic-like units in layerslog loss / cross-entropy

See also Linear & Logistic Regression (the one-input version, where the sigmoid is drawn over x), Perceptron (the same line with a hard step and mistake-only updates), Gradient Descent, Softmax & Cross-Entropy for more than two classes, and Neural Network Forward Pass.

The predicted probability is a score, and the ROC Curve and AUC page shows how sweeping a threshold over such scores draws the ROC curve and measures how well they rank positives above negatives.

See also Generative vs Discriminative Classifiers, where LDA reaches the same σ(w·x + b) form from class Gaussians and is compared with logistic regression on few points and with an outlier.

What the page leaves out

Only two inputs, so the boundary is a line you can see; with n inputs it is a hyperplane. Training is full-batch gradient descent with no regularization; real solvers (L-BFGS, Newton's method, liblinear) converge in far fewer steps and add an L2 penalty by default. Feature scaling, more than two classes (one-vs-rest, softmax), calibration, and non-linear boundaries from added features are not shown.

On the mind map

Open the Machine Learning mind mapMachine LearningLearning setup: what feedback, how to scoreLearning setupLinear models: weighted sum w·x + bLinear modelsOther classic models: trees, margins, neighboursOther classic modelsUnsupervised: find structure without labelsUnsupervisedTraining: loss, gradient, small stepTrainingNeural networks: layers of σ(W·a + b)Neural networksArchitectures: how layers share informationArchitecturesLanguage models: predict the next tokenLanguage modelsLLM speed and tuning: cheaper to run, safer to useLLM speed and tuningRetrieval and agents: give the model data and toolsRetrieval and agentsLinear regressionLinear regressionminimize squared residualsLogistic regressionsigmoid of w·x + b, log lossGenerative vs DiscriminativeGenerative vs DiscriminativeModel P(x|y) and use Bayes, or fit P(y|x)PerceptronPerceptronfix weights only on mistakesHopfield networkHopfield networkEnergy only falls; recall = nearest valleySupport vector machineSupport vector machinewidest margin, set by support vectorsL1 vs L2 regularizationL1 vs L2 regularizationL1 gives exact zeros, L2 shrinks allOpen the Machine Learning mind mapMachine Learningweighted sum w·x + bLinear modelsLinear regressionLinear regressionminimize squared residualsLogistic regressionsigmoid of w·x + b, log lossGenerative vs DiscriminativeGenerative vs DiscriminativeModel P(x|y) and use Bayes, or fit P(y|x)PerceptronPerceptronfix weights only on mistakesHopfield networkHopfield networkEnergy only falls; recall = nearest valleySupport vector machineSupport vector machinewidest margin, set by support vectorsL1 vs L2 regularizationL1 vs L2 regularizationL1 gives exact zeros, L2 shrinks all
This page on the Machine Learning mind map · 88 pages in 10 branches · open it to see the whole map and test yourself in recall mode.