Implement logistic regression with gradient descent in NumPy
8 steps · about 3 h 5 min · Intermediate
- Model probabilities with the logistic function (article, Wikipedia, 20 min)
Sets out the model, the odds interpretation and maximum likelihood fitting.
- Picture how logistic regression fits data (video, StatQuest (YouTube), 10 min)
A clear visual intuition before any algebra.
- Use cross-entropy as the loss (article, Wikipedia, 15 min)
Connects log-loss to likelihood and shows why it suits probabilities.
- Derive the gradient from the lecture notes (reference, Stanford University, 40 min)
Stanford CS229 notes derive the update rule for logistic regression step by step.
- Check gradients numerically and pick a step size (article, Stanford CS231n, 30 min)
Shows numerical vs analytic gradients and the effect of learning rate.
- Vectorise the update with broadcasting (reference, NumPy, 15 min)
Lets you compute predictions and gradients for all rows without loops.
- Code the training loop from scratch (tutorial, ML Glossary, 25 min)
Complete Python implementation of sigmoid, cost, gradient and training.
- Extend to many classes with softmax (article, Dive into Deep Learning, 30 min)
Generalises the same recipe to multiclass classification.