Note:[7:25 -\theta^TθTis a 1 by (n+1) matrix and not an (n+1) by 1 matrix]
Linear regression with multiple variables is also known as "multivariate linear regression".
We now introduce notation for equations where we can have any number of input variables.
x(i)jx(i)mn=value of feature j in the ith training example=the input (features) of the ith training example=the number of training examples=the number of features
The multivariable form of the hypothesis function accommodating these multiple features is as follows:
In order to develop intuition about this function, we can think about\theta_0θ0as the basic price of a house,\theta_1θ1as the price per square meter,\theta_2θ2as the price per floor, etc.x_1x1will be the number of square meters in the house,x_2x2the number of floors, etc.
Using the definition of matrix multiplication, our multivariable hypothesis function can be concisely represented as:
Remark: Note that for convenience reasons in this course we assumex_{0}^{(i)} =1 \text{ for } (i\in { 1,\dots, m } )x0(i)=1 for (i∈1,…,m).
This allows us to do matrix operations with theta and x. Hence making the two vectors '\thetaθ' andx^{(i)}x(i)match each other element-wise (that is, have the same number of elements: n+1).]This is a vectorization of our hypothesis function for one training example; see the lessons on vectorization to learn more.
Gradient Descent For Multiple Variables
Gradient Descent for Multiple Variables
The gradient descent equation itself is generally the same form; we just have to repeat it for our 'n' features:
}repeat until convergence:{θ0:=θ0−α1m∑i=1m(hθ(x(i))−y(i))⋅x(i)0θ1:=θ1−α1m∑i=1m(hθ(x(i))−y(i))⋅x(i)1θ2:=θ2−α1m∑i=1m(hθ(x(i))−y(i))⋅x(i)2⋯
In other words:
}repeat until convergence:{θj:=θj−α1m∑i=1m(hθ(x(i))−y(i))⋅x(i)jfor j := 0...n
The following image compares gradient descent with one variable to gradient descent with multiple variables:
Gradient Descent in Practice I - Feature Scaling
Note:[6:20 - The average size of a house is 1000 but 100 is accidentally written instead]
We can speed up gradient descent by having each of our input values in roughly the same range. This is because θ will descend quickly on small ranges and slowly on large ranges, and so will oscillate inefficiently down to the optimum when the variables are very uneven.
The way to prevent this is to modify the ranges of our input variables so that they are all roughly the same. Ideally:
−1 ≤x_{(i)}x(i)≤ 1
or
−0.5 ≤x_{(i)}x(i)≤ 0.5
These aren't exact requirements; we are only trying to speed things up. The goal is to get all input variables into roughly one of these ranges, give or take a few.
Two techniques to help with this arefeature scalingandmean normalization. Feature scaling involves dividing the input values by the range (i.e. the maximum value minus the minimum value) of the input variable, resulting in a new range of just 1. Mean normalization involves subtracting the average value for an input variable from the values for that input variable resulting in a new average value for the input variable of just zero. To implement both of these techniques, adjust your input values as shown in this formula:
x_i := \dfrac{x_i - \mu_i}{s_i}xi:=sixi−μi
Whereμ_iμiis theaverageof all the values for feature (i) ands_isiis the range of values (max - min), ors_isiis the standard deviation.
Note that dividing by the range, or dividing by the standard deviation, give different results. The quizzes in this course use range - the programming exercises use standard deviation.
For example, ifx_ixirepresents housing prices with a range of 100 to 2000 and a mean value of 1000, then,x_i := \dfrac{price-1000}{1900}xi:=1900price−1000.
----
Concretely, here's the gradient descent update rule.
And what I want to do in this video is tell you
about what I think of as debugging, and some tips for
making sure that gradient descent is working correctly.
And second, I wanna tell you how to choose the learning rate alpha or
at least how I go about choosing it.
Gradient Descent in Practice II - Learning Rate
Note:[5:20 - the x -axis label in the right graph should be\thetaθrather than No. of iterations ]
Debugging gradient descent.Make a plot withnumber of iterationson the x-axis. Now plot the cost function, J(θ) over the number of iterations of gradient descent. If J(θ) ever increases, then you probably need to decrease α.
Automatic convergence test.Declare convergence if J(θ) decreases by less than E in one iteration, where E is some small value such as10^{−3}10−3. However in practice it's difficult to choose this threshold value.
It has been proven that if learning rate α is sufficiently small, then J(θ) will decrease on every iteration.
To summarize:
If\alphaαis too small: slow convergence.
If\alphaαis too large: may not decrease on every iteration and thus may not converge.
ㅍ
Features and Polynomial Regression
We can improve our features and the form of our hypothesis function in a couple different ways.
We cancombinemultiple features into one. For example, we can combinex_1x1andx_2x2into a new featurex_3x3by takingx_1x1⋅x_2x2.
Polynomial Regression
Our hypothesis function need not be linear (a straight line) if that does not fit the data well.
We canchange the behavior or curveof our hypothesis function by making it a quadratic, cubic or square root function (or any other form).
For example, if our hypothesis function ish_\theta(x) = \theta_0 + \theta_1 x_1hθ(x)=θ0+θ1x1then we can create additional features based onx_1x1, to get the quadratic functionh_\theta(x) = \theta_0 + \theta_1 x_1 + \theta_2 x_1^2hθ(x)=θ0+θ1x1+θ2x12or the cubic functionh_\theta(x) = \theta_0 + \theta_1 x_1 + \theta_2 x_1^2 + \theta_3 x_1^3hθ(x)=θ0+θ1x1+θ2x12+θ3x13
In the cubic version, we have created new featuresx_2x2andx_3x3wherex_2 = x_1^2x2=x12andx_3 = x_1^3x3=x13.
To make it a square root function, we could do:h_\theta(x) = \theta_0 + \theta_1 x_1 + \theta_2 \sqrt{x_1}hθ(x)=θ0+θ1x1+θ2x1
One important thing to keep in mind is, if you choose your features this way then feature scaling becomes very important.
eg. ifx_1x1has range 1 - 1000 then range ofx_1^2x12becomes 1 - 1000000 and that ofx_1^3x13becomes 1 - 1000000000
In the problem that we are interested in, Theta is
no longer just a real number,
but, instead, is this
n+1-dimensional parameter vector, and,
a cost function J is
a function of this vector
value or Theta 0 through
Theta m.
Normal Equation
Note:[8:00 to 8:44 - The design matrix X (in the bottom right side of the slide) given in the example should have elements x with subscript 1 and superscripts varying from 1 to m because for all m training sets there are only 2 featuresx_0x0andx_1x1. 12:56 - The X matrix is m by (n+1) and NOT n by n. ]
Gradient descent gives one way of minimizing J. Let’s discuss a second way of doing so, this time performing the minimization explicitly and without resorting to an iterative algorithm. In the "Normal Equation" method, we will minimize J by explicitly taking its derivatives with respect to the θj ’s, and setting them to zero. This allows us to find the optimum theta without iteration. The normal equation formula is given below:
\theta = (X^T X)^{-1}X^T yθ=(XTX)−1XTy
There isno needto do feature scaling with the normal equation.
The following is a comparison of gradient descent and the normal equation:
Gradient DescentNormal Equation
Need to choose alpha
No need to choose alpha
Needs many iterations
No need to iterate
O (kn^2kn2)
O (n^3n3), need to calculate inverse ofX^TXXTX
Works well when n is large
Slow if n is very large
With the normal equation, computing the inversion has complexity\mathcal{O}(n^3)O(n3). So if we have a very large number of features, the normal equation will be slow. In practice, when n exceeds 10,000 it might be a good time to go from a normal solution to an iterative process.
Normal Equation Noninvertibility
When implementing the normal equation in octave we want to use the 'pinv' function rather than 'inv.' The 'pinv' function will give you a value of\thetaθeven ifX^TXXTXis not invertible.
IfX^TXXTXisnoninvertible,the common causes might be having :
Redundant features, where two features are very closely related (i.e. they are linearly dependent)
Too many features (e.g. m ≤ n). In this case, delete some features or use "regularization" (to be explained in a later lesson).
Solutions to the above problems include deleting a feature that is linearly dependent with another or deleting one or more features when there are too many features.