Hands-on Regression Project
Hands-on Regression Project
Build a real-world House Price Prediction model from scratch using Python and Scikit-Learn.
Project Overview
In this hands-on project, we will build a complete Regression Model to predict House Prices based on features like area, number of bedrooms, age of the house, and location score.
Project Objectives
- Load and explore the dataset.
- Perform data cleaning and preprocessing.
- Visualize relationships between features.
- Split data into train and test sets.
- Train multiple regression models.
- Evaluate model performance.
- Predict house prices on new data.
Prerequisites
- Python 3.x
- Jupyter Notebook / VS Code / Google Colab
- Libraries: pandas, numpy, matplotlib, seaborn, scikit-learn
pip install pandas numpy matplotlib seaborn scikit-learn
Project Workflow
Step-by-Step Plan
- Import libraries
- Load and explore dataset
- Clean the data
- Perform EDA (Exploratory Data Analysis)
- Feature scaling & preprocessing
- Train/Test split
- Train regression models
- Evaluate models
- Compare results
- Make new predictions
Step 1: Import Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
Step 2: Create or Load Dataset
For demonstration, we'll create a sample housing dataset.
data = {
"Area": [1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500],
"Bedrooms": [2, 3, 3, 4, 4, 5, 5, 5, 6, 6],
"Age": [10, 5, 15, 8, 3, 2, 1, 4, 2, 1],
"Location": [7, 8, 6, 9, 8, 9, 10, 9, 10, 10], # location score 1-10
"Price": [40, 55, 65, 80, 100, 120, 140, 150, 180, 200] # in lakhs
}
df = pd.DataFrame(data)
print(df.head())
Step 3: Data Exploration
print(df.shape)
print(df.info())
print(df.describe())
print(df.isnull().sum())
Step 4: Exploratory Data Analysis (EDA)
# Correlation heatmap
plt.figure(figsize=(6, 4))
sns.heatmap(df.corr(), annot=True, cmap="coolwarm")
plt.title("Feature Correlation")
plt.show()
# Scatter plot
sns.pairplot(df)
plt.show()
Step 5: Feature Selection & Scaling
X = df[["Area", "Bedrooms", "Age", "Location"]]
y = df["Price"]
# Standardize features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
Step 6: Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(
X_scaled, y, test_size=0.2, random_state=42
)
print("Train size:", X_train.shape)
print("Test size:", X_test.shape)
Step 7: Train Regression Models
Linear Regression
lr = LinearRegression()
lr.fit(X_train, y_train)
y_pred_lr = lr.predict(X_test)
Ridge Regression
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, y_train)
y_pred_ridge = ridge.predict(X_test)
Lasso Regression
lasso = Lasso(alpha=0.1)
lasso.fit(X_train, y_train)
y_pred_lasso = lasso.predict(X_test)
Step 8: Evaluate Models
def evaluate(model_name, y_test, y_pred):
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, y_pred)
print(f"\n{model_name} Performance:")
print(f"MAE: {mae:.2f}")
print(f"MSE: {mse:.2f}")
print(f"RMSE: {rmse:.2f}")
print(f"R²: {r2:.2f}")
evaluate("Linear Regression", y_test, y_pred_lr)
evaluate("Ridge Regression", y_test, y_pred_ridge)
evaluate("Lasso Regression", y_test, y_pred_lasso)
Step 9: Visualize Predictions
plt.figure(figsize=(8,5))
plt.scatter(y_test, y_pred_lr, color="blue", label="Linear Regression")
plt.scatter(y_test, y_pred_ridge, color="green", label="Ridge")
plt.scatter(y_test, y_pred_lasso, color="red", label="Lasso")
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], 'k--')
plt.xlabel("Actual Price")
plt.ylabel("Predicted Price")
plt.title("Actual vs Predicted Prices")
plt.legend()
plt.show()
Step 10: Predict on New Data
# New house features: [Area, Bedrooms, Age, Location]
new_house = np.array([[3200, 4, 5, 9]])
new_house_scaled = scaler.transform(new_house)
predicted_price = ridge.predict(new_house_scaled)
print(f"Predicted House Price: ₹{predicted_price[0]:.2f} lakhs")
Model Comparison
| Model | MAE | RMSE | R² | Best For |
|---|---|---|---|---|
| Linear Regression | Low | Medium | High | Linear data |
| Ridge Regression | Low | Low | High | Correlated features |
| Lasso Regression | Low | Medium | High | Feature selection |
Real-Life Analogy
Regression Project = Smart Real Estate Agent
Just like an experienced agent estimates house prices using area, location, and age — our regression model learns from data and predicts the price of new houses accurately.
Suggested Project Folder Structure
house_price_project/
│
├── data/
│ └── house_data.csv
├── notebooks/
│ └── house_price_prediction.ipynb
├── models/
│ └── ridge_model.pkl
├── visualizations/
│ └── correlation_heatmap.png
├── requirements.txt
└── README.md
Bonus: Save & Load Model
import joblib
# Save model
joblib.dump(ridge, "ridge_model.pkl")
joblib.dump(scaler, "scaler.pkl")
# Load model
loaded_model = joblib.load("ridge_model.pkl")
loaded_scaler = joblib.load("scaler.pkl")
new_pred = loaded_model.predict(loaded_scaler.transform([[3200, 4, 5, 9]]))
print("Loaded Model Prediction:", new_pred[0])
Real-World Applications of This Project
Real Estate Portals
- Auto price suggestions
- Market price estimation
Banks & Loans
- Loan eligibility
- Property valuation
Investors
- Property investment analysis
- ROI estimation
Real Estate Agents
- Smart pricing tools
- Client recommendations
Common Mistakes to Avoid
Best Practices
Quick Tips
- Always start with EDA & visualization.
- Compare multiple regression algorithms.
- Use cross-validation for stable evaluation.
- Document findings in a clean notebook.
- Save models for deployment.
- Continuously improve with new data.
What You Learned
Data Handling
- Loading datasets
- Cleaning & preprocessing
EDA & Visualization
- Correlation analysis
- Trend visualization
ML Modeling
- Training multiple models
- Comparing performance
Deployment-Ready Skills
- Save/Load models
- Predict new data
Golden Rule
Key Takeaway
This Hands-on Regression Project walked you through a complete ML workflow — from loading data to training, evaluating, and deploying a regression model. By practicing such projects, you bridge the gap between theory and real-world Machine Learning skills.