Advertisement
EXTREMEXPLOIT

Manual Linear Regression

Feb 12th, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. from matplotlib import style
  4.  
  5. style.use('dark_background')
  6. X, Y = np.arange(1950, 2015, 5), [2558, 2782, 3043, 3350, 3712, 4089, 4451, 4855, 5287, 5700, 6090, 6474, 6864]
  7. x = np.linspace(1950, 2015, 5)
  8.  
  9. X_Mean, Y_Mean = sum(X)/len(X), sum(Y)/len(Y)
  10. Sxx, Sxy = sum([(x - X_Mean)**2 for x in X]), sum([(X[i]-X_Mean)*(Y[i]-Y_Mean) for i in range(len(X))])
  11.  
  12. B1 = Sxy / Sxx
  13. B0 = Y_Mean - B1*X_Mean
  14. y = B0 + B1*x
  15. def G(x): return B0 + B1*x
  16.  
  17. for currentYear in X:
  18.     plt.plot(currentYear, G(currentYear), 'ro', color="red")
  19.  
  20. plt.title(f'G(X) = {int(B0)} + {int(B1)}x')
  21. plt.plot(x, y, color='red')
  22. plt.plot(X, Y, 'ro', color='white')
  23. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement