Lesson 8 of 13 · How Models Learn
How a Model Makes Predictions
See exactly how three ML models turn features into predictions: a linear model's weighted sum, a decision tree's questions and nearest neighbours' examples.
- 5 min read
- Beginner
- Reviewed
- Explain how a linear model turns features into a prediction
- Follow a decision tree's questions to a prediction
- Explain how nearest neighbours predicts from similar examples
- Describe what “parameters” are and where they come from
A trained model is a recipe
Training produces a recipe for turning features into a prediction. Different kinds of model use different recipes, but each is simple once you see it. Here are three recipes predicting the price of the same house (90 m², 3 bedrooms, 20 years old):
house = {"size_m2": 90, "bedrooms": 3, "age_years": 20}
# ---- 1. A linear model: a weighted sum of the features ----
weights = {"size_m2": 2_500, "bedrooms": 8_000, "age_years": -1_200}
start = 40_000
price = start + sum(weights[f] * house[f] for f in weights)
print(f"Linear model: {start:,} + 2,500 x 90 + 8,000 x 3 - 1,200 x 20 = {price:,}")
# ---- 2. A decision tree: a series of yes/no questions ----
def tree(h):
if h["size_m2"] > 80:
if h["age_years"] < 30:
return 290_000
return 240_000
if h["bedrooms"] >= 2:
return 210_000
return 160_000
print(f"Decision tree: size > 80? yes. age < 30? yes. -> {tree(house):,}")
# ---- 3. Nearest neighbours: average the prices of the most similar houses ----
sold = [((85, 3, 25), 270_000), ((96, 3, 14), 300_000), ((60, 2, 40), 190_000), ((92, 4, 18), 310_000)]
def distance(a, b):
return sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5
target = (house["size_m2"], house["bedrooms"], house["age_years"])
nearest = sorted(sold, key=lambda s: distance(s[0], target))[:2]
average = sum(p for _, p in nearest) / 2
print(f"Nearest neighbours: {nearest[0][1]:,} and {nearest[1][1]:,} -> average {average:,.0f}")Recipe 1: a weighted sum
A linear model multiplies each feature by a weight and adds them up, plus a starting value:
price = 40,000 + 2,500 × size + 8,000 × bedrooms − 1,200 × age
Each weight says how much the prediction changes when that feature goes up by one: every extra square metre adds 2,500; every extra year of age takes away 1,200.
Recipe 2: a series of questions
A decision tree asks yes/no questions about the features, one after another, until it reaches an answer:
Trees are easy to explain (“it's over 80 m² and under 30 years old, so…”), and they handle situations that a weighted sum can't, such as “size matters a lot in the city but little in villages”.
Recipe 3: look at similar examples
Nearest neighbours doesn't build a formula at all. It keeps the training examples and, for a new house, finds the most similar ones and averages their prices. Its “parameters” are the stored examples themselves.
| Linear model | Decision tree | Nearest neighbours | |
|---|---|---|---|
| Learns | One weight per feature | A set of questions | Nothing: stores the examples |
| Prediction | Weighted sum | Follow the path | Average of similar examples |
| Easy to explain? | Yes: read the weights | Yes: read the path | Yes: show the similar examples |
| Weak spot | Only straight-line effects | Jumpy predictions; can memorise | Slow with lots of data; needs scaled features |
Common mistakes
Make a prediction by hand with a trained linear model's weights.
weights = {"size_m2": 2_000, "bedrooms": 10_000, "garden": 15_000}
start = 50_000
house = {"size_m2": 100, "bedrooms": 3, "garden": 1}
# A linear model predicts: start + weight x value, added up over every feature.
# Print the prediction with thousands separators, like: 123,456
How does a trained linear model make a prediction?
Summary
- A trained model is a recipe for turning features into a prediction, defined by its parameters.
- A linear model uses a weighted sum; a decision tree follows yes/no questions; nearest neighbours averages similar examples.
- Each recipe has strengths and weak spots, and all three are easy to explain.
- Weights describe patterns, not cause and effect.
Next: the most important problem in machine learning, overfitting and underfitting.
Finished reading? Tick it off to track your progress.