Build garagePlay → read → next · ~10 min

Tutorials · Chapter D (4/4) · ~10 min

Train a tiny model

Play → read → next

Fit a simple model, then call `predict` on a new input — proof it learned.

Code Lab

Train a tiny model

Fit a simple line from toy examples, then predict.

Recap

What you just did

You fed examples into a learner, got back weights (or a fitted rule), and made a prediction on new data. That’s the showpiece: train once, reuse many times. Screenshot the fitted result + one new estimate.

Teach

How it works

Classic tiny fit: a line y ≈ w * x + b from a few points.

# toy: learn w,b so predictions ≈ labels
xs = [1, 2, 3, 4]
ys = [2, 4, 6, 8]   # roughly y = 2x

w, b = 2.0, 0.0     # after "fit" (lab finds these)
def predict(x):
    return w * x + b

print(predict(5))   # ~10 — the win you can show
  1. Examples (x, y) teach the pattern
  2. Fit searches for better w / b (or similar params)
  3. Predict uses the frozen params on new x

Mental model: training is practice; prediction is game day.

Use it

When you'd use this

  • Estimating house price from square footage (simple regression vibe)
  • Mapping study hours → likely quiz score
  • Any “given X, guess Y” once you have a table of past pairs

Watch out

Watch out

If you predict only on the same rows you trained on, you may be celebrating memorization. Hold out one number, fit without it, then predict that held-out point. Also: garbage labels → confident wrong fit.

Try next

Try this next

Change one training y wildly (an outlier). Refit. Does predict(5) move? That’s sensitivity you should know as a builder.