Skip to content
Tutorials Center
My learning

Topics

Lesson 66 of 76 ยท Projects

Project: House Price Model

Build a house price model end to end: audit synthetic housing data, engineer features, compare regression models with cross-validation, and study residuals.

Predicting house prices is the classic regression project, and for good reason: the target is a number everyone understands, the features (size, age, location) are intuitive, and the mistakes are easy to see. In this project you will build the whole thing yourself, from a messy table to a model you can defend. The data is synthetic, generated with a fixed seed, so you get the same numbers every time and we know the true rules that created the prices. That last point is a luxury real projects never give you.

The plan follows the data science workflow: audit the data, engineer a few features, compare several models fairly with cross-validation, pick one, test it once on held-out data, and finally study its mistakes. The last step, residual analysis, is the one beginners skip, and it is where you learn what to do next.

flowchart LR
    A["Audit and clean"] --> B["Engineer features"]
    B --> C["Compare models<br/>(cross-validation)"]
    C --> D["Pick one model"]
    D --> E["Test once<br/>on held-out data"]
    E --> F["Residual analysis"]
    F -. "ideas for next round" .-> B

The project loop: the residual analysis at the end feeds ideas back into the features.

Step 1: look at the data before modelling

Our table has 600 houses with area in square metres, bedrooms, age in years, distance to the city centre, a neighborhood, a garage flag and the sale price. We deliberately planted two real-world problems: 12 missing ages and one impossible area (4,000 square metres for a house with four bedrooms). A model fitted on this table without a look would still run and still print a score, which is exactly why you must look first. See Auditing Data Quality for the full checklist.

Python Audit the housing table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the output like a detective. The maximum area is 4,000 while the median is 102, and the row in question has a price of only 55,900: a data-entry error, not a mansion. The ages are missing in 12 rows. And the price is strongly right-skewed (skewness 4.84), while its logarithm is far more symmetric (0.7). That last fact is a hint for the modelling step: house prices tend to change by percentages, not by fixed amounts, so a model of the log price is a natural candidate.

What to do with the problems

We will make two decisions and write them down:

  • The impossible area is set to missing. We do not delete the row, because the other columns are fine, and we do not guess a value here.
  • Missing values (ages and the area) are imputed with the median inside the modelling pipeline. If we filled them in before splitting the data, statistics from the test rows would leak into training. Doing it in the pipeline means each cross-validation fold learns its own medians from its own training rows only. See Handling Missing Values and Outliers, Errors and Impossible Values.

Step 2: engineer a few features

Raw columns are rarely the best inputs. We add two features that reflect domain knowledge:

  • area_per_bedroom: how spacious the rooms are, which a plain linear model cannot build from area and bedrooms by itself.
  • is_new: whether the house is five years old or younger, because new construction often sells at a premium that is not a straight line in age.

The neighborhood is a category, so it is one-hot encoded: one 0/1 column per neighborhood. Keep the feature list short and justified; each feature should have a reason, not just a hope. More ideas are in Feature Engineering and Encoding and Scaling Features.

Step 3: compare models fairly

Now the main event. We compare five candidates with 5-fold cross-validation, which splits the training data into five parts, trains on four and scores on the fifth, five times. Every model sees the same folds and the same preprocessing, and every score is computed on rows the model did not train on.

The candidates:

  1. Mean baseline. Always predicts the average price. Any model that cannot beat this is useless, and it shows what R2 near zero looks like.
  2. Linear regression on the price.
  3. Linear regression on the log price. The same model, but it learns percentage effects.
  4. Random forest. An ensemble of trees that finds nonlinear patterns.
  5. Gradient boosting. Another tree ensemble that builds trees one after another, each fixing the errors of the previous ones.
Python Cross-validated comparison of five models

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The results are a lesson in themselves:

Model RMSE MAE R2
Mean baseline 52,876 33,196 -0.007
Linear 25,893 15,702 0.769
Linear, log target 12,323 8,510 0.942
Random forest 25,346 12,641 0.777
Gradient boosting 26,937 12,908 0.749

Three things stand out. First, the baseline's R2 is about zero, as it must be. Second, the winner is the humble linear model on the log price, with errors less than half those of the fancy ensembles. The reason is that our synthetic prices are built from percentage effects, and the log target makes the problem exactly linear. The trees have to approximate that smooth multiplicative surface with many steps, and with only 480 training rows per fold they do not have enough data to do it well. Third, look at the "R2 spread" column: the tree models vary a lot between folds (0.11 to 0.12) while the log-linear model is stable (0.01). A model that is both more accurate and more stable is an easy choice.

