Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
# set ffmpeg path to installed path (for anmation)
plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg'
# load the data
data = pd.read_csv('cars.csv')
# calculate the cost for many different values
# of w1 using our cost function
costs = []
for i in range(-10,11,1):
w1 = i
y_actual = data['dist']
y_predict = w1*data['speed']
costs.append(np.mean((y_predict - y_actual)**2))
# create a graph
fig, ax = plt.subplots()
ax.set_xlabel('w1')
ax.set_ylabel('cost')
_ = ax.plot(range(-10,11,1), costs)
line, = ax.plot([], [], lw=2, color='red')
dot, = ax.plot([], [], marker='o', color='red')
def f(w1):
return np.mean((w1*x_data - y_data)**2)
def get_tangents(w1, x_tan, frames, x_data, y_data):
h = 0.1
learn_rate = 0.001
tans = []
ws = [w1]
for i in range(frames):
# make tangent lines
w1_gradient = 2*np.mean(x_data*(w1*x_data - y_data))
tan = f(w1)+w1_gradient*(x_tan-w1)
tans.append(tan)
# update w1
w1 = w1 - (w1_gradient*learn_rate)
ws.append(w1)
return tans, ws
w1 = -10 # start at a random number
frames = 15
x_tan = np.linspace(-10, 10, 20)
tans, ws = get_tangents(w1, x_tan, frames, data['speed'], data['dist'])
def init():
w1 = -10
x_tan = np.linspace(-10, 10, 20)
w1_gradient = 2*np.mean(x_data*(w1*x_data - y_data))
tan = f(w1)+w1_gradient*(x_tan-w1)
dot.set_data([w1], [f(w1)])
line.set_data([x_tan], tan)
return (dot, line,)
def animate_gradients(i):
line.set_data(x_tan, tans[i])
dot.set_data([ws[i]], [f(ws[i])])
return (dot, line,)
# call the animator for our fitted line
anim = animation.FuncAnimation(fig,
animate_gradients,
init_func=init,
frames=frames,
blit=True)
rc('animation', html='html5')
anim.save('tan.gif', writer='imagemagick', fps=2)
anim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.