Logistic regression: train the S-curve step by step on your own data
Twenty students, their hours of study and whether they passed. Press run and watch gradient descent bend an S-shaped curve through the data, with every row's probability, loss and gradient shown as it changes. Then paste your own data.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
The chart shows one dot per student: hours studied along the bottom, and 0 (failed) or 1 (passed) up the side. The orange curve is the model's probability of passing for any number of hours. At the start both the weight and the bias are zero, so the curve is a flat line at 50%: the model knows nothing and guesses evenly.
The table under the steps holds the data. The white columns are the inputs you can edit; the orange columns are computed live for every row: the score z, the probability p, that row's log loss, its contribution to the gradient, and whether the 50% cut-off classifies it correctly. The results panel shows the mean loss, both gradients, the accuracy and the number of hours where the model gives an even chance.
The algorithm
Logistic regression predicts a probability with one weighted sum and one squashing function. For every row it computes a score z = w × x + b and passes it through the sigmoid, 1 / (1 + e^(−z)), which turns any number into a value between 0 and 1.
Training needs a way to say how wrong the probabilities are. Log loss does that: for a student who passed, the loss is −ln(p), so a confident wrong answer (p near 0) costs a lot and a confident right one costs almost nothing. The mean over all rows is the number gradient descent pushes down.
The gradient of log loss has a famously simple form: for each row it is (p − y) times the input. Average it over the rows and you know which way to move w and b. Each step subtracts the learning rate times the gradient. The simulation standardises hours first (subtracts the mean and divides by the spread), which is what libraries do in practice, so one learning rate works for any units.
The L2 penalty adds λ × w to the weight's gradient. It stops the weight growing without limit when the classes separate perfectly, which would otherwise push the curve towards a vertical step.
Things to try
- Press Run with the defaults and watch the loss chart drop quickly, then flatten. The accuracy settles while the loss keeps creeping down.
- Raise the learning rate to 3: the loss falls in fewer steps. With a very small rate, say 0.02, it crawls.
- Edit the data so that every student above 3 hours passed and everyone below failed. Without the L2 penalty the weight keeps growing and the curve becomes a cliff. Add a penalty of 0.1 and it stops at a sensible slope.
- Paste your own two columns, for example days since sign-up and whether a customer bought, and read off the 50% point.
- Download the results as CSV: you get every row's probability, ready for a spreadsheet.
How it works in real systems
Logistic regression is still one of the most used models in practice: credit scoring, click prediction and medical risk scores often start with it, because its weights are easy to explain and it trains in milliseconds. Libraries such as scikit-learn fit it with second-order solvers like L-BFGS or Newton methods rather than plain gradient descent, but they minimise the same log loss, usually with an L2 penalty switched on by default.
The same ideas run inside neural networks. The last layer of a binary classifier is exactly this model, and its training signal is the same (p − y) gradient flowing back through the network. Softmax regression extends it to many classes.