Hands-On Practice

Practice Project: Predicting House Prices with Linear Regression

7 min read

From theory to a real dataset

Earlier in this course, linear regression showed up as one line in a list: "predicts a continuous numeric value based on its relationship to other variables." That's true, but it doesn't show what the work actually looks like.

This lesson walks through a complete, realistic linear regression project end to end — the same shape of project a data scientist works through dozens of times a year. The scenario: a real estate agent wants a model that takes a few facts about a region and returns an estimated house price.

Dataset: USA_Housing.csv — 5,000 rows of made-up (but realistic-looking) regional housing data: average area income, average house age, average number of rooms and bedrooms, area population, address, and the sale price we're trying to predict.

Full notebook: 01-linear-regression-with-python.ipynb — download it, open it in Jupyter, and run every cell yourself alongside this lesson.


Step 1: Load and inspect the data

Every project starts the same way — get the data into a table and look at it before doing anything else.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

USAhousing = pd.read_csv('USA_Housing.csv')
USAhousing.head()
USAhousing.info()
USAhousing.describe()

head() shows the first five rows so you can eyeball the shape of the data. info() lists every column, its data type, and whether any values are missing. describe() gives quick statistics — mean, min, max, standard deviation — for every numeric column. None of this trains a model yet; it's just making sure the data is what you think it is before you trust it.

Check your understanding

Before training any model, why run head(), info(), and describe() first?


Step 2: Separate features from the target

A supervised model needs two things: the inputs it's allowed to learn from (the features, X) and the answer it's trying to predict (the target, y).

X = USAhousing[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
               'Avg. Area Number of Bedrooms', 'Area Population']]
y = USAhousing['Price']

Notice Address isn't in X. It's a free-text column — "1234 Main St, Anytown" — and linear regression only understands numbers. A column a model can't mathematically use has to be dropped or transformed before training, so here it's simply left out.


Step 3: Split into training and test sets

If a model is graded on the same data it studied, a high score doesn't prove much — it might have just memorized the answers. So the data gets split: one portion to train on, a separate portion the model never sees until it's time to grade it.

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=101)

test_size=0.4 holds out 40% of the rows for testing, training on the remaining 60%. random_state=101 just makes the split reproducible — anyone who runs this exact code gets the exact same split, which matters for comparing results later.

Check your understanding

If USAhousing has 5,000 rows and test_size=0.4, roughly how many rows end up in X_test?


Step 4: Create and train the model

This is the part that earlier lessons described conceptually — a model studying labeled examples until it learns the mapping between inputs and outputs. In code, it's remarkably short:

from sklearn.linear_model import LinearRegression

lm = LinearRegression()
lm.fit(X_train, y_train)

LinearRegression() creates an untrained model. .fit(X_train, y_train) is the training step — this is where the model looks at thousands of labeled examples (X_train paired with the correct answers in y_train) and works out how each feature relates to price.


Step 5: Read the coefficients

Once trained, a linear regression model has learned one number per feature — a coefficient — describing how strongly that feature pushes the predicted price up or down.

coeff_df = pd.DataFrame(lm.coef_, X.columns, columns=['Coefficient'])
coeff_df

Interpreting a coefficient: holding every other feature fixed, a one-unit increase in Avg. Area Income is associated with roughly a $21.52 increase in predicted price. A one-unit increase in Avg. Area Number of Rooms is associated with a much larger jump — about $122,368.67 — because "rooms" moves in much bigger, more price-relevant increments than "income measured in dollars."

This is one of linear regression's biggest advantages over more complex models: it's directly interpretable. You can point at a specific number and explain exactly what it means to a non-technical stakeholder — try doing that with a neural network.


Step 6: Predict on the test set

Now the model gets to prove itself on the 2,000 rows it has never seen.

predictions = lm.predict(X_test)
plt.scatter(y_test, predictions)

predict() runs the trained model on X_test and returns a predicted price for every row. Plotting the real prices (y_test) against the predicted ones (predictions) is a fast sanity check — if the model were perfect, every point would fall on a straight diagonal line. In practice, real models scatter around that line, and how tightly they cluster gives an intuitive sense of accuracy before looking at any formal metric.


Step 7: Score it with real metrics

A scatter plot is a gut check, not a grade. Three standard metrics quantify exactly how far off the predictions were:

from sklearn import metrics

print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))
  • MAE (Mean Absolute Error) — the average size of the error, in the same units as price. Easiest to explain in plain language.
  • MSE (Mean Squared Error) — squares every error before averaging, which punishes big misses much more than small ones.
  • RMSE (Root Mean Squared Error) — the square root of MSE, which brings the number back into interpretable price units while still penalizing large errors more than MAE does.

Check your understanding

A model has a few predictions that are wildly wrong and many that are nearly perfect. Which metric will be most sensitive to those few big misses?


Key takeaway

A linear regression project follows a repeatable shape: load and inspect the data, separate features from the target, split into train and test sets, fit the model, then evaluate it — first by eye with a scatter plot, then formally with MAE, MSE, and RMSE. Every one of those steps maps directly onto a concept from earlier in this course; this lesson just showed what they look like as running code.

What's next?

Linear regression predicts a number. The next practice project tackles the other half of supervised learning — predicting a category — by building a logistic regression model that guesses whether a Titanic passenger survived.