Guest User

Untitled

a guest
Jan 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. X=[]
  5. Y=[]
  6. # load the data
  7. for line in open("1d_Linear_regression.csv"):
  8. x,y= line.split(',')#line data has 2 column seperated by comma so take seperate input use split() function
  9. X.append(float(x));
  10. Y.append(float(y));
  11. # let's turn X and Y into numpy arrays since that will be useful later
  12. X=np.array(X)
  13. Y=np.array(Y)
  14. #plot only the each point
  15. plt.scatter(X,Y)
  16. #plt.plot(X,Y)
  17. plt.show();
  18. # now apply linear regression formula
  19. deno= X.dot(X)-X.mean()*X.sum()
  20. a=(X.dot(Y)-Y.mean()*X.sum())/deno
  21. b=(Y.mean()*X.dot(X)-X.mean()*X.dot(Y))/deno
  22.  
  23. Yhat= a*X+b
  24.  
  25. #ploting the value
  26. plt.scatter(X,Y)
  27. plt.plot(X,Yhat)
  28. plt.show()
  29.  
  30. #R squared code
  31. d1= Y-Yhat
  32. d2= Y-Y.mean()
  33.  
  34. r2=1-((d1.dot(d1))/(d2.dot(d2)))
  35. print "The value of the R squared model",r2
Add Comment
Please, Sign In to add comment