Skip to content

Instantly share code, notes, and snippets.

@yvan
Last active October 28, 2017 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yvan/4ac67e0eb8998ba3544fa5241f943678 to your computer and use it in GitHub Desktop.
Save yvan/4ac67e0eb8998ba3544fa5241f943678 to your computer and use it in GitHub Desktop.
%matplotlib inline
import matplotlib.pyplot as plt
# calculate the cost at -5
def f(w1):
return np.mean((w1*data['speed'] - data['dist'])**2)
w1 = -5
h = 0.1
x_tan = np.linspace(-10, 0, 15)
gradient = (f(w1+h)-f(w1))/h # derivative
tan = f(w1)+gradient*(x_tan-w1) # tangent
# plot the costs
plt.plot(range(-10,11,1), costs)
plt.plot(x_tan, tan, color='red')
plt.plot([-5], [f(-5)], marker='o', color='red')
plt.xlabel('w1')
plt.ylabel('cost')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment