Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 40 ยท Projects and Practice

Project: Predict House Prices

Build an end-to-end scikit-learn regression project: create a realistic housing table, clean it, compare models, tune a gradient boosting pipeline and report honest test errors.

In this project you will walk the full regression workflow on a small housing dataset: explore, split, preprocess with a pipeline, compare models against a baseline, tune the best one, and report an honest error on held-out data. Every step uses tools from earlier lessons.

The data is generated in code so the project runs anywhere without downloads. It mimics a real table: numeric columns with gaps, categorical columns, skewed prices and a few ugly outliers.

Step 1: Create and look at the data

Python Generate 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.

Observations to act on: ages have gaps (impute), district and condition are categorical (encode), and the maximum price is far above the 75th percentile (outliers, probably entry errors). Prices are also right-skewed, so predicting the log of price often works better.

Project plan

Explore Gaps, skew, outliers.
Split Lock away a test set.
Pipeline Impute, encode, scale.
Compare Baseline vs real models.

Then tune the winner and report the test error once.

1 / 2

Step 2: Split and build the preprocessing

Split first, so nothing about the test set leaks into any decision. Then build one preprocessing recipe shared by every model.

Python Baseline and preprocessing

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 baseline sets the bar: every model must beat "always guess the median". TransformedTargetRegressor handles the log transform and converts predictions back to dollars automatically. Because a few prices are corrupted, the MAE (typical error) is a fairer yardstick than RMSE, which is dominated by outliers.

Step 3: Compare several models

Python Model comparison with cross-validation

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 simple log-target ridge model wins, even against random forest and gradient boosting. That is not an accident: in this market price is roughly area x district factor x condition factor, a product, and taking logs turns a product into a sum, which a linear model fits perfectly. Trees would need lots of data to approximate multiplication with steps. The lesson: look at how the target behaves before reaching for a complicated model, and when a simple model ties or wins, keep the simple one.

Step 4: Tune and test once

Tune the chosen model (here the ridge strength alpha) with cross-validation on the training set only, then score the untouched test set a single time.

Python Tune, then evaluate on the test set

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 test MAE is a fair estimate because the test rows were never used for any choice. The overall R-squared looks modest because a few corrupted prices in the test set are impossible to predict; without them it is far higher. The median absolute error shows what a typical house looks like.

Step 5: Where to go next

  • Clean the outliers (drop prices more than a few times the district median) and see how the error changes; training on corrupted targets also hurts the model.
  • Try other models in the same pipeline, for example HistGradientBoostingRegressor(loss="absolute_error"), and see whether any beats the log-ridge baseline.
  • Inspect the model with permutation importance: do area and district dominate, as you would expect?
  • Add features: price per square meter by district, age buckets, interaction terms (see feature engineering).
  • Save it as a bundle with its metadata (see saving models).
  • Check error by segment: does the model do worse for old houses or the center district?

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why split off the test set before any exploration-driven decisions or tuning?

    Anything chosen using the test set makes its score optimistic.
  2. 2. What does TransformedTargetRegressor with log/exp do?

    It applies func to y before fitting and inverse_func to predictions.
  3. 3. Why include a DummyRegressor baseline?

    Without a baseline you cannot tell whether an error is good or merely typical.
  4. 4. Why prefer MAE when a few target values are corrupted?

    Squaring errors makes a few extreme rows dominate RMSE.

Summary

  • Split first, then build a pipeline that imputes, encodes and scales.
  • Compare real models against a baseline using cross-validation on the training set.
  • Predict a log-transformed target for skewed prices and convert back automatically.
  • Tune with grid search, evaluate on the test set once, and report the error in business terms.

Next: Project: Predict Customer Churn