SlideShare a Scribd company logo
1 of 12
Download to read offline
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 1/12
Linear Regression for Years of experience and Salary
Dataset
Gradient Descent in Linear Regression
In linear regression, the model targets to get the best-fit regression line to predict the value of y based on the
given input value (x). While training the model, the model calculates the cost function which measures the Root
Mean Squared error between the predicted value (pred) and true value (y). The model targets to minimize the
cost function. To minimize the cost function, the model needs to have the best value of θ1 and θ2. Initially model
selects θ1 and θ2 values randomly and then itertively update these value in order to minimize the cost function
untill it reaches the minimum. By the time model achieves the minimum cost function, it will have the best θ1
and θ2 values. Using these finally updated values of θ1 and θ2 in the hypothesis equation of linear equation,
model predicts the value of x in the best manner it can. Therefore, the question arises – How θ1 and θ2 values
get updated ?
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 2/12
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 3/12
-> θj : Weights of the hypothesis. -> hθ(xi) : predicted y value for ith input. -> j : Feature index number (can be 0,
1, 2, ......, n). -> α : Learning Rate of Gradient Descent. We graph cost function as a function of parameter
estimates i.e. parameter range of our hypothesis function and the cost resulting from selecting a particular set
of parameters. We move downward towards pits in the graph, to find the minimum value. Way to do this is
taking derivative of cost function as explained in the above figure. Gradient Descent step downs the cost
function in the direction of the steepest descent. Size of each step is determined by parameter α known as
Learning Rate.
In the Gradient Descent algorithm, one can infer two points :
1.If slope is +ve : θj = θj – (+ve value). Hence value of θj decreases.
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 4/12
2. If slope is -ve : θj = θj – (-ve value). Hence value of θj increases.
The choice of correct learning rate is very important as it ensures that Gradient Descent converges in a
reasonable time. :
1.If we choose α to be very large, Gradient Descent can overshoot the minimum.
It may fail to converge or even diverge.
2.If we choose α to be very small, Gradient Descent will take small steps to reach
local minima and will take a longer time to reach minima.
For linear regression Cost Function graph is always convex shaped.
Reference: geeksforgeeks.org/gradient-descent-in-linear-regression/ (Refered
For Theory)
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 5/12
First, we import a few libraries-
In [10]:
Data Preprocessing & importing Dataset
The next step is to import our dataset ‘Salary_Data.csv’ then split them into input (independent) variables and
output (dependent) variable. When you deal with real datasets, you usually have around thousands of rows but
since the one I have taken here is a sample, this has just 30 rows. So when we split our data into a training set
and a testing set, we split it in 1/3, i.e., 20 rows go into the training set and the rest 10 make it to the testing set.
In [11]:
Plotting Default Dataset
Out[11]:
YearsExperience Salary
0 1.1 39343.0
1 1.3 46205.0
2 1.5 37731.0
3 2.0 43525.0
4 2.2 39891.0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('Salary_Data.csv')
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values
data_top = dataset.head() #Dataset display
data_top
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 6/12
In [12]:
Training & Split
Split arrays or matrices into random train and test subsets
In [13]:
Linear Regression
Now, we will import the linear regression class, create an object of that class, which is the linear regression
model.
In [14]:
Fitting Data
Then we will use the fit method to “fit” the model to our dataset. What this does is nothing but make the
regressor “study” our data and “learn” from it.
plt.scatter(x, y, color = "red")
plt.plot(x,y, color = "green")
plt.title("Salary vs Experience (Dataset)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 1/3)
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 7/12
In [15]:
Testing
Now that we have created our model and trained it, it is time we test the model with our testing dataset.
In [16]:
Data Visualization for Training Dataset
First, we make use of a scatter plot to plot the actual observations, with x_train on the x-axis and y_train on the
y-axis. For the regression line, we will use x_train on the x-axis and then the predictions of the x_train
observations on the y-axis. We add a touch of aesthetics by coloring the original observations in red and the
regression line in green.
In [17]:
Data Visualization for Testing Dataset
repeat the same task for our testing dataset, and we get the following code-
Out[15]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=Fal
se)
lr.fit(x_train, y_train)
y_pred = lr.predict(x_test)
plt.scatter(x_train, y_train, color = "red")
plt.plot(x_train, lr.predict(x_train), color = "green")
plt.title("Salary vs Experience (Training set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 8/12
In [24]:
Linear Regression with Gradient Descent Algorithm
Approach for Years of experience and Salary
Dataset
Importing libraries,Dataset & data preprocessing
plt.scatter(x_test, y_test, color = "red")
plt.plot(x_train, lr.predict(x_train), color = "green")
plt.title("Salary vs Experience (Testing set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 9/12
In [27]:
# Making the imports
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (12.0, 9.0)
# Preprocessing Input data
data = pd.read_csv('Salary_Data.csv')
X = data.iloc[:, 0]
Y = data.iloc[:, 1]
#Plotting Data for visualization
plt.scatter(X, Y)
plt.title("Salary vs Experience (Dataset set)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 10/12
Optimizing parameter value like intercept "c" and slope "m" & learning rate
alpha in Gradient descent formula
In [26]:
Prediction
Slope & Intercept:
12836.600965885045 2915.2044856014018
# Building the model
m = 0
c = 0
L = 0.0001 # The learning Rate
#L = 0.0001 # The learning Rate
#L = 0.0002 # The learning Rate
#L = 0.0003 # The learning Rate
epochs = 1000 # The number of iterations to perform gradient descent
n = float(len(X)) # Number of elements in X
# Performing Gradient Descent
for i in range(epochs):
Y_pred = m*X + c # The current predicted value of Y
D_m = (-2/n) * sum(X * (Y - Y_pred)) # Derivative wrt m
D_c = (-2/n) * sum(Y - Y_pred) # Derivative wrt c
m = m - L * D_m # Update m
c = c - L * D_c # Update c
print("Slope & Intercept:")
print (m, c)
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 11/12
In [28]:
# Making predictions
Y_pred = m*X + c
plt.scatter(X, Y)
plt.plot([min(X), max(X)], [min(Y_pred), max(Y_pred)], color='red') # regression line
plt.title("Salary vs Experience (prediction)")
plt.xlabel("Years of Experience")
plt.ylabel("Salary")
plt.show()
9/17/2019 ML_Activity_1 - Jupyter Notebook
localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 12/12
Batch 1 Block 1 B.TECH ENTC
Omkar Rane BETB118
Kaustubh Wankhade BETB129

More Related Content

What's hot

Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABBhavesh Shah
 
Regression_1.pdf
Regression_1.pdfRegression_1.pdf
Regression_1.pdfAmir Saleh
 
Computing Information Flow Using Symbolic-Model-Checking_.pdf
Computing Information Flow Using Symbolic-Model-Checking_.pdfComputing Information Flow Using Symbolic-Model-Checking_.pdf
Computing Information Flow Using Symbolic-Model-Checking_.pdfPolytechnique Montréal
 
Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms sathish sak
 
Boosted Tree-based Multinomial Logit Model for Aggregated Market Data
Boosted Tree-based Multinomial Logit Model for Aggregated Market DataBoosted Tree-based Multinomial Logit Model for Aggregated Market Data
Boosted Tree-based Multinomial Logit Model for Aggregated Market DataJay (Jianqiang) Wang
 
4. linear programming using excel solver
4. linear programming using excel solver4. linear programming using excel solver
4. linear programming using excel solverHakeem-Ur- Rehman
 
Active contour segmentation
Active contour segmentationActive contour segmentation
Active contour segmentationNishant Jain
 
Aoa amortized analysis
Aoa amortized analysisAoa amortized analysis
Aoa amortized analysisSalabat Khan
 
Linear models
Linear modelsLinear models
Linear modelsFAO
 
Support Vector Machines (SVM)
Support Vector Machines (SVM)Support Vector Machines (SVM)
Support Vector Machines (SVM)FAO
 
Biosight: Quantitative Methods for Policy Analysis - Introduction to GAMS, Li...
Biosight: Quantitative Methods for Policy Analysis - Introduction to GAMS, Li...Biosight: Quantitative Methods for Policy Analysis - Introduction to GAMS, Li...
Biosight: Quantitative Methods for Policy Analysis - Introduction to GAMS, Li...IFPRI-EPTD
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3Max Kleiner
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlabkrajeshk1980
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysisajmalcs
 
Introducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlowIntroducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlowEtsuji Nakai
 

What's hot (20)

Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
Regression_1.pdf
Regression_1.pdfRegression_1.pdf
Regression_1.pdf
 
Computing Information Flow Using Symbolic-Model-Checking_.pdf
Computing Information Flow Using Symbolic-Model-Checking_.pdfComputing Information Flow Using Symbolic-Model-Checking_.pdf
Computing Information Flow Using Symbolic-Model-Checking_.pdf
 
Matlab Nn Intro
Matlab Nn IntroMatlab Nn Intro
Matlab Nn Intro
 
09. amortized analysis
09. amortized analysis09. amortized analysis
09. amortized analysis
 
Amortized Analysis of Algorithms
Amortized Analysis of Algorithms Amortized Analysis of Algorithms
Amortized Analysis of Algorithms
 
Boosted Tree-based Multinomial Logit Model for Aggregated Market Data
Boosted Tree-based Multinomial Logit Model for Aggregated Market DataBoosted Tree-based Multinomial Logit Model for Aggregated Market Data
Boosted Tree-based Multinomial Logit Model for Aggregated Market Data
 
13 Amortized Analysis
13 Amortized Analysis13 Amortized Analysis
13 Amortized Analysis
 
4. linear programming using excel solver
4. linear programming using excel solver4. linear programming using excel solver
4. linear programming using excel solver
 
Active contour segmentation
Active contour segmentationActive contour segmentation
Active contour segmentation
 
Aoa amortized analysis
Aoa amortized analysisAoa amortized analysis
Aoa amortized analysis
 
Linear models
Linear modelsLinear models
Linear models
 
Support Vector Machines (SVM)
Support Vector Machines (SVM)Support Vector Machines (SVM)
Support Vector Machines (SVM)
 
Biosight: Quantitative Methods for Policy Analysis - Introduction to GAMS, Li...
Biosight: Quantitative Methods for Policy Analysis - Introduction to GAMS, Li...Biosight: Quantitative Methods for Policy Analysis - Introduction to GAMS, Li...
Biosight: Quantitative Methods for Policy Analysis - Introduction to GAMS, Li...
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3
 
Importance of matlab
Importance of matlabImportance of matlab
Importance of matlab
 
Amortized analysis
Amortized analysisAmortized analysis
Amortized analysis
 
Amortized
AmortizedAmortized
Amortized
 
Introducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlowIntroducton to Convolutional Nerural Network with TensorFlow
Introducton to Convolutional Nerural Network with TensorFlow
 
MUMS: Transition & SPUQ Workshop - Practical Bayesian Optimization for Urban ...
MUMS: Transition & SPUQ Workshop - Practical Bayesian Optimization for Urban ...MUMS: Transition & SPUQ Workshop - Practical Bayesian Optimization for Urban ...
MUMS: Transition & SPUQ Workshop - Practical Bayesian Optimization for Urban ...
 

Similar to Linear Regression (Machine Learning)

maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIIMax Kleiner
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Yao Yao
 
Competition 1 (blog 1)
Competition 1 (blog 1)Competition 1 (blog 1)
Competition 1 (blog 1)TarunPaparaju
 
Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62Max Kleiner
 
Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...Yao Yao
 
Assignment 6.1.pdf
Assignment 6.1.pdfAssignment 6.1.pdf
Assignment 6.1.pdfdash41
 
support vector regression
support vector regressionsupport vector regression
support vector regressionAkhilesh Joshi
 
Chapter 1: Linear Regression
Chapter 1: Linear RegressionChapter 1: Linear Regression
Chapter 1: Linear RegressionAkmelSyed
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfssuserb4d806
 
Essentials of machine learning algorithms
Essentials of machine learning algorithmsEssentials of machine learning algorithms
Essentials of machine learning algorithmsArunangsu Sahu
 
Multinomial Logistic Regression with Apache Spark
Multinomial Logistic Regression with Apache SparkMultinomial Logistic Regression with Apache Spark
Multinomial Logistic Regression with Apache SparkDB Tsai
 

Similar to Linear Regression (Machine Learning) (20)

working with python
working with pythonworking with python
working with python
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VII
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
 
Competition 1 (blog 1)
Competition 1 (blog 1)Competition 1 (blog 1)
Competition 1 (blog 1)
 
Introductio2.docx
Introductio2.docxIntroductio2.docx
Introductio2.docx
 
Kaggle KDD Cup Report
Kaggle KDD Cup ReportKaggle KDD Cup Report
Kaggle KDD Cup Report
 
Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62
 
Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...
 
Assignment 6.1.pdf
Assignment 6.1.pdfAssignment 6.1.pdf
Assignment 6.1.pdf
 
support vector regression
support vector regressionsupport vector regression
support vector regression
 
Xgboost
XgboostXgboost
Xgboost
 
ML .pptx
ML .pptxML .pptx
ML .pptx
 
BPstudy sklearn 20180925
BPstudy sklearn 20180925BPstudy sklearn 20180925
BPstudy sklearn 20180925
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
 
3ml.pdf
3ml.pdf3ml.pdf
3ml.pdf
 
Xgboost
XgboostXgboost
Xgboost
 
Chapter 1: Linear Regression
Chapter 1: Linear RegressionChapter 1: Linear Regression
Chapter 1: Linear Regression
 
AIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdfAIML4 CNN lab256 1hr (111-1).pdf
AIML4 CNN lab256 1hr (111-1).pdf
 
Essentials of machine learning algorithms
Essentials of machine learning algorithmsEssentials of machine learning algorithms
Essentials of machine learning algorithms
 
Multinomial Logistic Regression with Apache Spark
Multinomial Logistic Regression with Apache SparkMultinomial Logistic Regression with Apache Spark
Multinomial Logistic Regression with Apache Spark
 

More from Omkar Rane

Enabling SSL Elasticsearch on server
Enabling SSL Elasticsearch on serverEnabling SSL Elasticsearch on server
Enabling SSL Elasticsearch on serverOmkar Rane
 
Anti lock braking (ABS) Model based Design in MATLAB-Simulink
Anti lock braking (ABS) Model based Design in MATLAB-SimulinkAnti lock braking (ABS) Model based Design in MATLAB-Simulink
Anti lock braking (ABS) Model based Design in MATLAB-SimulinkOmkar Rane
 
Autosar fundamental
Autosar fundamentalAutosar fundamental
Autosar fundamentalOmkar Rane
 
Stress Management
Stress ManagementStress Management
Stress ManagementOmkar Rane
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot) Omkar Rane
 
Concept of Diversity & Fading (wireless communication)
Concept of Diversity & Fading (wireless communication)Concept of Diversity & Fading (wireless communication)
Concept of Diversity & Fading (wireless communication)Omkar Rane
 
Tata Motors GDC .LTD Internship
Tata Motors GDC .LTD Internship Tata Motors GDC .LTD Internship
Tata Motors GDC .LTD Internship Omkar Rane
 
Machine Learning Model for M.S admissions
Machine Learning Model for M.S admissionsMachine Learning Model for M.S admissions
Machine Learning Model for M.S admissionsOmkar Rane
 
Timer 0 programming on LPC 1768
Timer 0 programming on LPC 1768Timer 0 programming on LPC 1768
Timer 0 programming on LPC 1768Omkar Rane
 
ADC (Analog to Digital conversion) using LPC 1768
ADC (Analog to Digital conversion) using LPC 1768ADC (Analog to Digital conversion) using LPC 1768
ADC (Analog to Digital conversion) using LPC 1768Omkar Rane
 
PWM based motor speed control using LPC 1768
PWM based motor speed control using LPC 1768PWM based motor speed control using LPC 1768
PWM based motor speed control using LPC 1768Omkar Rane
 
UART interfacing on LPC1768 (Cortex M3 micro controller)
UART interfacing on LPC1768 (Cortex M3 micro controller)UART interfacing on LPC1768 (Cortex M3 micro controller)
UART interfacing on LPC1768 (Cortex M3 micro controller)Omkar Rane
 
LED Blinking logic on LPC1768
LED Blinking logic on LPC1768LED Blinking logic on LPC1768
LED Blinking logic on LPC1768Omkar Rane
 
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)Omkar Rane
 
Vlisi Course project presentation:Keypad Scanner
Vlisi Course project presentation:Keypad ScannerVlisi Course project presentation:Keypad Scanner
Vlisi Course project presentation:Keypad ScannerOmkar Rane
 
VlSI course project report : Keypad Scanner
VlSI course project report : Keypad Scanner VlSI course project report : Keypad Scanner
VlSI course project report : Keypad Scanner Omkar Rane
 
LPC 1768 A study on Real Time clock features
LPC 1768 A study on Real Time clock featuresLPC 1768 A study on Real Time clock features
LPC 1768 A study on Real Time clock featuresOmkar Rane
 
Nexys4ddr rm FPGA board Datasheet
Nexys4ddr rm  FPGA board DatasheetNexys4ddr rm  FPGA board Datasheet
Nexys4ddr rm FPGA board DatasheetOmkar Rane
 
transmission gate based design for 2:1 Multiplexer in micro-wind
transmission gate based design for 2:1 Multiplexer in micro-windtransmission gate based design for 2:1 Multiplexer in micro-wind
transmission gate based design for 2:1 Multiplexer in micro-windOmkar Rane
 
2:1 Multiplexer using NAND gate in Microwind
2:1 Multiplexer using NAND gate in Microwind 2:1 Multiplexer using NAND gate in Microwind
2:1 Multiplexer using NAND gate in Microwind Omkar Rane
 

More from Omkar Rane (20)

Enabling SSL Elasticsearch on server
Enabling SSL Elasticsearch on serverEnabling SSL Elasticsearch on server
Enabling SSL Elasticsearch on server
 
Anti lock braking (ABS) Model based Design in MATLAB-Simulink
Anti lock braking (ABS) Model based Design in MATLAB-SimulinkAnti lock braking (ABS) Model based Design in MATLAB-Simulink
Anti lock braking (ABS) Model based Design in MATLAB-Simulink
 
Autosar fundamental
Autosar fundamentalAutosar fundamental
Autosar fundamental
 
Stress Management
Stress ManagementStress Management
Stress Management
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot)
 
