House price prediction walkthrough
scikit-learn 1.9.1
Fit a median baseline and a linear regression on the same data and compare test errors.
On this page
Define the prediction problem
Use area, bedroom count, and property age to estimate price_k, a price in thousands of the synthetic dataset’s units. The small example demonstrates a workflow from data to evaluation.
Compare predictions on the same test rows
Save the CSV next to your script. Split once, train both estimators only on the training data, and calculate MAE for their predictions on the same test rows.
import pandas as pd
from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
homes = pd.read_csv("house_prices.csv")
X = homes[["area_m2", "bedrooms", "age_years"]]
y = homes["price_k"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
models = {"Median baseline": DummyRegressor(strategy="median"), "Linear regression": LinearRegression()}
for name, model in models.items():
predictions = model.fit(X_train, y_train).predict(X_test)
print(f"{name}: {mean_absolute_error(y_test, predictions):.2f}")
Example output
Median baseline: 115.60 Linear regression: 4.91
Interpret the result
The trained model has lower test MAE on these five synthetic houses. Different splits can give different scores. Real-world performance needs representative data and validation that accounts for the market, time, or location of future predictions.