Simple Linear Regression: Predict Housing Prices
Part 1 of the ML Regression Lab. Build, evaluate, and deploy a one-feature linear regression model that predicts house prices from area — the full workflow, end to end.
@shvinn
Machine Learning Engineer
This is Part 1 of the ML Regression Lab — a five-project tour of regression where every model ends as a deployable app. We start with the simplest model that teaches the entire machine-learning workflow: simple linear regression with a single feature.
Follow along: this is
01_housing_price_predictionin github.com/shvinn/ai-lab-99. Open the notebook to run it cell by cell, or hand this article to a coding agent to reproduce the project from scratch.
The goal
Predict a house's price from one feature — its area in square feet. The model learns a straight line:
Step 1 — Setup
jupyter
pandas
matplotlib
scikit-learn
streamlitpip install -r requirements.txtStep 2 — Load and explore the data
import pandas as pd
df = pd.read_csv("housing_data.csv")
df.head()
df.describe()Always look at the data before modeling. A scatter plot tells you whether a straight line is even a reasonable hypothesis:
import matplotlib.pyplot as plt
plt.scatter(df["area_in_sqft"], df["price_in_dollars"], alpha=0.7)
plt.xlabel("Area (sqft)")
plt.ylabel("Price ($)")
plt.show()Step 3 — Train the model
Separate the feature matrix X from the target y, then hold out 20% of the
data so we can evaluate on examples the model never saw.
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
X = df[["area_in_sqft"]] # 2D: scikit-learn expects a matrix of features
y = df["price_in_dollars"] # 1D: the target
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
model = LinearRegression()
model.fit(X_train, y_train)Step 4 — Interpret what it learned
Linear models are wonderfully transparent — the learned parameters are the explanation:
model.coef_ # → [670.33] the slope w: $ per extra sqft
model.intercept_ # → 122632.82 the intercept b: price at area 0So each additional square foot adds about $670 to the predicted price.
Step 5 — Evaluate
RMSE puts the error back in the target's units (dollars). Comparing train vs. test RMSE tells you whether the model generalizes or is overfitting.
from sklearn.metrics import mean_squared_error
rmse_train = mean_squared_error(y_train, model.predict(X_train)) ** 0.5
rmse_test = mean_squared_error(y_test, model.predict(X_test)) ** 0.5
print(f"RMSE Train: {rmse_train:.2f}")
print(f"RMSE Test: {rmse_test:.2f}")Step 6 — Deploy as a Streamlit app
Once trained, the whole model is just two numbers. Freeze them into a tiny app so anyone can get predictions:
import streamlit as st
def predict_housing_price(area):
return 670.33282313 * area + 122632.82090315572
st.title("🏡 Housing Price Prediction")
area = st.number_input("Area (sqft):", min_value=0.0, step=10.0)
if st.button("Submit"):
if area <= 0:
st.error("Please enter a valid area.")
else:
st.success(f"Estimated Price: ${predict_housing_price(area):,.2f}")streamlit run app.pyWhat's next
You've run the complete loop: load → explore → train → interpret → evaluate → deploy. In Part 2 we add a second feature and move from a line to a plane with multiple linear regression.
Related reading
Multiple Linear Regression: Predict Energy Consumption
Part 2 of the ML Regression Lab. Extend regression to several features — predict energy use from temperature and humidity — and learn to read multi-feature coefficients.
Logistic Regression, Interactively
Build intuition for logistic regression by training one live in your browser — adjust the learning rate, watch the decision boundary move, no backend required.