SlideShare a Scribd company logo
1 of 21
Download to read offline
Lesson 12
Machine Learning - Regression Kush Kulshrestha
Introduction
It has long been known that crickets (an insect species) chirp more frequently on hotter days than on cooler days.
For decades, professional and amateur scientists have catalogued data on chirps-per-minute and temperature.
Using this data, you want to explore this relationship.
Introduction
As expected, the plot shows the temperature rising with the number of chirps. Is this relationship between chirps
and temperature linear?
Yes, you could draw a single straight line like the following to approximate this relationship: True, the line doesn't
pass through every dot, but the line does clearly show the relationship between chirps and temperature.
Introduction
Using the equation for a line, you could write down this
relationship as follows:
where,
- y is the temperature in Celsius—the value we're trying
to predict.
- m is the slope of the line.
- x is the number of chirps per minute—the value of our
input feature.
- b is the y-intercept
Introduction
By convention in machine learning, you'll write the above equation for a model slightly differently:
where,
- y’ is the predicted label (a desired output).
- b is the bias (the y-intercept), sometimes referred to as wo
- w1 is the weight of feature 1. Weight is the same concept as the "slope“ m in the traditional equation of the line.
- X1 is the feature.
To infer (predict) the temperature y’ for a new chirps-per-minute value x1, just substitute the x1 value into this
model.
Although this model uses only one feature, a more sophisticated model might rely on multiple features, each
having a separate weight (w1, w2 , etc.). For example, a model that relies on three features might look as follows:
What exactly is Training ?
Training a model simply means learning (determining) good values for all the weights and the bias from labelled
examples. In supervised learning, a machine learning algorithm builds a model by examining many examples and
attempting to find a model that minimizes loss.
Loss is the penalty for a bad prediction. That is, loss is a number indicating how bad the model's prediction was on
a single example. If the model's prediction is perfect, the loss is zero; otherwise, the loss is greater. The goal of
training a model is to find a set of weights and biases that have low loss, on average, across all examples.
Example, Figure 3 shows a high loss model on the left and a low loss model on the right.
Linear Regression
Linear regression is very good to answer the following questions:
• Is there a relationship between 2 variables?
• How strong is the relationship?
• Which variable contributes the most?
• How accurately can we estimate the effect of each variable?
• How accurately can we predict the target?
• Is the relationship linear?
• Is there an interaction effect?
Let’s assume we only have one variable and one target. Then, linear regression is expressed as:
In the equation above, the betas are the coefficients. These coefficients are what we need in order to make
predictions with our model.
To find the parameters, we need to minimize the least squares or the sum of squared errors.
Why do we use squared errors?
Linear Regression
In the below above, the red dots are the true data and the blue line is linear model. The grey lines illustrate the
errors between the predicted and the true values. The blue line is thus the one that minimizes the sum of the
squared length of the grey lines.
Linear Regression
After some math heavy lifting, you can finally estimate the coefficients with the following equations:
where x bar and y bar represent the mean.
Correlation coefficient
β1 can also be written as:
β1
where,
Sy and Sx are standard deviations of x and y values respectively, and r is the correlation coefficient defined as,
By examining the second equation for the estimated slope β1, we see that since sample standard deviations Sx and
Sy are positive quantities, the correlation coefficient (r), which is always between−1 and 1, measures how much x is
related to y and whether the trend is positive or negative. Figure 3.2 illustrates different correlation strengths.
Correlation coefficient
Figure below illustrates different correlation strengths.
An illustration of correlation strength. Each plot shows data with a particular correlation coefficient r. Values farther
than 0 (outside) indicate a stronger relationship than values closer to 0 (inside). Negative values (left) indicate an
inverse relationship, while positive values (right)indicate a direct relationship.
Coefficient of Determination
The square of the correlation coefficient, r^2 will always be positive and is called the coefficient of determination.
This also is equal to the proportion of the total variability that’s explained by a linear model.
As an extremely crucial remark, correlation does not imply causation!
Correlation and Causation
Just because there’s a strong correlation between two variables, there isn’t necessarily a causal relationship
between them.
For example, drowning deaths and ice-cream sales are strongly correlated, but that’s because both are affected by
the season (summer vs. winter). In general, there are several possible cases, as illustrated below:
1) Causal Link: Even if there is a causal link between x and y, correlation alone cannot tell us whether y causes x or x
causes y.
Correlation and Causation
2) Hidden Cause: A hidden variable z causes both x and y, creating the correlation.
3) Confounding Factor: A hidden variable z and x both affect y, so the results also depend on the value of z.
4) Coincidence: The correlation just happened by chance (e.g. the strong correlation between sun cycles and
number of Republicans in Congress)
Multiple Linear Regression
This is the case when instead of being single x value, we have a vector of x values (x1, x2, ……, xn) for every data
point i.
So, we have n data points (just like before), each with p different predictor variables or features. We’ll then try to
predict y for each data point as a linear function of the different x variables:
Even though it’s still linear, this representation is very versatile; here are just a few of thethings we can represent
with it:
• Multiple dependent variables: for example, suppose we’re trying to predict medical outcome as a function of
several variables such as age, genetic susceptibility, and clinical diagnosis. Then we might say that for each
patient, x1= age, x2= genetics, x3=diagnosis, and y= outcome.
• Nonlinearities: Suppose we want to predict a quadratic function y=ax^2 + bx + c, then for each data point we
might say x1= 1 , x2=x , and x3=x^2. This can easily be extended to any nonlinear function we want.
One may ask: why not just use multiple linear regression and fit an extremely high-degree polynomial to our data?
While the model then would be much richer, one runs the risk of overfitting
Multiple Linear Regression
Using too many features or too complex of a model can often lead to overfitting.
Suppose we want to fit a model to the points in Figure 1. If we fit a linear model, it might look like Figure 2. But, the
fit isn’t perfect. What if we use our newly acquired multiple regression powers to fit a 6th order polynomial to
these points? The result is shown in Figure 3
While our errors are definitely smaller than they were with the linear model, the new model is far too complex, and
will likely go wrong for values too far outside the range.
Application – Linear Regression in Scikit Learn
Please check out the jupyter notebook
Application – Linear Regression in Statsmodels
Please check out the jupyter notebook
Machine Learning Algorithm - Linear Regression
Machine Learning Algorithm - Linear Regression
Machine Learning Algorithm - Linear Regression

