EXPERIMENT No.
2
Date of Performance:
Date of Submission:
Aim:To implement the Simple Linear regression model in machine learning using Python.
Theory:
Simple Linear Regression: Simple Linear Regression is a type of Regression algorithm that
models the relationship betweenadependentvariableandasingleindependentvariable.The
relationship shownbyaSimpleLinearRegressionmodelislinearoraslopedstraightline,hence
it is called Simple Linear Regression.
Simple Linear regression algorithm has mainly two objectives:
oModel
the relationship between the two variables.
oForecasting
new observations.
The Simple Linear Regression model can be represented using the below
equation:
y= a
0
+a
1
x+ ε
Where,
a0= It is the intercept of the Regression line.
a1= It is the slope of the regression line, which tells whether the line is increasing or
decreasing. ε = The error term.
Assumptions of simple linear regression :
Simple linear regression is aparametric test, meaning that it makes certain assumptions about the
data. These assumptions are:
•Homogeneity of variance (homoscedasticity): the size of the error in our prediction
doesn’t change significantly across the values of the independent variable.
• Independence of observations: the observations in the dataset were collected using
statistically valid sampling methods, and there are no hidden relationships among
observations.
•Normality: The data follows a normal distribution.
Steps for Implementation:
Step 1: Import the required python packages.
•Pandas for data manipulation
•Numpy for mathematical calculations
•MatplotLib andSeabornfor visualizations.
•Sklearnlibraries are used for machine learning operations.
tep 2: Load the dataset.
S
Download the dataset and upload it to your notebook and read it into the pandas dataframe.Step
Step 3: Data Analysis.
Step 4: Split dataset into dependent/independent variables.
(X)is the independent variable
( y)is dependent on experience
Step 5: Split data into Train/Test sets.
Split data into training (80%) and test (20%) sets usingtrain_test_split
Step 6: Applying the Model.
Pass theX_trainandy_traindata into the regressor model byregressor.fitto train the model
with our training data.
Step 7: Predict the result.
To predict any value ofy(Salary)dependent onX(Experience)with the trained model using
regressor.predict
Step 8: Plot the training and test results.
•Plot training set data vs predictions
irst we plot the result of training sets(X_train, y_train)withX_trainand predicted value of
F
y_train (regressor.predict(X_train))
•Plot test set data vs predictions
econdly, we plot the result of test sets(X_test, y_test)withX_trainand predicted value
S
ofy_train (regressor.predict(X_train))
The linear equationy = mx + c, we can also get thec(y-intercept)andm(slope/coefficient)from
the regressor model.
Code And Output:
Conclusion:
Simple linear regression is a potent supervised machine learning approach that enables us to
predict linear correlations between two variables.
Marks & Signature:
R1 R2 R3 R4 Total Signature
(15 Marks)
(4 Marks) (4 Marks) (4 Marks) (3 Marks)
EXPERIMENT No. 3
Date of Performance:
Date of Submission:
Aim:To implement the Multiple Linear regression model in machine learning using Python.
Theory:
MultipleLinearRegressionisoneoftheimportantregressionalgorithmswhichmodels
the linear relationship between a single dependent continuous variable and more than one
independent variable.
Some key points about MLR:
• For MLR, the dependent or target variable(Y) must be the continuous/real, but the
predictor or independent variable may be of continuous or categorical form.
•Eachfeaturevariablemustmodelthelinearrelationshipwiththedependentvariable.•
MLR tries to fit a regression line through a multidimensional space of data-points.
The Simple Linear Regression model can be represented using the below
equation:y = β0+ β1X1+
……+ βnXn
+Є
Where,
•y= the predicted value of the dependent variable
•β0= the y-intercept (value of y when all other parameters are set to 0)
•β1X1 = the regression coefficient (β1) of the first independent variable (X1)•
βnXn = the regression coefficient of the last independent variable
•Є= model error (a.k.a. how much variation there is in our estimate of ‘y’
Assumptions of Multiple linear regression :
Simple linear regression is aparametric test, meaning that it makes certain assumptions about the
data. These assumptions are:
•Alinear relationshipshould exist between the Target and predictor variables.•
The regression residuals must benormally distributed.
•MLR assumes little orno multicollinearity(correlation between the independent variable)
in data.
Steps for Implementation:
Step 1: Import the required python packages.
•Pandas for data manipulation
•Numpy for mathematical calculations
•MatplotLib andSeabornfor visualizations.
•Sklearnlibraries are used for machine learning operations.
Step 2: Load the dataset.
ownload the dataset and upload it to your notebook and read it into the pandas dataframe.
D
tep 3: Handling Categorical Variables
S
•handle categorical variable
•dropping extra column
• concatation of independent variables and new categorical variable.
tep 4: Extracting dependent and independent Variables.
S
(X) is the independent variable
( y) is dependent on experience
Step 5: Split data into Train/Test sets.
plit data into training (80%) and test (20%) sets usingtrain_test_split.
S
Step 6: Applying the Model
Pass theX_trainandy_traindata into the regressor model byregressor.fitto train the model
with our training data.
Step 7: Predict the result
The last step for our model is checking the performance of the model.
#Predicting the Test set result;
y_pred= regressor.predict(x_test)
Code And Output:
Conclusion:
The basics of linear regression in Python, including the best-fit line, the coefficient of x, and how to
build multiple linear regression models using sklearn.
Marks & Signature:
R1 R2 R3 R4 Total Signature
(15 Marks)
(4 Marks) (4 Marks) (4 Marks) (3 Marks)