What we are building
The first learning model only saw square footage. Real houses bring more than one number — size, bedrooms, age — and the model needs a weight for each.
A vector is an ordered list of numbers. That is the whole idea. [1500, 3, 10] is a vector of three features for one house; [120, 25000, -2000] is a vector of three weights. Same length, same order, slot for slot.
- Vector
- An ordered list of numbers — in TypeScript, usually a
number[]. Order matters: index 0 is always the first feature, index 1 the second, and so on. - Feature vector
- The inputs for one example, packed together — here
[sqft, bedrooms, age]. - Weight vector
- One learned multiplier per feature, in the same order — here
[w₁, w₂, w₃]. - Dot product
- Multiply each matching pair, then add those products into one number. That number is the prediction.
This example builds a feature vector, pairs it with a weight vector, and predicts with that one operation. Same Learning overview story — build the model, train later, infer with the weights frozen.
Build model
From one number to a vector
Purpose: estimate sale price from three features at once. The shape is price ≈ w₁·sqft + w₂·beds + w₃·age. Each feature keeps its own weight; prediction is still a single number.
Dataset: five sold houses. Each row is one feature vector [sqft, bedrooms, age] paired with a sale price — five vectors, five answers.
| sqft | beds | age | price | |
|---|---|---|---|---|
| H1 | 1200 | 2 | 15 | $245,000 |
| H2 | 1800 | 3 | 8 | $310,000 |
| H3 | 2200 | 4 | 3 | $420,000 |
| H4 | 900 | 2 | 40 | $180,000 |
| H5 | 1500 | 3 | 10 | $295,000 |
- in play
You already know this shape from the first model: one input, one weight, one multiply. That is still correct — it is just incomplete for a real house.
Why one weight per feature
Each weight is the model’s opinion about that feature alone. Positive pushes price up; negative pulls it down; near zero means “this barely matters.”
| Feature | Example weight | Meaning |
|---|---|---|
| sqft | +120 | each sqft adds about $120 |
| bedrooms | +25,000 | each bedroom adds about $25k |
| age | −2,000 | each year older subtracts about $2k |
These example weights are fixed so you can see the arithmetic. In a full training loop the model would move all three from data — same idea as the single weight in Your First Learning Model, just longer.
Solution in TypeScript
In code the vector is just number[]. type Vector = number[] is a name for that list — nothing fancier. dot multiplies matching pairs and adds them. predict is that one call.
type Vector = number[];
// [sqft, bedrooms, age] → price ($)
const HOUSES: [Vector, number][] = [
[[1200, 2, 15], 245_000],
[[1800, 3, 8], 310_000],
[[2200, 4, 3], 420_000],
[[ 900, 2, 40], 180_000],
[[1500, 3, 10], 295_000],
];
function dot(a: Vector, b: Vector): number {
if (a.length !== b.length) {
throw new Error("vectors must match length");
}
return a.reduce((sum, ai, i) => sum + ai * b[i], 0);
}
function predict(house: Vector, weights: Vector): number {
return dot(house, weights);
}
const house = [1500, 3, 10];
const weights = [120, 25_000, -2_000]; // illustrative, not learned yet
console.log(predict(house, weights)); // 235000Lengths must match. Three features need three weights. Add a garage flag later and you need a fourth weight — nothing else in the formula changes.
Prediction
Walk the dot product
Take house H5 — [1500, 3, 10] — and the illustrative weights [120, 25000, -2000]. Step through each multiply, then the sum.
The same 1500 sqft house from the first example now also carries bedroom count and age. That ordered list is the feature vector.
What happens in this step
house = [1500, 3, 10]
sqft beds ageTraining & inference
Same phases, longer weight vector
Building the model meant writing predict = dot. Training will nudge every weight when a house is wrong. Inference freezes the whole vector and scores new listings.
- current phase
Features are a vector, weights are a vector, predict is their dot product. Nothing has learned yet.
HOUSES end to end.