Experiment
Simple Linear Regression Using California Housing Dataset (CSV Input)
Aim
To implement Simple Linear Regression using one feature (total_rooms) from the California Housing dataset (housing.csv) to predict house prices using the top 150 records.
Objectives
-
Load dataset from a CSV file
-
Select a single feature and target variable
-
Build a regression model manually
-
Compute MSE and R² manually
-
Visualize regression line
🛠️ Tools Required
-
Python
-
NumPy
-
Pandas
-
Matplotlib
📖 Theory
🔹 Simple Linear Regression
Where:
🔹 Regression Coefficients
🔹 Mean Squared Error (MSE)
🔹 R-squared (R²)
📋 Procedure
-
Load dataset from housing.csv
-
Select:
-
Use first 150 rows
-
Compute regression coefficients
-
Predict values
-
Compute MSE and R²
-
Plot regression line
💻 Program
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# -----------------------------
# Load dataset
# -----------------------------
df = pd.read_csv("/content/sample_data/housing.csv")
# -----------------------------
# Select feature and target
# -----------------------------
X = df['total_rooms'].head(150).values.reshape(-1, 1)
y = df['median_house_value'].head(150).values.reshape(-1, 1)
# -----------------------------
# Visualization (Scatter Plot)
# -----------------------------
plt.scatter(X, y, color="blue", s=20)
# -----------------------------
# Mean values
# -----------------------------
m_x = np.mean(X)
m_y = np.mean(y)
# -----------------------------
# Compute coefficients
# -----------------------------
SS_xy = np.sum((X - m_x) * (y - m_y))
SS_xx = np.sum((X - m_x) * (X - m_x))
b1 = SS_xy / SS_xx
b0 = m_y - b1 * m_x
print("Slope (b1):", b1)
print("Intercept (b0):", b0)
# -----------------------------
# Predictions
# -----------------------------
y_pred = b0 + b1 * X
# -----------------------------
# Manual MSE
# -----------------------------
n = len(y)
mse = np.sum((y - y_pred) ** 2) / n
print("MSE:", mse)
# -----------------------------
# Manual R²
# -----------------------------
SS_res = np.sum((y - y_pred) ** 2)
SS_tot = np.sum((y - m_y) ** 2)
r2 = 1 - (SS_res / SS_tot)
print("R²:", r2)
# -----------------------------
# Plot regression line
# -----------------------------
plt.plot(X, y_pred, color="red")
plt.xlabel("Total 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): 30.03063864613546
Intercept (b0): 144527.3970744946
MSE: 9070344426.642298
R²: 0.1494586436630212
Result
The linear regression model was successfully implemented using the total_rooms feature.
Model performance was evaluated using MSE and R².
The model establishes a linear relationship between total rooms and house price
-
Performance depends on how strongly the feature influences the target
-
Using only one feature may limit accuracy
Comments
Post a Comment