More Related Content

What's hot

Scatter plots
Scatter plotsScatter plots
Scatter plots
swartzje
 
Chapter 2 part3-Least-Squares Regression
Chapter 2 part3-Least-Squares RegressionChapter 2 part3-Least-Squares Regression
Chapter 2 part3-Least-Squares Regression
nszakir
 
Geographical Skills
Geographical SkillsGeographical Skills
Geographical Skills
clemaitre
 

What's hot (19)

Simple (and Simplistic) Introduction to Econometrics and Linear Regression
Simple (and Simplistic) Introduction to Econometrics and Linear RegressionSimple (and Simplistic) Introduction to Econometrics and Linear Regression
Simple (and Simplistic) Introduction to Econometrics and Linear Regression
 
Scatterplots, Correlation, and Regression
Scatterplots, Correlation, and RegressionScatterplots, Correlation, and Regression
Scatterplots, Correlation, and Regression
 
Scatter plots
Scatter plotsScatter plots
Scatter plots
 
Displaying Distributions with Graphs
Displaying Distributions with GraphsDisplaying Distributions with Graphs
Displaying Distributions with Graphs
 
Graphs that Enlighten and Graphs that Deceive
Graphs that Enlighten and Graphs that DeceiveGraphs that Enlighten and Graphs that Deceive
Graphs that Enlighten and Graphs that Deceive
 
Scatter diagrams and correlation
Scatter diagrams and correlationScatter diagrams and correlation
Scatter diagrams and correlation
 
Scattergrams
ScattergramsScattergrams
Scattergrams
 
Chapter 2 part3-Least-Squares Regression
Chapter 2 part3-Least-Squares RegressionChapter 2 part3-Least-Squares Regression
Chapter 2 part3-Least-Squares Regression
 
Scatter plot- Complete
Scatter plot- CompleteScatter plot- Complete
Scatter plot- Complete
 
Machine Learning Algorithm - KNN
Machine Learning Algorithm - KNNMachine Learning Algorithm - KNN
Machine Learning Algorithm - KNN
 
Histograms
HistogramsHistograms
Histograms
 
