Advertisement
khaiwen1111

vstack leastsquare

Dec 5th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. %matplotlib inline
  4. f = np.poly1d([5, 1])
  5. x = np.linspace(0, 10, 30)
  6. y = f(x) + 6*np.random.normal(size=len(x))
  7. plt.plot(x, y,"or")
  8.  
  9.  
  10. a=np.vstack([x,np.ones(len(x))]).T
  11. linfit=np.dot(np.linalg.inv(np.dot(a.T,a)),np.dot(a.T,y))
  12. print(linfit)
  13.  
  14. lstsqr=np.linalg.lstsq(a,y)[0]
  15. print(lstsqr)
  16.  
  17. m,n=np.polyfit(x,y,1)
  18. xn=np.linspace(0,10,100)
  19. yn=np.polyval([m,n],xn)
  20. plt.plot(xn,yn)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement