Skip to main content

The Prediction Game

ML Prediction Concept - Input to Output

Starting With Something You Already Know

Forget Python. Forget libraries. Forget math notation. Let’s play a game.

Round 1: The House Price Game

You’re a real estate agent. A client asks: “How much is this house worth?” They give you some info: What’s your guess?

Your Brain’s Algorithm

Without realizing it, you do this:
  1. Think of similar houses you’ve seen
  2. Remember what they sold for
  3. Adjust based on differences
  4. Make a guess
That’s machine learning. You learned patterns from past data and applied them to new data.
Real World ML - Email Spam Filtering

Round 2: Let’s Be More Systematic

What if I told you the average house in your area sells for:
  • Base price: $200,000
  • Each bedroom adds about $25,000
  • Each bathroom adds about $15,000
  • Each square foot adds about $150
Now you can compute:
You just built your first linear model!
The formula you just used:price = base + (bedrooms × weight1) + (bathrooms × weight2) + (sqft × weight3)Those “weights” (25k,25k, 15k, $150) are what machine learning learns automatically from data.

Let’s Code It (Still No Libraries!)


The Million Dollar Question

But wait… how did we know those weights?
  • Why 25,000perbedroomandnot25,000 per bedroom and not 30,000?
  • Why 150persqftandnot150 per sq ft and not 200?
We guessed. And our guesses might be wrong. Machine learning answers this: Given a bunch of houses with known prices, can we figure out the best weights automatically?

Real Data, Real Problem

Here’s actual data (simplified):
Our goal: Find weights that make our predictions match these actual prices as closely as possible.

Step 1: How Wrong Are We?

If we use our guessed weights, let’s see how we do:
Output:
We’re consistently too high! Our weights are off.

Step 2: Measure Total “Wrongness”

We need a single number that tells us how wrong we are overall. Simple approach: Sum of all errors
Problem: What if some errors are positive and some negative? They cancel out! Better approach: Sum of squared errors
This is called the Loss Function or Cost Function. Lower is better!
Why squared? Think of it like grading a student’s exam:
  1. No negative numbers — errors can’t cancel out (a +50Kovershootshouldnt"forgive"a50K overshoot shouldn't "forgive" a -50K undershoot)
  2. Big errors get penalized more — being off by 100Kismorethantwiceasbadasbeingoffby100K is more than twice as bad as being off by 50K. Squaring enforces this: 100K2=10B100K^2 = 10B vs 50K2=2.5B50K^2 = 2.5B (a 4x penalty for a 2x error)
  3. Smooth and differentiable — the curve has no sharp corners, so gradient descent can glide smoothly toward the minimum (we’ll need this in Module 2)
There are alternatives — Mean Absolute Error (MAE) treats all errors equally and is more robust to outliers. But MSE is the default starting point because its math is cleaner and it punishes the predictions you’re most embarrassingly wrong about.

Step 3: Try Different Weights

What if we try different values?
The challenge: There are infinite combinations of weights. How do we find the best ones?
What if we:
  1. Start with random weights
  2. Check how wrong we are
  3. Slightly adjust weights
  4. If error goes down, keep the change
  5. Repeat until error stops improving
This is the core idea behind Gradient Descent - which we’ll explore in the next module! Think of it like tuning a radio dial in the dark. Random search is spinning the dial blindly and hoping for a good station. Gradient descent is listening to the static — when it gets quieter, you keep turning that direction.

What You Just Learned

Let’s recap with proper ML terminology:

The Mathematical Connection

When you calculated:
In math notation, this is: y^=w0+w1x1+w2x2+w3x3\hat{y} = w_0 + w_1 x_1 + w_2 x_2 + w_3 x_3 Or in matrix form (from our Linear Algebra course): y^=wx\hat{y} = \mathbf{w} \cdot \mathbf{x} This is a dot product - the same operation you do when calculating weighted grades!

🚀 Mini Projects

Project 1

Build a house price estimator from scratch

Project 2

Create a used car valuation tool

Project 3

Visualize prediction errors and find patterns

Key Takeaways

ML is Pattern Matching

Find patterns in past data, apply to new data

Weights Capture Knowledge

The learned weights encode what matters

Loss Measures Wrongness

Lower loss = better predictions

Training = Minimizing Loss

Find weights that make predictions best match reality

Practice Challenge

Try this on your own:
Key insight: Unlike houses where more is usually better, for cars:
  • Older cars are worth less (negative weight for age)
  • Higher mileage is worth less (negative weight for mileage)
  • More horsepower is worth more (positive weight)
Try something like:

Next Up

In the next module, we’ll learn:
  • How to systematically find the best weights (not just random guessing)
  • The key insight of gradient descent - following the slope downhill
  • How this connects to calculus

Continue to Module 2: Learning From Mistakes

Discover gradient descent - the algorithm that powers all modern ML

🔗 Math → ML Connection

What you learned in this module connects to formal ML:Next module: We’ll replace “random guessing” with a systematic approach called gradient descent - the same algorithm that trains ChatGPT!

🚀 Going Deeper (Optional)

For learners who want the formal treatment:

Matrix Formulation

What we wrote as:
Can be written in matrix form as: y^=Xw\hat{y} = X \mathbf{w}Where:
  • XX is the feature matrix (each row is a house, each column is a feature)
  • w\mathbf{w} is the weight vector
  • y^\hat{y} is the prediction vector

Why Squared Error?

We use squared error (not absolute error) because:
  1. It’s differentiable - we can compute gradients (needed for Module 2)
  2. It penalizes large errors more - a 100Kerrorisworsethantwo100K error is worse than two 50K errors
  3. It leads to closed-form solutions in linear regression

Closed-Form Solution

For linear regression, there’s actually a formula that gives optimal weights directly: w=(XTX)1XTy\mathbf{w}^* = (X^T X)^{-1} X^T yWe’ll derive this in Linear Regression module.