Quantitative Methods for Lawyers - Class #19 - Regression Analysis - Part 2
Quantitative Methods for Lawyers - Class #19 - Regression Analysis - Part 2Quantitative Methods for Lawyers - Class #19 - Regression Analysis - Part 2
Quantitative Methods for Lawyers - Class #19 - Regression Analysis - Part 2
 
Quantitative Methods for Lawyers - Class #17 - Scatter Plots, Covariance, Cor...
Quantitative Methods for Lawyers - Class #17 - Scatter Plots, Covariance, Cor...Quantitative Methods for Lawyers - Class #17 - Scatter Plots, Covariance, Cor...
Quantitative Methods for Lawyers - Class #17 - Scatter Plots, Covariance, Cor...
 
Scatter Plots
Scatter PlotsScatter Plots
Scatter Plots
 
Geographical Skills
Geographical SkillsGeographical Skills
Geographical Skills
 
assignment 2
assignment 2assignment 2
assignment 2
 
Quantitative Methods for Lawyers - Class #22 - Regression Analysis - Part 5
Quantitative Methods for Lawyers - Class #22 - Regression Analysis - Part 5Quantitative Methods for Lawyers - Class #22 - Regression Analysis - Part 5
Quantitative Methods for Lawyers - Class #22 - Regression Analysis - Part 5
 
Quantitative Methods for Lawyers - Class #21 - Regression Analysis - Part 4
Quantitative Methods for Lawyers - Class #21 - Regression Analysis - Part 4Quantitative Methods for Lawyers - Class #21 - Regression Analysis - Part 4
Quantitative Methods for Lawyers - Class #21 - Regression Analysis - Part 4
 
Machine learning session4(linear regression)
Machine learning   session4(linear regression)Machine learning   session4(linear regression)
Machine learning session4(linear regression)
 

Similar to Machine Learning Algorithm - Linear Regression

The future is uncertain. Some events do have a very small probabil.docx
The future is uncertain. Some events do have a very small probabil.docxThe future is uncertain. Some events do have a very small probabil.docx
The future is uncertain. Some events do have a very small probabil.docx
oreo10
 
For this assignment, use the aschooltest.sav dataset.The d
For this assignment, use the aschooltest.sav dataset.The dFor this assignment, use the aschooltest.sav dataset.The d
For this assignment, use the aschooltest.sav dataset.The d
MerrileeDelvalle969
 
How to draw a good graph
How to draw a good graphHow to draw a good graph
How to draw a good graph
Tarun Gehlot
 
Requirements.docxRequirementsFont Times New RomanI NEED .docx
Requirements.docxRequirementsFont Times New RomanI NEED .docxRequirements.docxRequirementsFont Times New RomanI NEED .docx
Requirements.docxRequirementsFont Times New RomanI NEED .docx
heunice
 
Maths A - Chapter 11
Maths A - Chapter 11Maths A - Chapter 11
Maths A - Chapter 11
westy67968
 
Chapter 9 Regression
Chapter 9 RegressionChapter 9 Regression
Chapter 9 Regression
ghalan
 
RegressionMaking predictions using dataLimitations.docx
RegressionMaking predictions using dataLimitations.docxRegressionMaking predictions using dataLimitations.docx
RegressionMaking predictions using dataLimitations.docx
debishakespeare
 

Similar to Machine Learning Algorithm - Linear Regression (20)

REGRESSION ANALYSIS THEORY EXPLAINED HERE
REGRESSION ANALYSIS THEORY EXPLAINED HEREREGRESSION ANALYSIS THEORY EXPLAINED HERE
REGRESSION ANALYSIS THEORY EXPLAINED HERE
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Correlation and regression
Correlation and regressionCorrelation and regression
Correlation and regression
 
Chapter III.pptx
Chapter III.pptxChapter III.pptx
Chapter III.pptx
 
The future is uncertain. Some events do have a very small probabil.docx
The future is uncertain. Some events do have a very small probabil.docxThe future is uncertain. Some events do have a very small probabil.docx
The future is uncertain. Some events do have a very small probabil.docx
 
For this assignment, use the aschooltest.sav dataset.The d
For this assignment, use the aschooltest.sav dataset.The dFor this assignment, use the aschooltest.sav dataset.The d
For this assignment, use the aschooltest.sav dataset.The d
 
