A basic implementation of multi linear regression using gradient descent. Synthetic housing price data is generated based on area and number of rooms. Includes data visualization and model training from scratch using NumPy and Matplotlib
#MachineLearning #LinearRegression #GradientDescent #Python #NumPy #Matplotlib
#DataScience #ArtificialData #Regression #DeepLearning #AI #HousingPrices
#DataVisualization #ML #SyntheticData #Coding #OpenSource
import numpy as np
import matplotlib.pyplot as plt
n = 200
X1 = np.random.normal(loc=175, scale=30, size=n)
X1 = np.clip(X1, 50, 300)
X2 = np.random.randint(1, 7, size=n)
price = X1 * 200 + X2 * 100 + 500 + np.random.normal(0, 3000, size=n)
w1 = np.random.randn() * 0.1 # Weight for X1 (house area) w2 = np.random.randn() * 0.1 # Weight for X2 (number of rooms) b = np.random.randn() * 0.1 # Bias term
learning_rate = 0.00001 iterations = 1000
for i in range(iterations): # Compute predictions using the current weights and bias predictions = w1 * X1 + w2 * X2 + b
# Compute gradients (partial derivatives of the loss function)
dw1 = (2/n) * np.sum((predictions - price) * X1) # Gradient for w1
dw2 = (2/n) * np.sum((predictions - price) * X2) # Gradient for w2
db = (2/n) * np.sum((predictions - price)) # Gradient for bias
# Update parameters using gradient descent
w1 -= learning_rate * dw1
w2 -= learning_rate * dw2
b -= learning_rate * db
# Compute Mean Squared Error (MSE) loss
Loss = (1/n) * np.sum((predictions - price) ** 2)
# Print loss every 50 iterations to monitor training progress
if i % 50 == 0:
print(f"Iteration {i}: Loss {Loss}")
y = w1 * X1 + w2 * X2 + b
plt.scatter(X1, price, alpha=0.5, label='Data')
plt.plot(X1, y, label='Fitted Line', color='r')
plt.legend()
plt.show()