Linear Regression
Linear Regression
Roadmap
• How linear regression works?
Given that we are looking at history in order to estimate a new price point, it
becomes a regression problem
The fact that price and demand are linearly related to each other (the higher the price,
the lower the demand and vice versa) makes it a linear regression problem.
Variables: Dependent
and Independent
• For example, ice cream sales are influenced by temperature, but they
are also influenced by the price at which ice cream is being sold, along
with other factors such as location, ice cream brand, and so on
The baby’s weight starts at 3 (a, the bias) and increases linearly by 0.75 (b, the slope) every month.
Note that, a bias term is the value of the dependent variable when all the independent variables are 0
Formalizing Simple Linear
Regression
• The slope of a line is the difference
• A simple linear regression is represented between the x and y coordinates at
as: both extremes of the line upon the
length of line
Y = a + b*X
• Y is the dependent variable that we are
predicting for.
• X is the independent variable.
• a is the bias term.
• b is the slope of the variable (the
weight assigned to the independent
variable).
Example
• Let’s take a real world example of the price of agricultural products
and how it varies based on the location its sold. The price will be low
when bought directly from farmers and high when brought from the
downtown area.
• Given this dataset, we can predict the price of the product in
intermediate locations.
• The Mean Absolute Error (MAE) is a very good KPI to measure forecast
accuracy.
• Error basically is the absolute difference between the actual or true values
and the values that are predicted.
• Absolute difference means that if the result has a negative sign, it is ignored.
• Hence, MAE = True values – Predicted values
• MAE takes the average of this error from every sample in a dataset and gives
the output.
MAE Vs RMSE
Error is given in terms of the RMSE is more sensitive to
value you are predicting for outliers
In general, the higher the R-squared, the better the model fits your
data.
R2
R2
R2
R2
R2
R2
Linear Regression-- R2
Formula for R Squared
Example 1
X = 4, 8 ,12, 16 and
• https://www.mathsisfun.com/data/least-squares-regression.html
• https://towardsdatascience.com/implementation-linear-regression-in-
python-in-5-minutes-from-scratch-f111c8cc5c99
• https://towardsdatascience.com/mathematics-for-machine-learning-li
near-regression-least-square-regression-de09cf53757c