The whole point of building a model is to predict values you have never seen — let's do that.
Save
Complete lesson & earn 250 PX
EXERCISE
1The whole point of building a model is this moment — feeding it a value it has never seen and getting a prediction back.
Save
EXERCISE
2Extrapolation is the most tempting and dangerous mistake in regression — your model knows nothing about territory it has never seen.
Save
EXERCISE
3From raw data to validated prediction — here is every step a practitioner actually follows.
Save
Building a model without making predictions is like building a car and never driving it. The model''s job is . Everything before this was preparation.
The prediction formula:
predicted_y = slope × new_x + intercept
from scipy import stats
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
slope, intercept, r, p, std_err = stats.linregress(x, y)
def predict(age):
return slope * age + intercept
# Predict speed of a 10-year-old car
print(f"10 years: {round(predict(10), 2)} km/h")
# Output: 10 years: 85.59 km/h
# Predict speed of a 3-year-old car
print(f"3 years: {round(predict(3), 2)} km/h")
# Output: 3 years: 97.85 km/h
# Predict speed of a 15-year-old car
print(f"15 years: {round(predict(15), 2)} km/h")
# Output: 15 years: 76.84 km/h
> 💡 Key Insight: A prediction is only as good as the model behind it. Always report the r-value alongside your prediction. Saying "a 10-year-old car goes 85.6 km/h" is incomplete. Saying "85.6 km/h (r = -0.76)" tells the listener how much to trust that number.
Your model learned from cars aged 2–17 years. Predicting speed for a 50-year-old car is like asking someone who has only visited Paris to describe Tokyo — they will give you an answer, but it is pure imagination.
Interpolation vs Extrapolation:
from scipy import stats
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
slope, intercept, r, p, std_err = stats.linregress(x, y)
def predict(age):
return slope * age + intercept
# SAFE: interpolation (within 2-17 range)
print(f"Age 10: {round(predict(10), 1)} km/h")
# Output: Age 10: 85.6 km/h ✅ Reasonable
# DANGEROUS: extrapolation (way outside range)
print(f"Age 50: {round(predict(50), 1)} km/h")
# Output: Age 50: 15.5 km/h ⚠️ Nonsensical
print(f"Age 100: {round(predict(100), 1)} km/h")
# Output: Age 100: -72.0 km/h ❌ Negative speed?!
| Type | Definition | Risk |
|---|---|---|
| Interpolation | Predicting WITHIN the data range | Low — model has evidence |
| Extrapolation | Predicting OUTSIDE the data range | High — model is guessing |
> 💡 Key Insight: Linear models extend their line infinitely in both directions — they will predict negative speeds, negative prices, and other nonsense if you let them. In production ML, always clamp predictions to sensible ranges and log warnings when inputs fall outside training bounds.
In real jobs, nobody just runs linregress() and ships the result. There is a checklist every ML practitioner follows to make sure the prediction is trustworthy.
The complete workflow:
import matplotlib.pyplot as plt
from scipy import stats
# Step 1: Gather data
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]
# Step 2: Visualise (scatter plot)
plt.scatter(x, y)
plt.title("Step 2: Look before you model")
plt.show()
# Output: Scatter plot showing a downward trend
# Step 3: Fit the model
slope, intercept, r, p, std_err = stats.linregress(x, y)
# Step 4: Check the r-value BEFORE trusting predictions
print(f"R-value: {round(r, 4)} (|r| = {round(abs(r), 4)})")
# Output: R-value: -0.7586 (|r| = 0.7586)
# |r| > 0.7 → acceptable for predictions ✅
# Step 5: Make predictions (within data range only)
def predict(age):
return slope * age + intercept
print(f"Prediction for age 10: {round(predict(10), 1)} km/h")
# Output: Prediction for age 10: 85.6 km/h
# Step 6: Visualise the fit
mymodel = list(map(predict, x))
plt.scatter(x, y)
plt.plot(x, mymodel, color="red")
plt.title(f"Step 6: R={round(r, 2)}")
plt.show()
# Output: Scatter plot with regression line overlaid
The practitioner''s checklist:
> 💡 Key Insight: The best ML practitioners are not the ones who know the fanciest algorithms — they are the ones who follow rigorous workflows. Check, visualise, validate, then predict. Skipping steps is how bad models make it into production and cause real damage.
Use your fitted model function to predict outcomes for new inputs. Always check the r-value first to know if predictions are trustworthy.