Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- X=[]
- Y=[]
- # load the data
- for line in open("1d_Linear_regression.csv"):
- x,y= line.split(',')#line data has 2 column seperated by comma so take seperate input use split() function
- X.append(float(x));
- Y.append(float(y));
- # let's turn X and Y into numpy arrays since that will be useful later
- X=np.array(X)
- Y=np.array(Y)
- #plot only the each point
- plt.scatter(X,Y)
- #plt.plot(X,Y)
- plt.show();
- # now apply linear regression formula
- deno= X.dot(X)-X.mean()*X.sum()
- a=(X.dot(Y)-Y.mean()*X.sum())/deno
- b=(Y.mean()*X.dot(X)-X.mean()*X.dot(Y))/deno
- Yhat= a*X+b
- #ploting the value
- plt.scatter(X,Y)
- plt.plot(X,Yhat)
- plt.show()
- #R squared code
- d1= Y-Yhat
- d2= Y-Y.mean()
- r2=1-((d1.dot(d1))/(d2.dot(d2)))
- print "The value of the R squared model",r2
Add Comment
Please, Sign In to add comment