Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. import random
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. x = []
  6. y = []
  7.  
  8. for i in range(100):
  9. x.append(random.uniform(0, 2))
  10. y.append(4+3*x[i])
  11. y[i] += np.random.normal(y[i], 1)
  12. # np.random.normal(loc(mean),scale(STDEV))
  13.  
  14. plt.plot(x,y,'bo')
  15. # plot x and y using blue circle markers
  16. plt.xlabel("x-axis")
  17. plt.ylabel("y-axis")
  18. plt.title("Y = 4 + 3X + randn with linear regression")
  19.  
  20. Xb = np.vstack([x, np.ones(len(x))]).T
  21. # Stack arrays in sequence vertically and transpose matrix
  22. b2, b1 = np.linalg.lstsq(Xb, y)[0]
  23. # method of least squares to get fitted line
  24. X = np.array(x)
  25. plt.plot(X, b2*X + b1, 'r')
  26. # red line
  27.  
  28. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement