How a Machine Learns Using Logistic Regression

Prepared by Sajjad Ahmed Niloy

Watch a Logistic Regression model learn to predict diabetes from three health measurements: Glucose, BMI and Age. It trains with stochastic gradient descent: for every patient it computes a weighted sum z, squashes it with the sigmoid into a probability ŷ, measures the loss, and nudges the weights to reduce it.

Every step you run is highlighted in the source code at the bottom of the page, so you can see exactly which line is executing.

Data is synthetic (20 training / 6 test patients), generated to resemble diabetes-screening patterns. It is not real patient data. Inputs are standardised (mean 0, std 1) before training, which is why a normal learning rate such as 0.1 works.

Controls

Shortcuts: Space play / pause, → one step. You can change α and speed while training is running.

Training data (20 patients)

#GlucoseBMIAgeDiabetes (y)

The highlighted row is the patient being processed; greyed rows were already used in this epoch.

Feature correlation

Pearson correlation on the training set. Pink = negative, green = positive. Glucose and BMI correlate most strongly with diabetes.

🧠 Model internals

w₁ Glucose0.0000
w₂ BMI0.0000
w₃ Age0.0000
b Bias0.0000
Epoch 0 / 100Patient – / 20

Live calculations

1 SelectPick the next patient from the training set. SGD learns from one patient at a time. 2 ZCombine the patient's features with the current weights into a single score z. 3 σTurn the score z into a probability between 0 and 1 using the sigmoid function. 4 LossMeasure how wrong that probability is compared with the true label. 5 UpdateAdjust every weight a little in the direction that reduces the loss (gradient descent).
RawThe patient's measurement in its original units (mg/dL, kg/m², years). Scaled xThe raw value after standardising: (raw − training mean) ÷ training standard deviation. Puts all features on the same scale so training is stable. WeightHow strongly this feature pushes the prediction toward diabetes (positive) or away from it (negative). This is what the model learns.
Glucose–––
BMI–––
Age–––
Weighted sum (z)iA linear score. Each scaled feature x is multiplied by its weight w, the results are added, then the bias b shifts the total. z > 0 leans "diabetes", z < 0 leans "no diabetes". z = 0 is the decision boundary.
Sigmoid (ŷ)iThe sigmoid σ squashes any score z into a probability between 0 and 1. Large positive z gives ŷ near 1, large negative z gives ŷ near 0, and z = 0 gives exactly 0.5. e is Euler's number (≈ 2.718).
Loss (log-loss)iBinary cross-entropy for one patient. If the true label y is 1 it reduces to −ln ŷ; if y is 0 it is −ln(1 − ŷ). Confident wrong answers get a huge loss, confident right answers get a tiny one. ln is the natural logarithm.
Gradient updateiOne step of gradient descent. The error (ŷ − y) says how far off we were; multiplying by the feature x says how much that feature is to blame. α (the learning rate) scales the step. The bias moves by α·error only.

Press Step or Play to begin training. The model starts with all weights at 0 (it predicts 50% for everyone).

Data and decision boundary

Blue region = model predicts no diabetes, red region = predicts diabetes (deeper colour = more confident). Dot size = age. The purple line is where the model is 50/50, drawn with Age held at its training average (a 3-feature model really has a plane; this is a 2-D slice of it).

Train vs test performance

Accuracy after every epoch. The test set is only measured here; the model never learns from it. If train keeps rising while test falls, the model is overfitting. With only 6 test patients the test line moves in big jumps.

Cost per epoch

Average log-loss over all 20 training patients, measured once at the end of every epoch (epoch 0 = untrained model, ln 2 ≈ 0.693).

Python code, live

The line(s) being executed are highlighted; yellow values are from the current step.

Ready