Deep Learning for Housing Market Predictions

Lesson 19 of 21 · ·

House prices depend on size, rooms, age and location, but not in straight lines: the value of being close to the center falls off quickly, and very large homes add less per square meter. Deep learning models, neural networks with several layers, can learn curves like these from data. This lesson trains one for house price prediction in Python and, just as important, checks whether it actually beats a simple model.

Playing loads the video from YouTube. Watch on YouTube

The data

To make the example run anywhere, we generate 2,000 homes with a known pricing rule: a price per square meter, a premium for rooms, a discount for age, a location premium that falls off with distance, and some random noise. With real listings you’d load a CSV with pandas; the rest of the code is the same.

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.neural_network import MLPRegressor
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_absolute_error

rng = np.random.default_rng(42)
n = 2000
size = rng.uniform(50, 250, n)           # square meters
rooms = rng.integers(1, 6, n)
age = rng.uniform(0, 60, n)              # years
distance = rng.uniform(0.5, 30, n)       # km to the city center
price = (2500 * size + 15000 * rooms - 1200 * age
         + 400000 * np.exp(-distance / 6)          # location premium falls off with distance
         - 0.006 * (size - 150) ** 2 * 1000         # very large homes add less per square meter
         + rng.normal(0, 25000, n))
X = np.column_stack([size, rooms, age, distance])
X_train, X_test, y_train, y_test = train_test_split(X, price, test_size=0.2, random_state=0)

The test set, 400 homes, is set aside before any training. Every figure we report comes from homes the models never saw.

Worked example: a baseline, then a neural network

Always start with a simple baseline. Linear regression assumes each feature adds a fixed amount to the price. The neural network has two hidden layers of 64 and 32 units and can bend to fit curves. Both sit in a pipeline with a StandardScaler, because neural networks train badly when features are on very different scales; we also train the network on prices in thousands, for the same reason.

linear = make_pipeline(StandardScaler(), LinearRegression()).fit(X_train, y_train)
network = make_pipeline(StandardScaler(),
                        MLPRegressor(hidden_layer_sizes=(64, 32), max_iter=2000, random_state=0)).fit(X_train, y_train / 1000)

print(f"Linear regression MAE: {mean_absolute_error(y_test, linear.predict(X_test)):,.0f}")
print(f"Neural network MAE:    {mean_absolute_error(y_test, network.predict(X_test) * 1000):,.0f}")
Linear regression MAE: 44,449
Neural network MAE:    22,530

The mean absolute error (MAE) is the average amount each prediction is off by. The network halves it, because it learned the curved effect of distance and size that a straight line can’t represent. Notice how close its error is to the 25,000 of random noise we put in: no model can predict noise, so this is near the best possible on this data.

Predicting a new listing

new_home = np.array([[120, 3, 15, 4.0]])
print(f"Predicted price for the new listing: {network.predict(new_home)[0] * 1000:,.0f}")
Predicted price for the new listing: 546,913

Before trusting that number, look at where the network is wrong. Plot predicted against actual prices for the test set, or sort the test homes by error and read the worst ten. If the biggest misses are all very large or very remote homes, the training data probably had too few of them, and the model is guessing at the edges of what it has seen.

When deep learning is worth it, and when it isn’t

On this data the network wins clearly, but that isn’t guaranteed. With a few hundred listings, a network often overfits and loses to linear regression or a gradient-boosted tree. Real housing data also brings problems a model can’t fix on its own: prices change over time, so test on later sales than you train on, and location needs better features than distance alone, such as neighborhood or school catchment.

For larger datasets and images, such as listing photos, frameworks like PyTorch or Keras take over from scikit-learn. The workflow stays the same: a held-out test set, a simple baseline, and a fair comparison before you trust the bigger model.

Mistakes to avoid

  • Skipping the baseline. Without it, you can’t tell whether the network’s error is good.
  • Scaling features with statistics from the whole dataset: fit the scaler on training data only, which the pipeline does for you.
  • Reporting accuracy on the training set. Only the held-out error tells you how the model will do on the next listing.

Try it yourself

  • Train both models on only 200 homes instead of 1,600. Does the neural network still beat linear regression?
  • Remove the distance feature. How much worse do both models get, and what does that say about location?
  • Change the hidden layers to (16,) and to (128, 64, 32). Does a bigger network always do better here?

Key points

  • Set aside a test set first and report only the error on it.
  • Start with a simple baseline, such as linear regression, before you train a neural network.
  • A network can learn curved effects like the value of location, but it needs enough data and scaled features.

Want to go further?

I teach professionals one to one, and I build these systems for businesses. See Python AI mentoring or see what I build.