Data Science - Part XII - Ridge Regression, LASSO, and Elastic Nets
Data Science - Part XII - Ridge Regression, LASSO, and Elastic NetsData Science - Part XII - Ridge Regression, LASSO, and Elastic Nets
Data Science - Part XII - Ridge Regression, LASSO, and Elastic Nets
 
Correlation
CorrelationCorrelation
Correlation
 
How to draw a good graph
How to draw a good graphHow to draw a good graph
How to draw a good graph
 
Requirements.docxRequirementsFont Times New RomanI NEED .docx
Requirements.docxRequirementsFont Times New RomanI NEED .docxRequirements.docxRequirementsFont Times New RomanI NEED .docx
Requirements.docxRequirementsFont Times New RomanI NEED .docx
 
Maths A - Chapter 11
Maths A - Chapter 11Maths A - Chapter 11
Maths A - Chapter 11
 
Correlation and regression
Correlation and regressionCorrelation and regression
Correlation and regression
 
correlation and regression
correlation and regressioncorrelation and regression
correlation and regression
 
Chapter 2 Simple Linear Regression Model.pptx
Chapter 2 Simple Linear Regression Model.pptxChapter 2 Simple Linear Regression Model.pptx
Chapter 2 Simple Linear Regression Model.pptx
 
Regression
RegressionRegression
Regression
 
Chapter 9 Regression
Chapter 9 RegressionChapter 9 Regression
Chapter 9 Regression
 
2-20-04.ppt
2-20-04.ppt2-20-04.ppt
2-20-04.ppt
 
RegressionMaking predictions using dataLimitations.docx
RegressionMaking predictions using dataLimitations.docxRegressionMaking predictions using dataLimitations.docx
RegressionMaking predictions using dataLimitations.docx
 
Regression
RegressionRegression
Regression
 
TTests.ppt
TTests.pptTTests.ppt
TTests.ppt
 

More from Kush Kulshrestha

More from Kush Kulshrestha (12)

Machine Learning Algorithm - Decision Trees
Machine Learning Algorithm - Decision Trees Machine Learning Algorithm - Decision Trees
Machine Learning Algorithm - Decision Trees
 
Machine Learning Algorithm - Naive Bayes for Classification
Machine Learning Algorithm - Naive Bayes for ClassificationMachine Learning Algorithm - Naive Bayes for Classification
Machine Learning Algorithm - Naive Bayes for Classification
 
Machine Learning Algorithm - Logistic Regression
Machine Learning Algorithm - Logistic RegressionMachine Learning Algorithm - Logistic Regression
Machine Learning Algorithm - Logistic Regression
 
Interpreting Regression Results - Machine Learning
Interpreting Regression Results - Machine LearningInterpreting Regression Results - Machine Learning
Interpreting Regression Results - Machine Learning
 
General Concepts of Machine Learning
General Concepts of Machine LearningGeneral Concepts of Machine Learning
General Concepts of Machine Learning
 
Performance Metrics for Machine Learning Algorithms
Performance Metrics for Machine Learning AlgorithmsPerformance Metrics for Machine Learning Algorithms
Performance Metrics for Machine Learning Algorithms
 
Inferential Statistics
Inferential StatisticsInferential Statistics
Inferential Statistics
 
Descriptive Statistics
Descriptive StatisticsDescriptive Statistics
Descriptive Statistics
 
Scaling and Normalization
Scaling and NormalizationScaling and Normalization
Scaling and Normalization
 
Wireless Charging of Electric Vehicles
Wireless Charging of Electric VehiclesWireless Charging of Electric Vehicles
Wireless Charging of Electric Vehicles
 
Time management
Time managementTime management
Time management
 
Handshakes and their types
Handshakes and their typesHandshakes and their types
Handshakes and their types
 

Recently uploaded

Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
mikehavy0
 
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
zifhagzkk
 
sourabh vyas1222222222222222222244444444
sourabh vyas1222222222222222222244444444sourabh vyas1222222222222222222244444444
sourabh vyas1222222222222222222244444444
saurabvyas476
 
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get CytotecAbortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
q6pzkpark
 