Concept of Diversity & Fading (wireless communication)
Concept of Diversity & Fading (wireless communication)Concept of Diversity & Fading (wireless communication)
Concept of Diversity & Fading (wireless communication)
 
Tata Motors GDC .LTD Internship
Tata Motors GDC .LTD Internship Tata Motors GDC .LTD Internship
Tata Motors GDC .LTD Internship
 
Machine Learning Model for M.S admissions
Machine Learning Model for M.S admissionsMachine Learning Model for M.S admissions
Machine Learning Model for M.S admissions
 
Timer 0 programming on LPC 1768
Timer 0 programming on LPC 1768Timer 0 programming on LPC 1768
Timer 0 programming on LPC 1768
 
ADC (Analog to Digital conversion) using LPC 1768
ADC (Analog to Digital conversion) using LPC 1768ADC (Analog to Digital conversion) using LPC 1768
ADC (Analog to Digital conversion) using LPC 1768
 
PWM based motor speed control using LPC 1768
PWM based motor speed control using LPC 1768PWM based motor speed control using LPC 1768
PWM based motor speed control using LPC 1768
 
UART interfacing on LPC1768 (Cortex M3 micro controller)
UART interfacing on LPC1768 (Cortex M3 micro controller)UART interfacing on LPC1768 (Cortex M3 micro controller)
UART interfacing on LPC1768 (Cortex M3 micro controller)
 
