Python Code to implement Linear Regression
Input a Dataset and the X value to predict future Y.
Apply Regression Algorithm
Output :
Scatter Plot and Best Regression Line.
and Predicted Y Value
Code:
class linregr():
def __init__(self):
self.coeff1=0
self.coeff0=0
def fitting(self,x,y):
def mean(val):
return sum(val)/len(val)
xm=mean(x)
ym=mean(y)
xx=[i-xm for i in x]
yy=[i-ym for i in y]
xx2=[i*i for i in xx]
xxyy=[xx[i]*yy[i] for i in range(len(xx))]
self.coeff1=sum(xxyy)/sum(xx2)
self.coeff0=ym-(self.coeff1*xm)
def predict(self,args):
li=[]
if(type(args)==type([])):
pass
else:
arg=[]
arg.append(args)
args=arg
for i in args:
li.append((self.coeff1*float(i))+self.coeff0)
return li
Dataset:
YearsExperi Sala
ence ry
2508
1.6 1
2665
1.8 9
4509
2.1 7
4783
2.4 3
5755
2.7 0
5850
2.8 7
6201
3.2 4
6307
3.4 6
6770
3.8 1
7298
4.3 8
7598
5.1 7
7805
5.4 0
8090
5.5 9
9310
5.8 3
9473
6.3 7
1009
6.4 01
1019
7.1 91
1045
7.7 00
1073
8.1 84
1085
8.2 35
1107
8.4 31
1192
8.6 43
1201
9.7 22
1233
9.9 30
1239
10.1 61
1262
10.2 72
1286
10.2 25
1292
10.7 43
1356
10.7 01
1394
10.9 63
Output :
Colab Notebook Link:
https://colab.research.google.com/drive/1HiyvAoQqsZtfzmW
4pdlek_wcVvWA6OSB?usp=sharing