Abortion pills in Riyadh Saudi Arabia| +966572737505 | Get Cytotec, Unwanted Kit
Abortion pills in Riyadh Saudi Arabia| +966572737505 | Get Cytotec, Unwanted KitAbortion pills in Riyadh Saudi Arabia| +966572737505 | Get Cytotec, Unwanted Kit
Abortion pills in Riyadh Saudi Arabia| +966572737505 | Get Cytotec, Unwanted Kit
Abortion pills in Riyadh +966572737505 get cytotec
 
Huawei Ransomware Protection Storage Solution Technical Overview Presentation...
Huawei Ransomware Protection Storage Solution Technical Overview Presentation...Huawei Ransomware Protection Storage Solution Technical Overview Presentation...
Huawei Ransomware Protection Storage Solution Technical Overview Presentation...
LuisMiguelPaz5
 
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotecAbortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 

Recently uploaded (20)

Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
 
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
Abortion Clinic in Kempton Park +27791653574 WhatsApp Abortion Clinic Service...
 
DAA Assignment Solution.pdf is the best1
DAA Assignment Solution.pdf is the best1DAA Assignment Solution.pdf is the best1
DAA Assignment Solution.pdf is the best1
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024
 
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
 
sourabh vyas1222222222222222222244444444
sourabh vyas1222222222222222222244444444sourabh vyas1222222222222222222244444444
sourabh vyas1222222222222222222244444444
 
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
Identify Rules that Predict Patient’s Heart Disease - An Application of Decis...
 
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get CytotecAbortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
 
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
SAC 25 Final National, Regional & Local Angel Group Investing Insights 2024 0...
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Abortion pills in Riyadh Saudi Arabia| +966572737505 | Get Cytotec, Unwanted Kit
Abortion pills in Riyadh Saudi Arabia| +966572737505 | Get Cytotec, Unwanted KitAbortion pills in Riyadh Saudi Arabia| +966572737505 | Get Cytotec, Unwanted Kit
Abortion pills in Riyadh Saudi Arabia| +966572737505 | Get Cytotec, Unwanted Kit
 
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarjSCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
 
Huawei Ransomware Protection Storage Solution Technical Overview Presentation...
Huawei Ransomware Protection Storage Solution Technical Overview Presentation...Huawei Ransomware Protection Storage Solution Technical Overview Presentation...
Huawei Ransomware Protection Storage Solution Technical Overview Presentation...
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshare
 
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotecAbortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchers
 

