Contents

import numpy as np
m = 100000

X = np.linspace(0, 10, m)
y = 3 * X + 2 + np.random.randn(m)
X.shape
(100000,)
from matplotlib import pyplot as plt

plt.scatter(X, y, s=0.1)
plt.show()
../../../_images/a66ab6b2f6234fb58e55c57a7e570523309be73605220d392960fd068998974f.png
X = X.reshape((m,1))
X = np.c_[np.ones((m,1)), X]

y = y.reshape((m,1))
import time

start = int(round(time.time() * 1000))
theta = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
end = int(round(time.time() * 1000))

print(end - start)

print(theta.T)
4
[[2.00474847 2.99961938]]
start = int(round(time.time() * 1000))

theta = np.random.randn(2).reshape((2,1))
print(theta)

for _ in range(1000):
    predictions = X.dot(theta).reshape((m,1))
    errors = predictions - y
    gradient = (X.T.dot(errors)) / m
    theta -= 0.03 * gradient

end = int(round(time.time() * 1000))

print(end - start)

print(theta)
[[-0.33385675]
 [ 0.01597117]]
296
[[2.00357536]
 [2.99979664]]