Simple Linear Regression Using California Housing Dataset (CSV Input)

 

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

y=b0+b1xy = b_0 + b_1 x

Where:

  • xx: Independent variable (total_rooms)

  • yy: Dependent variable (median_house_value)


🔹 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 housing.csv

  2. Select:

    • Feature: total_rooms

    • Target: median_house_value

  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 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


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

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