California House Pricing Prediction (OLS Linear Regression)

This repository contains a Linear Regression model trained using the Ordinary Least Squares (OLS) method on the California Housing dataset to predict median house values.

πŸ“‹ Project Overview

In this study, a machine learning model was developed to predict home prices based on various neighborhood features (such as median income, house age, average rooms, and location) in California.

πŸ“Š Dataset Specifications

The dataset was loaded using scikit-learn's fetch_california_housing utility. It contains 20,640 samples with 8 independent features:

  • MedInc: Median income in block (The most critical feature with a strong positive correlation of ~0.69)
  • HouseAge: Median house age in block
  • AveRooms: Average number of rooms per household
  • AveBedrms: Average number of bedrooms per household
  • Population: Block population
  • AveOccup: Average house occupancy
  • Latitude / Longitude: Geographical location coordinates
  • MedHouseVal (Target Variable): Median house value for California districts, expressed in hundreds of thousands of dollars ($100,000s)

Feature Correlation Matrix

Below is the correlation matrix of the dataset features:

Correlation Matrix


πŸ› οΈ Model Pipeline and Code Steps

1. Library Imports

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

2. Splitting the Dataset (Train-Test Split)

The dataset is split into an 80% training set and a 20% test set:

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

3. Training the Model (Linear Regression)

model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

πŸ“ˆ Model Evaluation and Performance

The performance metrics computed on the hold-out test set are as follows:

  • RΒ² Score (Coefficient of Determination): 0.5758 (Our model explains approximately 57.58% of the variance in the target variable.)
  • Mean Squared Error (MSE): 0.5559
  • Root Mean Squared Error (RMSE): 0.7456

Actual vs. Predicted Values Plot

Actual vs Predicted Graph


πŸ’Ύ Loading and Running the Model Locally

You can load the pre-trained model file (linear_regression_model.pkl or local counterpart) in Python as follows:

import joblib

# Load the model
loaded_model = joblib.load('linear_regression_model.pkl')

# Make predictions
predictions = loaded_model.predict(X_test)
Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support