linear-regression-ML-DA - Ipynb - Colaboratory
linear-regression-ML-DA - Ipynb - Colaboratory
ipynb - Colaboratory
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
import matplotlib.pyplot as plt
import pandas as pd
from math import sqrt
from sklearn.metrics import mean_squared_error, r2_score
from google.colab import files
uploaded = files.upload()
df = pd.read_csv(r"Salary_Data.csv")
df.head()
https://colab.research.google.com/drive/1HiyvAoQqsZtfzmW4pdlek_wcVvWA6OSB?usp=sharing#scrollTo=UgLGBX3BfGfC 1/4
1/4/22, 11:34 AM linear-regression-ML-DA.ipynb - Colaboratory
0 1.6 25081
_x = df.iloc[:, :-1].values
1 1.8 26659
_y = df.iloc[:, 1].values
2 2.1 45097
x=[]
3 2.4 47833
y=list(_y)
4 2.7 57550
for i in range(len(_x)):
x.append(_x[i,0])
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=1/2, random_state=0)
model=linregr()
model.fitting(x_train,y_train)
y_pred=model.predict(x_test)
# Visualizing the Training set results
plt.scatter(x_train, y_train, color='red')
plt.plot(x_train, model.predict(x_train), color='blue')
plt.title('Salary VS Experience (Training set)')
plt.xlabel('Year of Experience')
plt.ylabel('Salary')
plt.show()
# Visualizing the Test set results
plt.scatter(x_test, y_test, color='red')
plt.plot(x_test , model.predict(x_test), color='blue')
plt.title('Salary VS Experience (Test set)')
plt.xlabel('Year of Experience')
plt.ylabel('Salary')
plt.show()
https://colab.research.google.com/drive/1HiyvAoQqsZtfzmW4pdlek_wcVvWA6OSB?usp=sharing#scrollTo=UgLGBX3BfGfC 2/4
1/4/22, 11:34 AM linear-regression-ML-DA.ipynb - Colaboratory
y_pred
[43107.14235168352,
138632.49358107147,
84205.25857828066,
76429.93929216769,
133078.69409099076,
131967.93419297464,
138632.49358107147,
79762.21898621612,
105309.69664058731,
127524.89460091008,
50882.461637796485,
98645.13725249047,
61990.06061795788,
89759.05806836135,
129746.41439694236]
print("RMSE :",sqrt(mean_squared_error(y_test,y_pred)))
print("R2 Score :",r2_score(y_test,y_pred))
RMSE : 5720.769775165595
R2 Score : 0.9568516775540743
https://colab.research.google.com/drive/1HiyvAoQqsZtfzmW4pdlek_wcVvWA6OSB?usp=sharing#scrollTo=UgLGBX3BfGfC 3/4
1/4/22, 11:34 AM linear-regression-ML-DA.ipynb - Colaboratory
https://colab.research.google.com/drive/1HiyvAoQqsZtfzmW4pdlek_wcVvWA6OSB?usp=sharing#scrollTo=UgLGBX3BfGfC 4/4