Sub topic 1
What We Are Predicting and Why It Is a Regression Problem
This chapter frames the project before any code runs. You decide what the model predicts, what kind of problem that is, and how you will know whether it works. Later chapters build on these choices.
Prerequisites
This chapter assumes you have:
- Basic Python: functions, imports, lists and dictionaries.
- Basic pandas: DataFrames, selecting columns, and
describe. - The idea of supervised learning: a model learns from rows that have features (inputs) and a target (the answer).
- A Python environment with numpy, pandas, matplotlib and scikit-learn installed.
The question we are asking
We want to predict the median house value of a district in California from facts about that district. The dataset has one row per census block group, which is a small area of a few hundred to a few thousand people. The features include median income, average house age, average number of rooms, location, and population. The target is the median house value for that area, stored in a column named MedHouseVal. Its unit is $100,000, so a value of 2.5 means $250,000.
A short way to state the project:
- Input: eight numeric facts about a district.
- Output: one number, the median house value.
- Goal: predictions close to the true values on districts the model has never seen.
Why this is regression
Supervised problems fall into two main groups, based on the target.
| Kind | Target | Example |
|---|---|---|
| Classification | A label from a fixed set | Is this email spam or not? |
| Regression | A continuous number | What is this district's median house value? |
House value can take almost any number in a range, and being off by a little is better than being off by a lot. That makes this a regression problem. If we had split houses into "cheap" and "expensive" and predicted the label, it would be classification. We would also throw away detail. Keeping the number is the better choice here.
Defining success before modeling
Decide how you will judge the model before you train anything. Otherwise you may pick a metric that flatters whatever you built. For this project we use:
- Mean absolute error (MAE): the average size of the miss, in the target's own unit. An MAE of 0.5 means predictions are off by about $50,000 on average. It is easy to explain.
- R²: the share of the target's variation that the model explains. A model that always predicts the mean scores 0. A perfect model scores 1.
Chapter 4 covers these metrics, and RMSE, in more detail. For now, note the key idea: success is a number you can compare across models, measured on data the model did not train on.
How the project fits together
Each step depends on the one before it. The test set is set aside early and used only at the end, which is why the final score can be trusted.
A first look at the target
You can confirm the framing with a few lines. This code loads the data and checks that the target is numeric and continuous. It downloads the data on the first run.
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing(as_frame=True)
df = housing.frame
print(df["MedHouseVal"].dtype)
print(df["MedHouseVal"].describe())
print("Distinct target values:", df["MedHouseVal"].nunique())The dtype is float64, and there are thousands of distinct values. A classifier needs a small set of labels, so this target does not fit one.
Common mistakes at the framing stage
- Treating a numeric target as classes. This discards information.
- Skipping the metric choice, then picking whichever score looks best afterward.
- Forgetting the unit. Reporting an error of 0.4 without saying it means $40,000 confuses readers.
Exercise
A colleague wants to predict whether a district's median house value will be above $300,000. Is that regression or classification, and which framing keeps more information from this dataset?
Show answer
Predicting "above $300,000 or not" is classification, because the output is one of two labels. Predicting the value itself is regression and keeps more information, since you can still apply the $300,000 threshold to the predicted number afterward.