Module 15 · Predicting Numbers

Which used Civic is the better deal?

Lesson 15.1 · Draw Your Best Guess

Felix

Fifteen Civics, two price tags, one notebook.

Felix is at the lot with $6,000 and a notebook.

Fifteen Civics are for sale. Car A has 38,000 miles. Car B has 72,000.

The prices are close enough that Felix can’t tell which one is priced fair.

So Felix writes down every car’s miles and price and stares at the list.

Try it yourself

Drag the two handles so the line runs through the middle of the dots. That line is your guess at what a Civic should cost for its miles.

Drag the two handles so the line runs through the middle of the dots. That line is your guess at what a Civic should cost for its miles.1234A6789B1112131415AB

What you just did has a name

Linear regression

Drawing the best straight line through a cloud of dots is called linear regression.

It’s one of the oldest tools in machine learning, and a lot of price estimates you see online still start with it.

A few more things

The line is a prediction machine

Pick any number of miles, go up to the line, read across. That’s the price the pattern says a Civic should have. You just predicted a number you never saw.

“Best” means smallest misses

Every dot sits some distance above or below the line. The best line is the one where all those misses, added up, are as small as they can get. Not zero. Small.

The dots are the past

Fifteen cars that already have prices. The line learned from them. That’s the whole trick of machine learning: learn a pattern from what already happened, use it on what hasn’t.

Go deeper

The longer version, if you want it.

Least squares, for the curious

The line is y = a + bx, where x is miles and y is price. The slope b is how much the price changes per mile, and a is the price the line predicts at zero miles.

For each car, the miss is the real price minus the line’s prediction. Ordinary least squares picks a and b to make the sum of the squared misses as small as possible. Squaring keeps the misses positive and punishes big misses harder than small ones.

You never have to search for a and b. There is a formula: b = (the covariance of miles and price) ÷ (the variance of miles), and a = the average price minus b times the average miles. The app runs that exact formula when you tap Show the best line.

Quick check

Rosa has ten past bake sales written down: cookies baked and dollars made. She draws a line through the dots and reads off that 200 cookies should bring in about $95. What did she just do?

Let’s take it slower

Here’s the rule of thumb. If you have a pile of past pairs, this much of one thing went with that much of another, you can lay a ruler through the middle of them. That ruler is the regression line. Once it’s down, hand it any new value of the first thing and it hands back a guess for the second. It won’t be exact. It will be the least wrong straight line anyone could draw through those dots, and that is usually better than anyone’s gut.

More examples

1

Height from shoe size

A school nurse has heights and shoe sizes for 300 students. A line through them lets her guess a kid’s height from a shoe size alone, give or take an inch. Nobody had to measure anything new.

2

The 35-minute delivery

The arrival time on a food app comes from a line through thousands of past orders: distance in, minutes out. Your order isn’t in that data yet. The line guessed anyway, and it’s usually close.

Back to the question

So which Civic is the deal, A or B?

Show the answer

Car A sits below the line, so it costs less than the pattern says a car with its miles should. Car B sits above it. Car A is the better deal, and now Felix can say why instead of just feeling it.

One last question

Felix tries to explain regression to the crew. Who got it?

Talk Card

Talk Card · 15.1

Regression draws the best line through past data so you can predict a number, like a car’s price from its miles.

Data to AI · pettigrewlab.com/learn

Next lesson