Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. import numpy as np
  2.  
  3. m=20
  4. X=np.array((np.arange(0,10,0.5)))
  5. y=2*X+0.5
  6. X=np.concatenate((X.reshape(m,1),np.ones(m).reshape(m,1)),axis=1)
  7. randn=np.random.rand(len(X))
  8. y=y-randn
  9. y=y.reshape(m,1)
  10. #X.shape=(20,2),y.shape=(20,1)
  11. theta=np.ones(2).reshape(2,1)
  12.  
  13. a=0.01
  14. epochs=100
  15. def linear(x,theta):
  16. return np.dot(x,theta)
  17.  
  18. def gradient(y,ypred,x):
  19. return (ypred-y)*(x.reshape(2,1))#The Question
  20.  
  21. for i in range(2):
  22. for j,x in enumerate(X):
  23. ypred=linear(x,theta)
  24. theta=theta-a*gradient(y[j],ypred,x)
  25.  
  26. print(theta)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement