Simple Linear Regression Using California Housing Dataset (using scikit-learn)

 

Experiment 

Simple Linear Regression Using California Housing Dataset (scikit-learn)


Aim

To implement Simple Linear Regression using one feature (AveRooms) from the California Housing dataset  to predict house prices using the top 150 records.


Objectives

  • Load dataset from a scikit-learn

  • Select a single feature(AveRoomsand target variable ( MedHouseVal)

  • Build a regression model using scikit-learn

  • Compute MSE and R² using buitin functions

  • Visualize regression line


🛠️ Tools Required

  • Python

  • scikit-learn

  • NumPy

  • Pandas

  • Matplotlib


📖 Theory

🔹 Simple Linear Regression

y=b0+b1xy = b_0 + b_1 x

Where:

  • xx: Independent variable (AveRooms)

  • yy: Dependent variable (MedHouseVal)


🔹 Regression Coefficients

b1=(xxˉ)(yyˉ)(xxˉ)2b_1 = \frac{\sum (x - \bar{x})(y - \bar{y})}{\sum (x - \bar{x})^2}b0=yˉb1xˉb_0 = \bar{y} - b_1 \bar{x}

🔹 Mean Squared Error (MSE)

MSE=1n(yiy^i)2MSE = \frac{1}{n} \sum (y_i - \hat{y}_i)^2

🔹 R-squared (R²)

R2=1SSresSStotR^2 = 1 - \frac{SS_{res}}{SS_{tot}}

📋 Procedure

  1. Load dataset from scikit-learn

  2. Select:

    • Feature: AveRooms

    • Target: MedHouseVal

  3. Use first 150 rows

  4. Compute regression coefficients

  5. Predict values

  6. Compute MSE and R²

  7. Plot regression line


💻 Program

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# -----------------------------
# Load dataset directly
# -----------------------------
housing = fetch_california_housing()

X = housing.data[:, housing.feature_names.index('AveRooms')].reshape(-1, 1)
y = housing.target.reshape(-1, 1)

# Take first 150 samples (same as your original)
X = X[:150]
y = y[:150]

# -----------------------------
# Visualization (Scatter Plot)
# -----------------------------
plt.scatter(X, y, s=20)

# -----------------------------
# Train model
# -----------------------------
model = LinearRegression()
model.fit(X, y)

# -----------------------------
# Coefficients
# -----------------------------
b1 = model.coef_[0][0]
b0 = model.intercept_[0]

print("Slope (b1):", b1)
print("Intercept (b0):", b0)

# -----------------------------
# Predictions
# -----------------------------
y_pred = model.predict(X)

# -----------------------------
# Metrics
# -----------------------------
mse = mean_squared_error(y, y_pred)
r2 = r2_score(y, y_pred)

print("MSE:", mse)
print("R²:", r2)

# -----------------------------
# Plot regression line
# -----------------------------
plt.plot(X, y_pred)

plt.xlabel("Average Rooms")
plt.ylabel("Median House Value")
plt.title("Simple Linear Regression (150 Samples)")
plt.show()


 Output

  • Scatter plot of data

  • Regression line

  • Computed values:

    • Slope (b₁)

    • Intercept (b₀)

    • MSE


Slope (b1): 0.3072787169173846 Intercept (b0): 0.5097916053072971 MSE: 0.8823553195768612 R²: 0.17260066984923983





Result

The linear regression model was successfully implemented using the AveRooms feature.
Model performance was evaluated using MSE and R².

  • The model establishes a linear relationship between AveRooms and MedHouseVal

  • Performance depends on how strongly the feature influences the target

  • Using only one feature may limit accuracy

Comments

Popular posts from this blog

Machine Learning Lab PCCSL508 Semester 5 KTU CS 2024 Scheme manual - Dr Binu V P

Explore California Housing Dataset

Lab Assignment-1