1. Home
  2. AI & Machine Learning
  3. Logistic Regression

Logistic Regression

Predict yes/no probabilities with an S-shaped curve. Watch a 3D probability sheet bend to separate two classes as it trains.

Interactive 3DIntermediate13 min readAI/MLUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

Logistic regression predicts a probability between 0 and 1 — e.g. "will this student pass?" from two features. The coloured sheet is the model’s probability for every point on the floor. Press Train and watch it bend into an S-curve.

Pseudocode

  1. z ← w₁·x₁ + w₂·x₂ + b
  2. p ← σ(z) = 1 / (1 + e^(−z)) // probability of class 1
  3. loss ← −mean( y·log p + (1−y)·log(1−p) ) // cross-entropy
  4. repeat:
  5. w ← w − η · mean( (p − y) · x )
  6. b ← b − η · mean( p − y )
  7. predict class 1 when p ≥ 0.5 (the decision boundary is z = 0)
Read this model as text · 14 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. Logistic regression predicts a probability between 0 and 1 — e.g. "will this student pass?" from two features. The coloured sheet is the model’s probability for every point on the floor. Press Train and watch it bend into an S-curve.
  2. Points of class 1 sit at the top (probability 1), class 0 at the bottom. With all weights 0 the model says p = 0.5 everywhere — a flat sheet halfway up. Glowing points are currently misclassified.
  3. Step 1: move w and b against the gradient of the cross-entropy loss. The sheet tilts into an S-shaped sigmoid that rises towards the class-1 points. Loss 0.302, accuracy 100%.
  4. Step 2: move w and b against the gradient of the cross-entropy loss. The sheet tilts into an S-shaped sigmoid that rises towards the class-1 points. Loss 0.217, accuracy 100%.
  5. Step 4: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.15, accuracy 100%.
  6. Step 7: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.109, accuracy 100%.
  7. Step 12: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.08, accuracy 100%.
  8. Step 20: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.059, accuracy 100%.
  9. Step 35: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.042, accuracy 100%.
  10. Step 60: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.03, accuracy 100%.
  11. Step 100: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.021, accuracy 100%.
  12. Step 160: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.015, accuracy 100%.
  13. Step 250: the S-curve gets steeper and the white boundary (p = 0.5) settles between the classes. Loss 0.011, accuracy 100%.
  14. Trained! To classify a new point, compute p = σ(w₁x₁ + w₂x₂ + b) and predict class 1 if p ≥ 0.5. The decision boundary is a straight line — logistic regression is a linear classifier. Accuracy 100%.

Try this in the 3D model

  • Rotate the scene to look along the boundary. Can you see the S-shaped sigmoid?
  • Train on Overlapping data. Why does accuracy stay below 100%?
  • Try learning rate 0.1 and then 1.5. Which one finishes faster?
  • Watch the glowing (misclassified) points disappear as training goes on.

From numbers to yes/no

Linear regression predicts a number. But many questions have yes/no answers: Will this student pass? Is this email spam? Is this tumour malignant? We want a probability between 0 and 1.

Logistic regression does this in two steps:

  1. Compute a score exactly like linear regression: z = w₁x₁ + w₂x₂ + b
  2. Squash it into (0, 1) with the sigmoid function:
σ(z) = 1 / (1 + e^(−z))

The sigmoid is an S-shaped curve: very negative z → almost 0, very positive z → almost 1, and z = 0 → exactly 0.5.

Seeing it in 3D

In the model, the floor holds two features (x₁, x₂) and the height of the coloured sheet is the predicted probability of class 1. Class-1 points float at the top (probability 1), class-0 points sit at the bottom. As training runs, the flat sheet tilts and bends into an S-shaped “cliff” that rises right between the two groups.

The decision boundary

We predict class 1 when p ≥ 0.5, which happens exactly when z ≥ 0. So the boundary is the set of points where

w₁x₁ + w₂x₂ + b = 0

— a straight line (the white line in the model). Logistic regression is therefore a linear classifier.

Training: cross-entropy and gradient descent

For a point with label y (0 or 1) and predicted probability p, the cross-entropy loss is:

loss = −[ y · log(p) + (1 − y) · log(1 − p) ]

It’s near 0 when the prediction is confident and right, and huge when it’s confident and wrong. Its gradient turns out to be beautifully simple:

∂loss/∂w = (p − y) · x        ∂loss/∂b = (p − y)

So each step nudges the weights in proportion to the error p − y, just like gradient descent for linear regression.

Code

import numpy as np

def sigmoid(z):
    return 1 / (1 + np.exp(-z))

# features: [hours studied, attendance %] (scaled); label: passed (1) or not (0)
X = np.array([[0.5, 0.4], [1.0, 0.5], [1.5, 0.3], [3.0, 0.9],
              [3.5, 0.7], [4.0, 0.95], [2.0, 0.6], [4.5, 0.8]])
y = np.array([0, 0, 0, 1, 1, 1, 0, 1])

w, b, lr = np.zeros(2), 0.0, 0.5
for step in range(2000):
    p = sigmoid(X @ w + b)
    w -= lr * X.T @ (p - y) / len(y)
    b -= lr * np.mean(p - y)

new_student = np.array([2.8, 0.85])
print("P(pass) =", sigmoid(new_student @ w + b))

With scikit-learn:

from sklearn.linear_model import LogisticRegression
model = LogisticRegression().fit(X, y)
print(model.predict_proba([[2.8, 0.85]])[0, 1])

Beyond two classes and straight lines

  • Multi-class: use one-vs-rest, or the softmax generalisation (the last layer of most neural networks).
  • Curved boundaries: add features like x₁², x₁x₂ — or use a neural network.
  • Regularisation (L1/L2) keeps weights small to avoid overfitting.

Where is it used?

Spam filters, credit scoring, medical diagnosis, click-through prediction, and as the final layer of neural network classifiers. It’s fast, simple and its weights are easy to interpret.

Common mistakes

  • Calling it “regression” and expecting it to predict numbers — it’s a classification method.
  • Using mean squared error as the loss — it trains poorly with a sigmoid.
  • Forgetting that the decision threshold doesn’t have to be 0.5 (e.g. medical tests may use a lower one).

Complexity at a glance

Case / operationTimeWhy
PredictionO(d)One weighted sum and a sigmoid.
One training stepO(n · d)
Extra spaceO(d)

Quick check

Test yourself — pick an answer to see if you got it.

1. What is the output range of the sigmoid function σ(z)?

2. σ(0) equals…

3. What shape is the decision boundary of logistic regression (with the raw features)?

4. Which loss function is used to train logistic regression?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Logistic Regression. Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.