The maths, in dev terms
Save this one
Everything the video skipped: the actual formula behind each term, what it means in plain English, the developer version, and the mistake to avoid. Plus gradient descent from scratch in 12 lines.
- 9 terms
- Formulas + code
- Save it
Featuresinputs
The columns the model learns from. One example is a row of features; the whole dataset is a table of m rows and n features.
Dev: Function argumentsWatch out: More features isn't automatically better. Irrelevant ones add noise.
Notationx = (x1, x2, …, xn) · X has shape (m, n)X = df[["hours_slept", "lines_of_code"]]Targetoutput
The answer you want the model to predict. The model's guess is written ŷ ("y-hat"); the real answer is y.
Dev: Expected output in a unit testWatch out: Never let the target sneak into the features ("data leakage"). Your model will look perfect and fail in prod.
Notationy = real answer · ŷ = predictiony = df["bugs_shipped"]Linear regressionpredicts a number
Multiply each feature by a weight, add them up, add a bias. Training means finding the w and b that fit the data best.
Dev: A function with tunable config valuesWatch out: It assumes a roughly straight-line relationship. Curvy data needs extra features (e.g. x²).
Modelfw,b(x) = w · x + b = w1x1 + … + wnxn + bLinearRegression().fit(X, y)Logistic regressionpredicts yes / no
Same linear formula, then squashed by the sigmoid into a probability between 0 and 1. Above 0.5 means "yes".
Dev: Same function, plus an if-statementWatch out: Despite the name, it's a classification model, not regression.
Modelz = w · x + b → g(z) = 11 + e−zpredict 1 if g(z) ≥ 0.5, else 0Cost functionhow wrong
One number that sums up how wrong the model is across every example. Errors are squared so big misses count much more than small ones.
Dev: Your failing test countWatch out: Linear and logistic use different costs. Squared error makes logistic's cost bumpy, so it uses log loss.
Linear: squared errorJ(w,b) = 12m Σi=1m (f(x(i)) − y(i))2Logistic: log lossL = −[ y log(ŷ) + (1 − y) log(1 − ŷ) ]Gradient descenthow it learns
Repeat: work out which direction lowers the cost (the gradient), take a small step that way. Stop when the cost stops dropping.
Dev: A for loop: nudge, rerun, repeatWatch out: Update w and b at the same time, using the old values for both.
Repeat until convergedw := w − α ∂J∂w b := b − α ∂J∂b∂J∂w = 1m Σ (f(x(i)) − y(i)) x(i) ∂J∂b = 1m Σ (f(x(i)) − y(i))Learning ratestep size
α controls how big each step is. Too big and the cost bounces or explodes; too small and training crawls.
Dev: Step size per iterationWatch out: Plot cost vs iterations. It should go down every time. If it goes up, α is too big.
Values to try (≈3× apart)α ∈ { 0.001, 0.003, 0.01, 0.03, 0.1, 0.3 }cost rising → α too big cost flat-ish → α too smallFeature scalingsame range
Rescale features so they cover similar ranges. Otherwise one large-valued feature dominates and gradient descent zigzags slowly.
Dev: Converting ms and seconds to one unitWatch out: Fit the scaler on training data only, then apply the same μ and σ to test data.
Standardisation (z-score)x′ = x − μσ mean 0, most values in −3…3Min-maxx′ = x − minmax − min range 0…1SGDfast updates
Stochastic gradient descent updates after one random example (or a mini-batch of 32–256) instead of the whole dataset. Each step is noisier, but far faster.
Dev: Run one test, fix, repeatWatch out: A jumpy cost curve is normal. Shuffle the data every epoch (one full pass).
Update per example iw := w − α (f(x(i)) − y(i)) x(i)SGDRegressor().fit(X_scaled, y)
Andrew Ng's ML course
Supervised Machine Learning: Regression and Classification
Start here. This is where all nine terms come from, taught slowly with quizzes and labs. Weeks 1–3 cover everything on the cheat sheet.
- Weeks 1–3
- Enroll free on Coursera
- Python labs
- Week 1: Linear regressionSupervised learning, the cost function and gradient descent
- Week 2: Multiple featuresVectorisation, feature scaling and choosing a learning rate
- Week 3: Logistic regressionClassification, the sigmoid, decision boundaries and the logistic cost
Watch it clicked
Josh Starmer · short, visual, a little silly
When a formula doesn't click, watch the matching video. Each one draws the idea out step by step in 10–20 minutes.
- 10–20 min each
- Free
- Linear regression, clearly explainedFitting the line and measuring how good it is
- Gradient descent, step by stepThe downhill walk, with the maths drawn out
- Logistic regressionFrom a straight line to a yes/no probability
- Stochastic gradient descentWhy using one example at a time is faster
ML Crash Course
Short interactive lessons from Google
Finish here. Drag the sliders yourself and watch the cost go up and down, then take the quiz at the end to check you really get it.
- ~2 hours
- Interactive
- Free
- LossWhat the cost function measures, and why errors are squared
- Gradient descentPlus an interactive exercise to play with it
- HyperparametersLearning rate, batch size and epochs, i.e. where SGD comes in
- Logistic regressionThe sigmoid and predicting probabilities