Linear Regression
Linear Regression
2
Linear Regression: Pictorially
▪ Linear regression is like fitting a line or (hyper)plane to a set of points 𝑧1
𝑧2 = 𝜙(𝑥)
(Output 𝑦)
modeled by a nonlinear (Output 𝑦)
curve or curved surface?
▪ The line/plane must also predict outputs the unseen (test) inputs well
Simplest Possible Linear Regression Model
This is the base model for all statistical
machine learning
x is a one feature data variable
y is the value we are trying to predict
The regression model is
y = w0 + w1 x +
Two parameters to estimate – the slope of
the line w1 and the y-intercept w0
ε is the unexplained, random, or error
component.
Solving the regression problem
We basically want to find {w0, w1} that
minimize deviations from the predictor
line
How do we do it?
Iterate over all possible w values
along the two dimensions?
Same, but smarter? [next class]
No, we can do this in closed form with
just plain calculus
Very few optimization problems in ML
have closed form solutions
The ones that do are interesting for
that reason
Parameter estimation via calculus
We just need to set the partial derivatives
to zero (full derivation)
Simplifying
(𝑥𝑦) − (𝑥)(
ҧ 𝑦)
ത
=
(𝑥𝑖2 ) − (𝑥)ҧ 2
Problem
Xi(week) Yi (Sales in Thousands)
1 1.2
2 1.8
3 2.6
4 3.2
5 3.8
Evaluation
x y x2 x*y
1 1.2 1 1.2
2 1.8 4 3.6
3 2.6 9 7.8
4 3.2 16 12.8
5 3.8 25 19
Average=15/5=3 Average=12.6/5=2.52 Average=55/5=11 Average=44.4/5=8.88
Slope=(8.88-3(2.52))/(11- Intercept=2.52-0.66*3= Line: y=0.54-0.66*x
32)=0.66 0.54
Validation
• Standard Error: difference between actual and predicted value
1 𝑛−1
• Mean Absolute Error(MAE) : σ𝑖=0 𝑦𝑖 − 𝑦ෝ𝑖
𝑛
1 𝑛−1
• Mean squared Error (MSE): σ𝑖=0 𝑦𝑖 − 𝑦ෝ𝑖 2
𝑛
• Root Mean Square Error(RMSE): Root of MSE
• Relative MSE: ratio of the prediction ability of y cap to the average of
the trivial population
σ𝑛−1 𝑦𝑖 2
𝑖=0 𝑦𝑖 −ෞ
• σ𝑛−1 2
𝑖=0 𝑦𝑖 −𝑦𝑖