Your First Learning Model

One feature, one weight, and a training loop that discovers dollars per square foot from a sold house.

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.

The model we are about to train
Feature1500 sqftModel× weightPredictionprice $inputguess
  • in play
StatusStart with a blank weight

Weight begins at 0. Until training moves it, every prediction is zero — the model has no opinion yet.

Step 1 of 4

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.

first-learning-model.tsTypeScript
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)); // ~294837

The 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 climbing toward $/sqft
weight0
predict
error
StatusStart of training

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
Step 1 of 10

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.

Build model → Training → Inference
Build modelpredict existsTraininglabels + updatesInferenceweight frozen
  • current phase
StatusBuild model

You write predict and train. Nothing has learned yet — the weight is still blank.

Step 1 of 3
One multiply, two jobs. 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.

Keep reading

TopicDescription
Machine LearningHow a model improves from examples: labels, features, training, and the difference between fitting data and predicting on new data.
Vectors and WeightsPack sqft, bedrooms, and age into a feature vector, pair it with one weight per feature, and predict with a dot product.
Linear RegressionTrain a linear model with weights and bias on all five houses, watch MSE fall, then predict a new listing.
Gradient DescentWalk downhill on error: one weight on a loss bowl, then the same step on every knob — with a learning-rate dial.
A Single NeuronAdd ReLU to the weighted sum you already know — one neuron that can bend, and why XOR still needs a network.
Neural NetworksStack layers, run a forward pass, send blame backward — train XOR with hidden ReLU neurons and a linear output.