Predictive modeling

Learning to Predict Housing Market Trends with Machine Learning

In the competitive world of real estate, understanding market trends and forecasting sales performance can lead to smarter investment decisions and better risk planning. As part of my self-guided learning in predictive modeling, I explored housing market data to simulate the kind of analysis a real estate analytics team might perform.

Using a global housing market dataset from Kaggle (2015–2024), I applied machine learning techniques to build models that could forecast sales trends and surface key drivers of market movement. The goal was to push beyond theory and create something with real-world relevance—and showcase how data science can inform strategic decisions.

The Dataset: Real Estate Signals from Around the World

My journey began with exploring a global housing market dataset from Kaggle. This dataset provided a rich source of information, allowing me to delve into various factors influencing housing prices.

import pandas as pd
# Load the dataset
df = pd.read_csv('Global_Housing_Market.csv')
# View key features
print(df.columns)

The dataset included:

  • House Price Index (HPI)
  • Rent Index
  • Affordability Ratio
  • Mortgage Rate (%)
  • Inflation Rate (%)
  • GDP Growth (%)
  • Construction Index

The Challenge: Predicting Housing Price Index

The core objective of this project was to predict future values of the House Price Index using economic and housing variables. To simulate market demand, I leveraged affordability and rent indices as contextual features. This allowed me to learn how to frame a real-world prediction problem in a machine learning context.

Feature Engineering for Smarter Predictions

To enhance model performance, I focused on feature engineering, a crucial step in preparing data for machine learning. This involved handling missing values and creating new features from existing ones.

# Fill missing values
df.fillna(method='ffill', inplace=True)
# Create lag features
df['HPI_lag1'] = df['House Price Index'].shift(1)
df['GDP_growth_lag1'] = df['GDP Growth (%)'].shift(1)
# Drop rows with NaN after shifting
df.dropna(inplace=True)
# Feature set and target
features = ['Rent Index', 'Affordability Ratio', 'Mortgage Rate (%)',
            'Inflation Rate (%)', 'GDP_growth_lag1', 'Construction Index', 'HPI_lag1']
target = 'House Price Index'

X = df[features]
y = df[target]

Through this process, I learned the importance of transforming raw data into a format that machine learning models can effectively learn from.

Model Building with scikit-learn

I then moved on to building predictive models. I decided to compare a traditional linear regression model with a more flexible Random Forest Regressor to understand their strengths and weaknesses in this context.

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_squared_error

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Random Forest
rf = RandomForestRegressor(n_estimators=100, random_state=42)
rf.fit(X_train, y_train)
y_pred_rf = rf.predict(X_test)

# Linear Regression
lr = LinearRegression()
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)

# Evaluation
print("Random Forest R²:", r2_score(y_test, y_pred_rf))
print("Linear Regression R²:", r2_score(y_test, y_pred_lr))

This hands-on experience taught me how to train, test, and evaluate different regression models using scikit-learn, a fundamental library in machine learning.

Communicating Results Visually

Finally, I focused on making the model's insights accessible and impactful through data visualization. This allowed me to understand not just what the model predicted, but why.

import matplotlib.pyplot as plt
import seaborn as sns

# Feature importance from Random Forest
importances = rf.feature_importances_
sns.barplot(x=importances, y=features)
plt.title("Feature Importance")
plt.show()

# Predicted vs Actual
plt.plot(y_test.values, label="Actual")
plt.plot(y_pred_rf, label="Predicted")
plt.legend()
plt.title("Random Forest Predictions vs Actual HPI")
plt.show()

These visualizations brought clarity to the models’ decisions and showcased the real-world impact of factors like mortgage rates and construction activity on the House Price Index.

What I Learned

This project was an invaluable learning experience. It taught me how to:

  • Build end-to-end regression models using real-world data.
  • Handle missing values and apply effective feature transformations.
  • Evaluate and visualize model performance for practical business applications.
  • Utilize model interpretation techniques, such as feature importance, to generate actionable insights.

← all field notes