Vectors and Weights

Pack sqft, bedrooms, and age into a feature vector, pair it with one weight per feature, and predict with a dot product.

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.

sqftbedsageprice
H11200215$245,000
H2180038$310,000
H3220043$420,000
H4900240$180,000
H51500310$295,000
Features in, weighted sum out
sqft1500beds3age10Dot productΣ xᵢ·wᵢPredictionprice $× w₁× w₂× w₃guess
  • in play
StatusStart from one feature

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.

Step 1 of 4

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.”

FeatureExample weightMeaning
sqft+120each sqft adds about $120
bedrooms+25,000each bedroom adds about $25k
age−2,000each 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.

vectors-and-weights.tsTypeScript
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)); // 235000

Lengths 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.

One house → one prediction
sqft1500
beds3
age10
sum
StatusOne house, three features

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

Training & 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.

Build model → Training → Inference
Build modeldot existsTrainingupdate all wᵢInferencevector frozen
  • current phase
StatusBuild model

Features are a vector, weights are a vector, predict is their dot product. Nothing has learned yet.

Step 1 of 3
This page stops before training. You now have the representation and the prediction formula. The next example — Linear Regression — teaches all three weights plus a bias from HOUSES end to end.

Keep reading

TopicDescription
Machine LearningHow a model improves from examples: labels, features, training, and the difference between fitting data and predicting on new data.
Your First Learning ModelOne feature, one weight, and a training loop that discovers dollars per square foot from a sold house.
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.