import numpy as np
import time
import json
from sklearn.linear_model import LinearRegression
def gradient_descent(x, y, alpha, num_iters):
m, n = x.shape w = np.zeros((n, 1))
start = time.time()
for i in range(num_iters):
gradient = np.dot(x.T, (np.dot(x, w) - y)) / m
w -= alpha * gradient
end = time.time()
print((end - start) * 1000, "milliseconds")
return w
f = open('data.json')
data = json.load(f)
x = data["x"]
x = np.array(x)
x = np.reshape(x, (data["m"], data["n"]))
y = data["y"]
y = np.array(y)
y = np.reshape(y, (data["m"],1))
alpha = 0.0001
num_iters= 10000
start = time.time()
w_optimal = gradient_descent(x, y, alpha, num_iters)
end = time.time()
print("My method took", (end - start) * 1000, "milliseconds")
print(np.reshape(w_optimal, data["n"]))
start = time.time()
model = LinearRegression().fit(x, y)
print(f"intercept: {model.intercept_}")
print(f"slope: {model.coef_}")
end = time.time()
print("SKLearn method took", (end - start) * 1000, "milliseconds")