LED Blinking logic on LPC1768
LED Blinking logic on LPC1768LED Blinking logic on LPC1768
LED Blinking logic on LPC1768
 
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)
CAN interfacing on LPC1768 (ARM Cortex M3 based Micro controller)
 
Vlisi Course project presentation:Keypad Scanner
Vlisi Course project presentation:Keypad ScannerVlisi Course project presentation:Keypad Scanner
Vlisi Course project presentation:Keypad Scanner
 
VlSI course project report : Keypad Scanner
VlSI course project report : Keypad Scanner VlSI course project report : Keypad Scanner
VlSI course project report : Keypad Scanner
 
LPC 1768 A study on Real Time clock features
LPC 1768 A study on Real Time clock featuresLPC 1768 A study on Real Time clock features
LPC 1768 A study on Real Time clock features
 
Nexys4ddr rm FPGA board Datasheet
Nexys4ddr rm  FPGA board DatasheetNexys4ddr rm  FPGA board Datasheet
Nexys4ddr rm FPGA board Datasheet
 
transmission gate based design for 2:1 Multiplexer in micro-wind
transmission gate based design for 2:1 Multiplexer in micro-windtransmission gate based design for 2:1 Multiplexer in micro-wind
transmission gate based design for 2:1 Multiplexer in micro-wind
 
