Professional Documents
Culture Documents
Linear Regression
with Multiple Variables
Embedded System
-1-
1 Introduction
Outline
Multiple features
Normal equation
-2-
1 Introduction
Outline
Multiple features
Normal equation
-3-
1 Introduction
-4-
1 Introduction
2104 5 1 45 460
1416 3 2 40 232
1534 3 2 30 315
852 2 1 36 178
-5-
1 Introduction
Notation:
: number of features ( =4 in the above)
() =
() : input (features) of training example
()
: value of feature in training example () =
-6-
1 Introduction
-7-
1 Introduction
0 0
1 1
= 2 +1 , = 2 +1
= 0 0 + 1 1 + 2 2 + 3 3 + 4 4
=
=
-8-
1 Introduction
Outline
Multiple features
Normal equation
-9-
1 Introduction
Parameters
= 0 , 1 , , +1
Cost function
1
() () 2
= (0 , 1 , , ) =
2 =1
Gradient descent
Repeat {
()
- 10 -
} (simultaneously update for every = 0,1,2, , )
1 Introduction
Gradient Descent
One variable ( = 1) Mutiple ( 1)
Repeat { Repeat {
1 1 ()
0 0 () () () ()
=1 =1
1 }
1 1 () () () (Update for every
=1
= 0,1,2, , simultaneously ) ()
0 = 1
}
1 ()
(Update for 0 and 1 simultaneously) 0 0 () () 0
=1
1 ()
1 1 () () 1
=1
1 ()
2 2 () () 2
=1
- 11 -
1 Introduction
Outline
Multiple features
Normal equation
- 12 -
1 Introduction
Feature Scaling
Idea: Make sure features are on a similar scale
- 13 -
1 Introduction
Feature Scaling
Feature Scaling
Get every feature into approximately 1 1 , 2 1 range.
Given 0 = 1,
0 1 3 OK
2 2 0.5 OK
100 2 100 change
0.0001 2 0.0001 change
- 14 -
1 Introduction
Feature Scaling
Mean Normalization
Replace with
to make features have approximately zero mean
(Do not apply to 0 = 1).
For example,
100 # 2
1 = 2000
, 2 = 5
0.5 1 , 2 0.5
1 1 2 2
1 2
1 2
1 : average value of 1 2 : average value of 2
1 2
Either Range of 1 (max-min) Either Range of 2 (max-min)
or Standard deviation of 1 or Standard deviation of 2
- 15 -
1 Introduction
Outline
Multiple features
Normal equation
- 16 -
1 Introduction
Gradient Descent
()
Debugging
How to make sure gradient descent is working correctly
- 17 -
1 Introduction
Gradient Descent
Making sure gradient descent is working correctly
() should decrease
after every iteration
Declare convergence
if () decreases by less than = 10-3 in one iteration.
- 18 -
1 Introduction
Gradient Descent
Making sure gradient descent is working correctly
Use smaller
No. of iterations
Use smaller
- 19 -
1 Introduction
Gradient Descent
Summary
Too small
Slow convergence
Too large
Most of times, () may not decrease on every iteration
() may not converge
(Sometimes, slow convergence is also possible.)
To choose , try
, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1,
X3 X 3 X3 X 3
- 20 -
1 Introduction
Outline
Multiple features
Normal equation
- 21 -
1 Introduction
- 22 -
1 Introduction
Polynomial Regression
Price
(y)
Size (x)
- 23 -
1 Introduction
Polynomial Regression
0 + 1 + 2 2
Price
(y)
Size (x)
- 24 -
1 Introduction
Polynomial Regression
0 + 1 + 2 2
Price
(y)
Size (x)
- 25 -
1 Introduction
Polynomial Regression
0 + 1 + 2 2 + 3 3
0 + 1 + 2 2
Price
(y)
Size (x)
2 3
= 0 + 1 1 + 2 2 + 3 3 = 0 + 1 + 2 + 3
1 = , 2 = ()2 , 3 = ()3
- 26 -
1 Introduction
Polynomial Regression
0 + 1 + 2 2 + 3 3
0 + 1 + 2 2
Price
(y)
Size (x)
2 3
= 0 + 1 1 + 2 2 + 3 3 = 0 + 1 + 2 + 3
1 = , 2 = ()2 , 3 = ()3
- 27 -
1 Introduction
Choice of Features
Price
(y)
Size (x)
- 28 -
1 Introduction
Choice of Features
Price
(y)
Size (x)
= 0 + 1 () + 2 ()2
= 0 + 1 () + 2
- 29 -
1 Introduction
- 31 -
1 Introduction
- 32 -
1 Introduction
Outline
Multiple features
Normal equation
- 33 -
1 Introduction
Normal Equation
A least-square solution to =
2
=
iff
- 34 -
1 Introduction
Gradient Descent
- 35 -
1 Introduction
Normal Equation
Normal equation
Method to solve for analytically
If
= 2 + +
Set = =0
Solve for
If +1
1 2
0 , 1 , , =
2 =1
Set = = 0 (for every )
Solve for 0 , 1 , ,
- 36 -
1 Introduction
Normal Equation
If +1
1 2
0 , 1 , , =
=1
2
1
= 2
2
()
0
0 (1) ()
1
(2)
where = 1 , = (design matrix), () = ()
2
() ()
= 1
- 37 -
1 Introduction
Example
=4 ()
Number of Number of Age of home Price ($1000)
Size (feet2) Bedrooms Floors (years)
1 2104 5 1 45 460
1 1416 3 2 40 232
1 1534 3 2 30 315
1 852 2 1 36 178
1 2104 5 1 45 460
= 1 1416 3 2 40 4(+1) = 232
1 1534 3 2 30 315
1 852 2 1 36 178
= 1
- 38 -
1 Introduction
Example
=5
Size (feet2) Number of Number of Age of home Price ($1000)
Bedrooms Floors (years)
1 2104 5 1 45 460
1 1416 3 2 40 232
1 1534 3 2 30 315
1 852 2 1 36 178
1 3000 4 1 38 540
1 2104 5 1 45 460
1 1416 3 2 40 232
= 1 5(+1) = 315
1534 3 2 30
1 852 2 1 36 178
1 3000 4 1 38 540
- 39 -
= 1
1 Introduction
features
()
0
()
(1)
1
() = () +1 , = (2) (+1)
2
() ()
(1)
(2)
= = 1
()
- 40 -
1 Introduction
One feature
(1)
1 1
1 (2)
If ()
= () 2 , = 1 1 2
1
()
1 1
- 41 -
1 Introduction
Need to compute 1
( 3 )
- 42 -
1 Introduction
Normal Equation
= 1
Singular
Redundant features (linearly dependent)
e.g. 1 = size in feet2
2 = size in m2
1 = 3.28 2 2
Too many features (e.g. )
Delete some features, or use regularization
- 43 -
1 Introduction
References
Andrew Ng, https://www.coursera.org/learn/machine-learning
http://www.holehouse.org/mlclass/04_Logistic_Regression.html
- 44 -