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(AveRooms) and 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
Where:
🔹 Regression Coefficients
🔹 Mean Squared Error (MSE)
🔹 R-squared (R²)
📋 Procedure
Load dataset from scikit-learn
Select:
Feature: AveRooms
Target: MedHouseVal
Use first 150 rows
Compute regression coefficients
Predict values
Compute MSE and R²
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
R²
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
Post a Comment