Code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
theta_0 = 4
theta_1 = 0
alpha = 0.1
costs = np.zeros(1000)
theta_0_list = np.zeros(1000)
theta_1_list = np.zeros(1000)
for i in range(1000):
costs[i] = cost(X, y, theta_0, theta_1)
theta_0 = theta_0 - 2*alpha*((y_hat(X, theta_0, theta_1)-y).mean())
theta_1 = theta_1 - 2*alpha*((y_hat(X, theta_0, theta_1)-y).T@X)/len(X)
theta_0_list[i] = theta_0
theta_1_list[i] = theta_1
for i in range(0, 200, 20):
plt.title(label="Fit at iteration {}".format(i))
plt.plot(X, theta_0_list[i]+theta_1_list[i]*X, color='k')
plt.scatter(X, y, color='k')
plt.xlabel("x")
plt.ylabel("y")
format_axes(plt.gca())
plt.savefig("../gradient-descent/fit-iteration-{}.pdf".format(i), bbox_inches="tight", transparent=True)
plt.cla()