2:1 Multiplexer using NAND gate in Microwind
2:1 Multiplexer using NAND gate in Microwind 2:1 Multiplexer using NAND gate in Microwind
2:1 Multiplexer using NAND gate in Microwind
 

Recently uploaded

welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...Amil Baba Dawood bangali
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptNarmatha D
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 

Recently uploaded (20)

young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
NO1 Certified Black Magic Specialist Expert Amil baba in Uae Dubai Abu Dhabi ...
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Industrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.pptIndustrial Safety Unit-IV workplace health and safety.ppt
Industrial Safety Unit-IV workplace health and safety.ppt
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 

Linear Regression (Machine Learning)

  • 1. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 1/12 Linear Regression for Years of experience and Salary Dataset Gradient Descent in Linear Regression In linear regression, the model targets to get the best-fit regression line to predict the value of y based on the given input value (x). While training the model, the model calculates the cost function which measures the Root Mean Squared error between the predicted value (pred) and true value (y). The model targets to minimize the cost function. To minimize the cost function, the model needs to have the best value of θ1 and θ2. Initially model selects θ1 and θ2 values randomly and then itertively update these value in order to minimize the cost function untill it reaches the minimum. By the time model achieves the minimum cost function, it will have the best θ1 and θ2 values. Using these finally updated values of θ1 and θ2 in the hypothesis equation of linear equation, model predicts the value of x in the best manner it can. Therefore, the question arises – How θ1 and θ2 values get updated ?
  • 2. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 2/12
  • 3. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 3/12 -> θj : Weights of the hypothesis. -> hθ(xi) : predicted y value for ith input. -> j : Feature index number (can be 0, 1, 2, ......, n). -> α : Learning Rate of Gradient Descent. We graph cost function as a function of parameter estimates i.e. parameter range of our hypothesis function and the cost resulting from selecting a particular set of parameters. We move downward towards pits in the graph, to find the minimum value. Way to do this is taking derivative of cost function as explained in the above figure. Gradient Descent step downs the cost function in the direction of the steepest descent. Size of each step is determined by parameter α known as Learning Rate. In the Gradient Descent algorithm, one can infer two points : 1.If slope is +ve : θj = θj – (+ve value). Hence value of θj decreases.
  • 4. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 4/12 2. If slope is -ve : θj = θj – (-ve value). Hence value of θj increases. The choice of correct learning rate is very important as it ensures that Gradient Descent converges in a reasonable time. : 1.If we choose α to be very large, Gradient Descent can overshoot the minimum. It may fail to converge or even diverge. 2.If we choose α to be very small, Gradient Descent will take small steps to reach local minima and will take a longer time to reach minima. For linear regression Cost Function graph is always convex shaped. Reference: geeksforgeeks.org/gradient-descent-in-linear-regression/ (Refered For Theory)
  • 5. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 5/12 First, we import a few libraries- In [10]: Data Preprocessing & importing Dataset The next step is to import our dataset ‘Salary_Data.csv’ then split them into input (independent) variables and output (dependent) variable. When you deal with real datasets, you usually have around thousands of rows but since the one I have taken here is a sample, this has just 30 rows. So when we split our data into a training set and a testing set, we split it in 1/3, i.e., 20 rows go into the training set and the rest 10 make it to the testing set. In [11]: Plotting Default Dataset Out[11]: YearsExperience Salary 0 1.1 39343.0 1 1.3 46205.0 2 1.5 37731.0 3 2.0 43525.0 4 2.2 39891.0 import numpy as np import pandas as pd import matplotlib.pyplot as plt dataset = pd.read_csv('Salary_Data.csv') x = dataset.iloc[:, :-1].values y = dataset.iloc[:, 1].values data_top = dataset.head() #Dataset display data_top
  • 6. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 6/12 In [12]: Training & Split Split arrays or matrices into random train and test subsets In [13]: Linear Regression Now, we will import the linear regression class, create an object of that class, which is the linear regression model. In [14]: Fitting Data Then we will use the fit method to “fit” the model to our dataset. What this does is nothing but make the regressor “study” our data and “learn” from it. plt.scatter(x, y, color = "red") plt.plot(x,y, color = "green") plt.title("Salary vs Experience (Dataset)") plt.xlabel("Years of Experience") plt.ylabel("Salary") plt.show() from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 1/3) from sklearn.linear_model import LinearRegression lr = LinearRegression()
  • 7. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 7/12 In [15]: Testing Now that we have created our model and trained it, it is time we test the model with our testing dataset. In [16]: Data Visualization for Training Dataset First, we make use of a scatter plot to plot the actual observations, with x_train on the x-axis and y_train on the y-axis. For the regression line, we will use x_train on the x-axis and then the predictions of the x_train observations on the y-axis. We add a touch of aesthetics by coloring the original observations in red and the regression line in green. In [17]: Data Visualization for Testing Dataset repeat the same task for our testing dataset, and we get the following code- Out[15]: LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=Fal se) lr.fit(x_train, y_train) y_pred = lr.predict(x_test) plt.scatter(x_train, y_train, color = "red") plt.plot(x_train, lr.predict(x_train), color = "green") plt.title("Salary vs Experience (Training set)") plt.xlabel("Years of Experience") plt.ylabel("Salary") plt.show()
  • 8. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 8/12 In [24]: Linear Regression with Gradient Descent Algorithm Approach for Years of experience and Salary Dataset Importing libraries,Dataset & data preprocessing plt.scatter(x_test, y_test, color = "red") plt.plot(x_train, lr.predict(x_train), color = "green") plt.title("Salary vs Experience (Testing set)") plt.xlabel("Years of Experience") plt.ylabel("Salary") plt.show()
  • 9. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 9/12 In [27]: # Making the imports import numpy as np import pandas as pd import matplotlib.pyplot as plt plt.rcParams['figure.figsize'] = (12.0, 9.0) # Preprocessing Input data data = pd.read_csv('Salary_Data.csv') X = data.iloc[:, 0] Y = data.iloc[:, 1] #Plotting Data for visualization plt.scatter(X, Y) plt.title("Salary vs Experience (Dataset set)") plt.xlabel("Years of Experience") plt.ylabel("Salary") plt.show()
  • 10. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 10/12 Optimizing parameter value like intercept "c" and slope "m" & learning rate alpha in Gradient descent formula In [26]: Prediction Slope & Intercept: 12836.600965885045 2915.2044856014018 # Building the model m = 0 c = 0 L = 0.0001 # The learning Rate #L = 0.0001 # The learning Rate #L = 0.0002 # The learning Rate #L = 0.0003 # The learning Rate epochs = 1000 # The number of iterations to perform gradient descent n = float(len(X)) # Number of elements in X # Performing Gradient Descent for i in range(epochs): Y_pred = m*X + c # The current predicted value of Y D_m = (-2/n) * sum(X * (Y - Y_pred)) # Derivative wrt m D_c = (-2/n) * sum(Y - Y_pred) # Derivative wrt c m = m - L * D_m # Update m c = c - L * D_c # Update c print("Slope & Intercept:") print (m, c)
  • 11. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 11/12 In [28]: # Making predictions Y_pred = m*X + c plt.scatter(X, Y) plt.plot([min(X), max(X)], [min(Y_pred), max(Y_pred)], color='red') # regression line plt.title("Salary vs Experience (prediction)") plt.xlabel("Years of Experience") plt.ylabel("Salary") plt.show()
  • 12. 9/17/2019 ML_Activity_1 - Jupyter Notebook localhost:8890/notebooks/ML Activity/ML_Activity_1.ipynb 12/12 Batch 1 Block 1 B.TECH ENTC Omkar Rane BETB118 Kaustubh Wankhade BETB129