Machine Learning Algorithm - Linear Regression

  • 1. Lesson 12 Machine Learning - Regression Kush Kulshrestha
  • 2. Introduction It has long been known that crickets (an insect species) chirp more frequently on hotter days than on cooler days. For decades, professional and amateur scientists have catalogued data on chirps-per-minute and temperature. Using this data, you want to explore this relationship.
  • 3. Introduction As expected, the plot shows the temperature rising with the number of chirps. Is this relationship between chirps and temperature linear? Yes, you could draw a single straight line like the following to approximate this relationship: True, the line doesn't pass through every dot, but the line does clearly show the relationship between chirps and temperature.
  • 4. Introduction Using the equation for a line, you could write down this relationship as follows: where, - y is the temperature in Celsius—the value we're trying to predict. - m is the slope of the line. - x is the number of chirps per minute—the value of our input feature. - b is the y-intercept
  • 5. Introduction By convention in machine learning, you'll write the above equation for a model slightly differently: where, - y’ is the predicted label (a desired output). - b is the bias (the y-intercept), sometimes referred to as wo - w1 is the weight of feature 1. Weight is the same concept as the "slope“ m in the traditional equation of the line. - X1 is the feature. To infer (predict) the temperature y’ for a new chirps-per-minute value x1, just substitute the x1 value into this model. Although this model uses only one feature, a more sophisticated model might rely on multiple features, each having a separate weight (w1, w2 , etc.). For example, a model that relies on three features might look as follows:
  • 6. What exactly is Training ? Training a model simply means learning (determining) good values for all the weights and the bias from labelled examples. In supervised learning, a machine learning algorithm builds a model by examining many examples and attempting to find a model that minimizes loss. Loss is the penalty for a bad prediction. That is, loss is a number indicating how bad the model's prediction was on a single example. If the model's prediction is perfect, the loss is zero; otherwise, the loss is greater. The goal of training a model is to find a set of weights and biases that have low loss, on average, across all examples. Example, Figure 3 shows a high loss model on the left and a low loss model on the right.
  • 7. Linear Regression Linear regression is very good to answer the following questions: • Is there a relationship between 2 variables? • How strong is the relationship? • Which variable contributes the most? • How accurately can we estimate the effect of each variable? • How accurately can we predict the target? • Is the relationship linear? • Is there an interaction effect? Let’s assume we only have one variable and one target. Then, linear regression is expressed as: In the equation above, the betas are the coefficients. These coefficients are what we need in order to make predictions with our model. To find the parameters, we need to minimize the least squares or the sum of squared errors. Why do we use squared errors?
  • 8. Linear Regression In the below above, the red dots are the true data and the blue line is linear model. The grey lines illustrate the errors between the predicted and the true values. The blue line is thus the one that minimizes the sum of the squared length of the grey lines.
  • 9. Linear Regression After some math heavy lifting, you can finally estimate the coefficients with the following equations: where x bar and y bar represent the mean.
  • 10. Correlation coefficient β1 can also be written as: β1 where, Sy and Sx are standard deviations of x and y values respectively, and r is the correlation coefficient defined as, By examining the second equation for the estimated slope β1, we see that since sample standard deviations Sx and Sy are positive quantities, the correlation coefficient (r), which is always between−1 and 1, measures how much x is related to y and whether the trend is positive or negative. Figure 3.2 illustrates different correlation strengths.
  • 11. Correlation coefficient Figure below illustrates different correlation strengths. An illustration of correlation strength. Each plot shows data with a particular correlation coefficient r. Values farther than 0 (outside) indicate a stronger relationship than values closer to 0 (inside). Negative values (left) indicate an inverse relationship, while positive values (right)indicate a direct relationship.
  • 12. Coefficient of Determination The square of the correlation coefficient, r^2 will always be positive and is called the coefficient of determination. This also is equal to the proportion of the total variability that’s explained by a linear model. As an extremely crucial remark, correlation does not imply causation!
  • 13. Correlation and Causation Just because there’s a strong correlation between two variables, there isn’t necessarily a causal relationship between them. For example, drowning deaths and ice-cream sales are strongly correlated, but that’s because both are affected by the season (summer vs. winter). In general, there are several possible cases, as illustrated below: 1) Causal Link: Even if there is a causal link between x and y, correlation alone cannot tell us whether y causes x or x causes y.
  • 14. Correlation and Causation 2) Hidden Cause: A hidden variable z causes both x and y, creating the correlation. 3) Confounding Factor: A hidden variable z and x both affect y, so the results also depend on the value of z. 4) Coincidence: The correlation just happened by chance (e.g. the strong correlation between sun cycles and number of Republicans in Congress)
  • 15. Multiple Linear Regression This is the case when instead of being single x value, we have a vector of x values (x1, x2, ……, xn) for every data point i. So, we have n data points (just like before), each with p different predictor variables or features. We’ll then try to predict y for each data point as a linear function of the different x variables: Even though it’s still linear, this representation is very versatile; here are just a few of thethings we can represent with it: • Multiple dependent variables: for example, suppose we’re trying to predict medical outcome as a function of several variables such as age, genetic susceptibility, and clinical diagnosis. Then we might say that for each patient, x1= age, x2= genetics, x3=diagnosis, and y= outcome. • Nonlinearities: Suppose we want to predict a quadratic function y=ax^2 + bx + c, then for each data point we might say x1= 1 , x2=x , and x3=x^2. This can easily be extended to any nonlinear function we want. One may ask: why not just use multiple linear regression and fit an extremely high-degree polynomial to our data? While the model then would be much richer, one runs the risk of overfitting
  • 16. Multiple Linear Regression Using too many features or too complex of a model can often lead to overfitting. Suppose we want to fit a model to the points in Figure 1. If we fit a linear model, it might look like Figure 2. But, the fit isn’t perfect. What if we use our newly acquired multiple regression powers to fit a 6th order polynomial to these points? The result is shown in Figure 3 While our errors are definitely smaller than they were with the linear model, the new model is far too complex, and will likely go wrong for values too far outside the range.
  • 17. Application – Linear Regression in Scikit Learn Please check out the jupyter notebook
  • 18. Application – Linear Regression in Statsmodels Please check out the jupyter notebook