import numpy as np
# Data
X = np.array([
[1, 2, 3], # First data point: price=1, size=2, rooms=3
[4, 5, 6], # Second data point
[7, 8, 9] # Third data point
])
# Let's assume y is the target variable (not provided in your input)
# For demonstration, I'll create a simple target
y = np.array([14, 32, 50]) # Example target values
# Initialize parameters (weights and bias)
weights = np.zeros(X.shape[1]) # [0, 0, 0]
bias = 0
learning_rate = 0.01
iterations = 1000
# Gradient Descent
for i in range(iterations):
# Forward pass: predictions
predictions = np.dot(X, weights) + bias
# Calculate error
errors = predictions - y
# Calculate gradients (derivatives of MSE with respect to weights and bias)
dw = (2/X.shape[0]) * np.dot(X.T, errors)
db = (2/X.shape[0]) * np.sum(errors)
# Update parameters
weights = weights - learning_rate * dw
bias = bias - learning_rate * db
# Calculate MSE for monitoring (optional)
if i % 100 == 0:
mse = np.mean(errors**2)
print(f"Iteration {i}, MSE: {mse}, Weights: {weights}, Bias: {bias}")
print("Final parameters:")
print(f"Weights: {weights}")
print(f"Bias: {bias}")