Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Intermediate ⏱ 55 min

🏠 Predict House Prices with Scikit-Learn

🎯 What You'll Build

A machine learning model that predicts house prices from the California Housing dataset, evaluated with RMSE and R².

📋 What You'll Need

1

Load the dataset

Scikit-learn ships the California Housing dataset — no download needed.

from sklearn.datasets import fetch_california_housing
import pandas as pd

data = fetch_california_housing()
df = pd.DataFrame(data.data, columns=data.feature_names)
df['Price'] = data.target  # price in $100k units
print(df.describe())
2

Split and train

Hold out 20% of the data for testing, then train a Random Forest regressor.

from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor

X = df.drop('Price', axis=1)
y = df['Price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
print("Training complete.")
Training complete.
3

Evaluate and interpret

Calculate RMSE and R², then plot feature importance.

from sklearn.metrics import mean_squared_error, r2_score
import numpy as np, matplotlib.pyplot as plt

y_pred = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2   = r2_score(y_test, y_pred)
print(f"RMSE: ${rmse * 100_000:,.0f}")
print(f"R²:   {r2:.3f}")

# Feature importance
importances = pd.Series(model.feature_importances_, index=data.feature_names).sort_values()
importances.plot(kind='barh', title='Feature Importance', figsize=(8,5))
plt.tight_layout()
plt.savefig('feature_importance.png', dpi=150)
RMSE: $49,832
R²:   0.817
💡 Tip: Try GridSearchCV to tune n_estimators and max_depth — often shaves another 5-10% off the RMSE.

🎉 You Did It!

You trained, evaluated, and interpreted your first regression model. This pattern — load, split, train, evaluate — applies to almost every supervised learning problem.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.