Linear Regression with
multiple variables
Multiple features
Machine Learning
Andrew Ng
Size (feet2) Price ($1000)
2104 460
1416 232
1534 315
852 178
… …
Multiple features (variables)
Andrew Ng
Size (feet2) Number of
bedrooms
Number of
floors
Age of home
(years)
Price ($1000)
2104 5 1 45 460
1416 3 2 40 232
1534 3 2 30 315
852 2 1 36 178
… … … … …
Multiple features (variables).
Notation:
= number of features
= input (features) of training example.
= value of feature in training example.
Andrew Ng
Hypothesis:
Previously:
Andrew Ng
For convenience of notation, define .
Multivariate linear regression.
Linear Regression with
multiple variables
Gradient descent for
multiple variables
Machine Learning
Andrew Ng
Hypothesis:
Cost function:
Parameters:
(simultaneously update for every )
Repeat
Gradient descent:
Andrew Ng
(simultaneously update )
Gradient Descent
Repeat
Previously (n=1):
New algorithm :
Repeat
(simultaneously update for
)
Linear Regression with
multiple variables
Gradient descent in
practice I: Feature Scaling
Machine Learning
Andrew Ng
E.g. = size (0-2000 feet2)
= number of bedrooms (1-5)
Feature Scaling
Idea: Make sure features are on a similar scale.
size (feet2)
number of bedrooms
Andrew Ng
Feature Scaling
Get every feature into approximately a range.
Andrew Ng
Replace with to make features have approximately zero mean
(Do not apply to ).
Mean normalization
E.g.
Linear Regression with
multiple variables
Gradient descent in
practice II: Learning rate
Machine Learning
Andrew Ng
Gradient descent
- “Debugging”: How to make sure gradient
descent is working correctly.
- How to choose learning rate .
Andrew Ng
Example automatic
convergence test:
Declare convergence if
decreases by less than
in one iteration.
0 100 200 300 400
No. of iterations
Making sure gradient descent is working correctly.
Or,
Andrew Ng
Making sure gradient descent is working correctly.
Gradient descent not working.
Use smaller .
No. of iterations
No. of iterations θ
- For sufficiently small , should decrease on every iteration.
- But if is too small, gradient descent can be slow to converge.
Andrew Ng
Summary:
- If is too small: slow convergence.
- If is too large: may not decrease on
every iteration; may not converge.
To choose , try
. . ., 0.001, , 0.01, , 0.1, , 1, . . .
Linear Regression with
multiple variables
Features and
polynomial regression
Machine Learning
Andrew Ng
Housing prices prediction
Andrew Ng
Polynomial regression
Price
(y)
Size (x)
Andrew Ng
Choice of features
Price
(y)
Size (x)
Linear Regression with
multiple variables
Normal equation
Machine Learning
Andrew Ng
Gradient Descent
Normal equation: Method to solve for
analytically.
Andrew Ng
Intuition: If 1D
Solve for
(for every )
Andrew Ng
Size (feet2) Number of
bedrooms
Number of
floors
Age of home
(years)
Price ($1000)
1 2104 5 1 45 460
1 1416 3 2 40 232
1 1534 3 2 30 315
1 852 2 1 36 178
Size (feet2) Number of
bedrooms
Number of
floors
Age of home
(years)
Price ($1000)
2104 5 1 45 460
1416 3 2 40 232
1534 3 2 30 315
852 2 1 36 178
Examples:
Andrew Ng
examples ; features.
E.g. If
Andrew Ng
is inverse of matrix .
Octave: pinv(X’*X)*X’*y
Andrew Ng
training examples, features.
Gradient Descent Normal Equation
• No need to choose .
• Don’t need to iterate.
• Need to choose .
• Needs many iterations.
• Works well even
when is large.
• Need to compute
• Slow if is very large.
Linear Regression with
multiple variables
Normal equation
and non-invertibility
(optional)
Machine Learning
Andrew Ng
Normal equation
- What if is non-invertible? (singular/
degenerate)
- Octave: pinv(X’*X)*X’*y
Andrew Ng
What if is non-invertible?
• Redundant features (linearly dependent).
E.g. size in feet2
size in m2
• Too many features (e.g. ).
- Delete some features, or use regularization.

Machine Learning lecture3(linear regression)

Editor's Notes

  • #5 Pop-up Quiz
  • #12 Features should take a similar range of values. This leads to a faster convergence of the gradient descent.
  • #13 The feature range of values shouldn’t be very large and also not very tiny. Or, it shouldn’t be far from the -1 to +1 range.
  • #17 Debugging by plotting cost function J versus no. of iterations of the algorithm. The algorithm works well as J decreases in the plot. The plot also shows where the algorithm converges as the curve becomes flattened. Choosing the threshold in the automatic convergence test is difficult.
  • #19 We try to pick the largest possible reasonable value for the learning rate to ensure a fast convergence.
  • #21 We can define new features to get a better model.
  • #22 We can fit the polynomial function to the linear regression model by redefining or choosing features. In this case, feature scaling becomes extremely important.
  • #23 This slide shows another different choice for your features.
  • #27 Pop-up Quiz
  • #28 Pop-up Quiz
  • #30 Feature scaling is not necessary here.
  • #31 As the no. of features is less than 1000, the normal equation method is useful. When the model becomes more complicated n> 1000, better use gradient descent.
  • #34 Regularization enables to fit a large no. of parameters using a small no. of training examples.