CLASSIFICATION · BEGINNER GUIDE

Gradient Descent for Logistic Regression: A Small Example

See how logistic regression updates its weights to reduce prediction error, with a small, language-neutral worked example.

Worked example · gradient descent logistic regression pythonUpdated October 31, 2026

Quick answer: Gradient descent changes model weights in the direction that lowers a chosen loss. Logistic regression maps a weighted input through a sigmoid to produce a probability, then learns weights from labeled examples.

Infographic for gradient descent logistic regression python: Encode the class outcome, then Calculate model probabilities, then Compute loss gradients, then Update and validate weights.
Infographic: a practical sequenceKeep the question, design and assumptions in view from start to finish.
One logistic-regression learning loop: Predict probabilities, then Measure log-loss, then Update weights, then Check held-out data.
One logistic-regression learning loopThe optimizer changes weights; validation checks whether learning transfers.

What this method answers

A logistic model forms z=b+w₁x₁+… and converts it to probability 1/(1+e⁻ᶻ). Log-loss penalizes confident wrong predictions. A gradient estimates how each weight should move to reduce that loss.

Use an optimizer when fitting a differentiable model. A library solver is safer for production; a manual update is useful for understanding learning.

Before running a calculation, write down the population and the exact quantity you want to estimate. A method name alone does not define the question, the comparison or the assumptions.

Steps to follow

  1. Represent the binary outcome consistently as 0 or 1.
  2. Calculate the linear predictor and sigmoid probability.
  3. Use the selected loss gradient to update the intercept and feature weights.
  4. Track convergence, then evaluate on held-out data.

Keep a record of exclusions, recoding decisions and software settings. That makes the analysis easier to check and repeat.

Worked example

A model predicts whether a learner passes from study hours. Start with intercept b=0 and slope w=0, so the sigmoid predicts 0.5 for each learner.

LearnerHoursOutcome
A1Fail (0)
B3Pass (1)
C4Pass (1)

A training step computes probabilities, compares them with labels, averages the loss gradient, then updates a weight as w_new=w_old−learning_rate×gradient. The exact numeric update depends on the loss, scaling and learning rate.

Interpret the result carefully

Repeated updates may assign larger probabilities to higher study hours if the data support that pattern. Lower training loss alone does not show generalization.

Limit to keep in mind: Poor scaling, a large learning rate, separation or a weak feature set can destabilize fitting. Validate on held-out data and check calibration when probabilities drive decisions.

When reporting an analysis, include the sample size, estimate, units and uncertainty. Explain why the method fits the sampling and measurement design. Separate planned tests from exploratory checks, and avoid saying that association proves a cause.

Frequently asked questions

Does gradient descent output classes?

It fits weights; the model outputs probabilities that a separate threshold can convert to labels.

What does the learning rate do?

It sets update size. Too large may overshoot; too small can make fitting slow.

Should I standardize inputs?

Often helpful when scales differ, especially with regularization. Fit scaling inside training folds.

Is training loss enough?

No. Validate the intended metric and check calibration if probabilities matter.

Sources and next steps