Tip: the lesson is not "linear models beat trees". It is "the right shape of the problem beats a fancier algorithm". On real data you cannot know the shape in advance, which is why you compare. Try adding TransformedTargetRegressor around the forest and the boosting model as an exercise: trees on the log price usually improve too.

Step 4: test once, then read the residuals

We pick the log-linear model and now use the held-out 20 percent of the houses, which no model has seen, exactly once. If we peeked at the test set while choosing, the score would no longer be an honest estimate (see Train/Test Splits and Cross-Validation).

A residual is the prediction error: predicted minus actual. Because house prices span a wide range, we express errors as a percentage of the actual price, so a 10,000 miss on a cheap house counts as more than the same miss on an expensive one. We then look for structure in the errors. Random noise around zero is what a good model leaves behind; patterns mean the model is missing something.

Python Final test and residual analysis

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On the held-out houses the model reaches R2 = 0.954 and a mean absolute error of about 8,268. The typical house is off by 7.3 percent, and the average error is only -1 percent, so there is no large overall bias. That agrees with the cross-validation estimate (R2 0.94), which is what a trustworthy pipeline should deliver.

What the residuals say

  • Predicted vs actual hugs the diagonal line across the whole price range. Points far from it are individual houses to inspect.
  • Error vs prediction should look like a horizontal band of noise. A funnel (errors growing with price) would mean the model needs a different target transform; a curve would mean a missing nonlinear term. Ours is a fairly even band, which confirms the log target was a good idea.
  • By neighborhood, Hillcrest shows an average error of -4.7 percent: the model tends to under-predict its prices a little. With only 28 Hillcrest houses in the test set and a spread of about 10 percent, this could be chance. A responsible next step is not to patch the model immediately, but to check whether the pattern also appears in cross-validated predictions on all 600 rows, then consider an interaction between neighborhood and area.
  • Largest errors are worth reading row by row. Often they are data problems (a wrong value) or special cases the features cannot describe (a house with a view). Here the biggest misses are about 20 to 30 percent: noisy but not catastrophic.

Because the data is synthetic, we know the true noise level is about 12 percent in log terms, so errors around 8 to 10 percent are close to the best any model could do. On real data you never know the floor, so compare against a baseline and against simpler models instead.

Report what you found

A short written summary closes every project. For this one it could read: "A linear model on the log of the price, with area, bedrooms, age, distance, garage and neighborhood, predicts held-out sale prices within about 7 percent for a typical house (R2 0.95). It beat random forest and gradient boosting in cross-validation, mainly because price effects are multiplicative. It may under-predict Hillcrest slightly, which we would investigate with more data. Two records had impossible or missing values and were imputed inside the pipeline." Notice that it names the metric, the baseline comparison, the main limitation and the data fixes. More advice is in Communicating Results to Non-Experts.

Ideas to extend the project

  • Wrap the forest and boosting models in the log-target transformer and compare again.
  • Tune the forest and boosting hyperparameters with grid or randomized search and see whether they can match the linear model.
  • Add an interaction between neighborhood and area and check the Hillcrest residuals.
  • Explain the winning model with permutation importance.
  • Change make_houses (more noise, fewer rows, a nonlinear age effect) and see which model wins then.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why are missing values imputed inside the cross-validated pipeline rather than before splitting the data?

    Imputing before the split would compute medians using rows that later serve as test data. Inside a pipeline, every fold fits the imputer on training rows only.
  2. 2. The log-target linear model beat the random forest by a wide margin. What is the best explanation in this project?

    The prices were generated from multiplicative effects, so the log makes the relationship linear. Trees must approximate it with steps and have little data to do so.
  3. 3. Why are residuals shown as a percentage of the actual price?

    House prices span a wide range, so relative errors compare cheap and expensive houses on the same scale.
  4. 4. A mean residual of -4.7 percent appears for Hillcrest, which has 28 test houses. What is the sensible reaction?

    With few rows and a large spread the gap could be chance. Verify it on more data and avoid tuning on the test set.
  5. 5. What does a baseline model such as the mean predictor give you?

    A baseline shows what no skill looks like (R2 near zero here) so you can judge how much better the real models are.

Summary

  • Audit first: this table hid an impossible area and missing ages that would have been silently modelled.
  • Do imputation and encoding inside a pipeline so cross-validation stays honest.
  • Compare several models and a mean baseline on identical folds; look at the spread between folds, not just the average.
  • The target transform (log price) mattered more than the algorithm, because it matched the shape of the problem.
  • Use the test set once, then read the residuals: overall bias, patterns against the prediction, errors by group and the largest misses.
  • Close with a short written summary that states the metric, the comparison and the limitations.

Next: Project: Survey Analysis, where the challenge is messy human answers instead of numbers.