What we are building
A house-price model with one input and one learned number: square footage goes in, a price comes out, and the dollars-per-square-foot weight is discovered from a sold house instead of typed by hand.
This is the smallest version of the loop from the Learning overview: build a model, train it on examples, then run inference on new listings. The vocabulary — model, feature, weight, training, and inference — is the same; here it becomes a few lines of TypeScript.
Build model
One feature, one weight
Purpose: estimate sale price from size alone. The shape is price ≈ sqft × weight. You do not pick the weight. Training will.
Dataset for this walkthrough: one sold house — 1500 sqft for $295,000. That single pair is enough to teach the idea. Real datasets have many rows; the update rule does not change.
- in play
Weight begins at 0. Until training moves it, every prediction is zero — the model has no opinion yet.
Solution in TypeScript
predict is the model. train is one correction. The loop is training. After the loop, you only call predict — that is inference.
class SimpleLearner {
weight = 0;
predict(sqft: number): number {
return sqft * this.weight;
}
train(sqft: number, actualPrice: number, learningRate = 0.0000001): void {
const prediction = this.predict(sqft);
const error = actualPrice - prediction;
this.weight += error * learningRate;
}
}
const model = new SimpleLearner();
const sqft = 1500;
const price = 295_000;
for (let step = 0; step < 50_000; step++) {
model.train(sqft, price);
}
console.log(model.weight.toFixed(2)); // ~196.56
console.log(model.predict(1500).toFixed(0)); // ~294837The learning rate is tiny on purpose. Sale prices are hundreds of thousands; without a small step size the weight would thrash. The overview covers why that knob matters.
Training
Three steps, then the curve
Each training step does the same three moves: predict, measure error, nudge the weight. Walk the first three by hand, then watch the chart climb across tens of thousands of steps toward ~$197/sqft.
With one house, each step is a tiny epoch of size one. When you later train on five houses, an epoch means one full pass through all five — same update rule, more examples per pass.
Weight begins at 0. The model has never seen a house. One sold listing is enough to start: 1500 sqft sold for $295,000.
What happens in this step
weight = 0 sqft = 1500 actual = 295000 learningRate = 0.0000001
Inference
Freeze the weight, then predict
Training had sale prices and kept moving the weight. Inference does not. A new 2000 sqft listing has no label yet — you call predict once and ship the number.
- current phase
You write predict and train. Nothing has learned yet — the weight is still blank.
predict runs in both training and inference. Training wraps it with an error and an update. Inference leaves the weight alone. Mixing those up is how a team ships a model that looks brilliant on yesterday’s sales and fails on the first live listing — the same trap the
overview warns about.