Data Science and Data Scientist

Module 27 of 43

Module 27: Supervised Learning — Regression

4 min read774 words
What you'll learn
Explain what regression predictsUnderstand the line of best fitRecognize regression problems

"Regression answers 'how much?' — predicting a number by finding the line (or curve) that best fits your data."

Learning Objectives

By the end of this module, you will be able to:

  • Explain what regression predicts
  • Understand the line of best fit
  • Recognize regression problems

1. Predicting Numbers

Regression predicts a continuous number — a price, a temperature, a sales figure. It's supervised learning where the label (y) is a number.

Key idea: Simple regression fits a straight line of best fit through your data — the line that sits as close as possible to all the points. Once you have the line, you can read off a prediction for any new input.

Explain like I'm new: A continuous number is one that can land anywhere on a scale — $312,450 or 21.7°C — not just in fixed buckets. If your answer is a quantity you could measure with a ruler or a thermometer, you're in regression territory. If it's a category (yes/no, cat/dog), that's the next module.

Some quick sanity checks help you recognize a regression problem in the wild. Ask, "Would the answer make sense with a decimal or a unit attached?" Predict tomorrow's temperature → 22.4°C, yes. Predict a house's price → $318,000, yes. Estimate delivery time in minutes → yes. Now compare: Is this email spam? → the answer is a yes/no, not a quantity, so that's classification instead. The dividing line is simply whether you're predicting how much / how many (regression) or which category (classification). Getting this distinction right is the first decision in any supervised project.

2. How It Works (Gently)

Picture a scatter plot of house size vs. price. Regression draws the line that minimizes the total distance to all the dots. That line becomes your predictor: give it a size, it returns a price.

python
[object Object], sklearn.linear_model ,[object Object], LinearRegression
model = LinearRegression()
model.fit(X_train, y_train)      ,[object Object],
model.predict(X_new)             ,[object Object],

Read that code as a sentence: create a model, let it fit (learn) the line from training examples, then predict a number for new inputs. Almost every scikit-learn model follows this same fit then predict rhythm, so learning it once carries you a long way.

What does "best fit" actually mean? For each data point, there's a gap between where the point sits and where the line predicts it should be — that gap is the error for that point. Regression chooses the one line that makes the total error as small as possible across all points (technically, it minimizes the squared errors, which just means big misses are penalized more than small ones). You don't have to compute any of this by hand — the library does it in a fraction of a second. But holding the intuition, "the line that's collectively closest to every dot," is what makes the results feel understandable rather than magical.

Simple example: Predicting a taxi fare from trip distance is regression — more distance, higher fare, roughly along a line. Feed the model past trips and it predicts the fare for a new one.

Real-world use case: A café wants to staff correctly. Using features like day of week and weather, a regression model predicts how many coffees they'll sell tomorrow — a number, say 340. The manager rounds up staffing to match. When the answer you need is a quantity to plan around, regression is the natural fit.

3. Beyond Straight Lines

When the trend curves, polynomial regression fits a curve instead. But beware bending too hard to fit every point (overfitting, Module 31). A line that wiggles through every single training dot usually memorizes noise rather than learning the real pattern — and then it stumbles badly on new data.

Common mistake: Forcing a straight line onto data that clearly curves (or vice versa). A model that's too simple underfits and misses the shape; one that's too flexible overfits. The right complexity sits between them — and a quick plot usually reveals which side you're on.

Data scientist tip: Always plot your regression line over the data. If the line clearly misses the shape (a curve forced into a straight line), your model is too simple — the picture tells you instantly.

Key Takeaway: Regression is supervised learning that predicts a number by fitting the line of best fit (or a curve) through the data, then reading predictions off it. It suits problems like prices, fares, and temperatures. Plot the fit to sanity-check it, and watch for over-bending.

Further Learning

Part of the "Data Science and Data Scientist" course.