0% found this document useful (0 votes)
128 views4 pages

Linear Regression for Beginners

The document describes using a linear regression algorithm to predict employee salaries based on years of experience. It defines a linear regression class, loads and splits salary and experience data, trains a model on the training data, makes predictions on test data, and calculates performance metrics like RMSE and R2 score. Visualizations of the training and test predictions are also generated.

Uploaded by

Dreaming Boy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views4 pages

Linear Regression for Beginners

The document describes using a linear regression algorithm to predict employee salaries based on years of experience. It defines a linear regression class, loads and splits salary and experience data, trains a model on the training data, makes predictions on test data, and calculates performance metrics like RMSE and R2 score. Visualizations of the training and test predictions are also generated.

Uploaded by

Dreaming Boy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1/4/22, 11:34 AM linear-regression-ML-DA.

ipynb - Colaboratory

Linear regression Algorithm

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=[]
      [Link](args)
      args=arg
    for i in args:
      [Link]((self.coeff1*float(i))+self.coeff0)
    return li

import [Link] as plt

import pandas as pd

from math import sqrt

from [Link] import mean_squared_error, r2_score

from [Link] import files

uploaded = [Link]()

df = pd.read_csv(r"Salary_Data.csv")

[Link]()

[Link] 1/4
1/4/22, 11:34 AM [Link] - Colaboratory

Choose Files No file chosen


Upload widget is only available when the cell has been executed in
browser session. Please rerun this cell to enable.
YearsExperience Salary

0 1.6 25081
_x = [Link][:, :-1].values

1 1.8 26659
_y = [Link][:, 1].values

2 2.1 45097

x=[]
3 2.4 47833
y=list(_y)

4 2.7 57550
for i in range(len(_x)):

    [Link](_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()

[Link](x_train,y_train)

y_pred=[Link](x_test)

# Visualizing the Training set results

[Link](x_train, y_train, color='red')

[Link](x_train, [Link](x_train), color='blue')

[Link]('Salary VS Experience (Training set)')

[Link]('Year of Experience')

[Link]('Salary')

[Link]()

# Visualizing the Test set results

[Link](x_test, y_test, color='red')

[Link](x_test , [Link](x_test), color='blue')

[Link]('Salary VS Experience (Test set)')

[Link]('Year of Experience')

[Link]('Salary')

[Link]()

[Link] 2/4
1/4/22, 11:34 AM [Link] - 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

[Link] 3/4
1/4/22, 11:34 AM [Link] - Colaboratory

[Link] 4/4

You might also like