Ordinary Least Square Method
Step 1: Import the necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Step 2: Load the CSV Data
# Load the dataset
data = pd.read_csv('house_data.csv')
# Extract the features (X) and target variable (y)
X = data['Size'].values
y = data['Price'].values
# Reshape X to be a 2D array
X = X.reshape(-1, 1)
# Add a column of ones to X for the intercept
X_b = np.c_[np.ones((X.shape[0], 1)), X]
Step 3: Add a Column of Ones to X for the Intercept
# Add a column of ones to X for the intercept
X_b = np.c_[np.ones((X.shape[0], 1)), X]
Step 4: Implement the OLS Method
# Calculate the OLS estimate of theta (the coefficients)
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
Step 5: Make Predictions
# Make predictions
y_pred = X_b.dot(theta_best)
Step 6: Visualize the Results
# Plot the data and the regression line
plt.scatter(X, y, color='blue', label='Data')
plt.plot(X, y_pred, color='red', label='Regression Line')
plt.xlabel('Size (Square Feet)')
plt.ylabel('Price (Dollars)')
plt.legend()
plt.show()
Comments