0%

week2

Multivariate Linear Regression(多元线性回归)

Multiple Features(多特性)

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.

  • xj(i)x_j^{(i)}= value of feature j in the ithi^{th} training example
  • x(i)x^{(i)}=the input (features) of the i(th)i^{(th)} training example
  • mm =the number of training examples
  • nn =the number of features

The multivariable form of the hypothesis function accommodating these multiple features is as follows:

hθ(x)=θ0+θ1x1+θ2x2+θ3x3++θnxnh_\theta (x) = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \theta_3 x_3 + \cdots + \theta_n x_n

In order to develop intuition about this function, we can think about θ0\theta_0 as the basic price of a house, θ1\theta_1 as the price per square meter, θ2\theta_2 as the price per floor, etc. x1x_1 will be the number of square meters in the house, x2x_2 the number of floors, etc.

Using the definition of matrix multiplication, our multivariable hypothesis function can be concisely represented as:

hθ(x)=[θ0θ1...θn][x0x1...xn]=θTxh_θ(x)=\left[ \begin{matrix} \theta_0 & \theta_1 & ... & \theta_n \end{matrix} \right]\left[ \begin{matrix} x_0 \\ x_1 \\ ... \\ x_n \end{matrix} \right] = \theta^Tx

This is a vectorization of our hypothesis function for one training example; see the lessons on vectorization to learn more.

Remark Note that for convenience reasons in this course we assume x0(i)=1 for (i1,,m)x_{0}^{(i)} =1 \text{ for } (i\in { 1,\dots, m } ). This allows us to do matrix operations with theta and x. Hence making the two vectors ‘θ\theta’ and x(i)x^{(i)} match each other element-wise (that is, have the same number of elements:n+1n+1).]

Gradient Descent for Multiple Variables

NOTE$h_\theta(x^{i())}) - y^{(i)} $是⼀个确定的值,因为在收敛开始之前就需要对参数 以及 初始化⼀个值,这 时该表达式的值就确定是⼀个实际值,在 Vectorization ⼩节会涉及到;

The gradient descent equation itself is generally the same form; we just have to repeat it for our ‘n’ features:

\begin{align*} & \text{repeat until convergence:} \; \lbrace \newline \; & \theta_0 := \theta_0 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_0^{(i)}\newline \; & \theta_1 := \theta_1 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_1^{(i)} \newline \; & \theta_2 := \theta_2 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_2^{(i)} \newline & \cdots \newline \rbrace \end{align*}

in other words:

\begin{align*}& \text{repeat until convergence:} \; \lbrace \newline \; & \theta_j := \theta_j - \alpha \frac{1}{m} \sum\limits_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) \cdot x_j^{(i)} \; & \text{for j := 0...n}\newline \rbrace\end{align*}

The following image compares gradient descent with one variable to gradient descent with multiple variables:

20210709170310

Gradient Descent in Practice 1 - Feature Scaling

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:

1x(i)1−1 ≤ x_{(i)} ≤ 1

or

0.5x(i)0.5−0.5 ≤ 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 are feature scaling and mean 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:

xi:=xiμisix_i := \dfrac{x_i - \mu_i}{s_i}

Where μiμ_i* is the average of all the values for feature (i) and sis_i* is the range of values (max - min), or sis_i is 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, if xix_i represents housing prices with a range of 100 to 2000 and a mean value of 1000, then, xi:=price10001900x_i := \dfrac{price-1000}{1900}

Gradient Descent in Practice 2 - Learning Rate

Debugging gradient descent. Make a plot with number of iterations on 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 as 10310^{−3}. However in practice it’s difficult to choose this threshold value.

20210709171144

It has been proven that if learning rate α is sufficiently small, then J(θ) will decrease on every iteration.

20210709171202

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 can combine multiple features into one. For example, we can combine x1x_1 and x2x_2 into a new feature x3x_3 by taking x1x2x_1⋅x_2.

Polynomial Regression

Our hypothesis function need not be linear (a straight line) if that does not fit the data well.

We can change the behavior or curve of our hypothesis function by making it a quadratic, cubic or square root function (or any other form).

For example, if our hypothesis function is hθ(x)=θ0+θ1x1h_\theta(x) = \theta_0 + \theta_1 x_1 then we can create additional features based on x_1x1, to get the quadratic function hθ(x)=θ0+θ1x1+θ2x12h_\theta(x) = \theta_0 + \theta_1 x_1 + \theta_2 x_1^2 or the cubic function hθ(x)=θ0+θ1x1+θ2x12+θ3x13h_\theta(x) = \theta_0 + \theta_1 x_1 + \theta_2 x_1^2 + \theta_3 x_1^3

In the cubic version, we have created new features x2x_2 and x3x_3 where x2=x12x_2 = x_1^2 and x3=x13x_3 = x_1^3.

To make it a square root function, we could do: hθ(x)=θ0+θ1x1+θ2x1h_\theta(x) = \theta_0 + \theta_1 x_1 + \theta_2 \sqrt{x_1}

One important thing to keep in mind is, if you choose your features this way then feature scaling becomes very important.

eg. if x1x_1 has range 1 - 1000 then range of x12x_1^2 becomes 1 - 1000000 and that of x13x_1^3 becomes 1 - 1000000000

Computering Parameters Analytically

Normal Equation

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:

θ=(XTX)1XTy\theta = (X^T X)^{-1}X^Ty

20210709171831

There is no need to do feature scaling with the normal equation.

The following is a comparison of gradient descent and the normal equation:

Gradient Descent Normal Equation
Need to choose alpha No need to choose alpha
Needs many iterations No need to iterate
O(kn2)\mathcal{O}(kn^2) O(n3)\mathcal{O}(n^3), need to calculate inverse of XTXX^TX
Works well when n is large Slow if n is very large

With the normal equation, computing the inversion has complexity O(n3)\mathcal{O}(n^3). 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 Nonivertibility

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 if XTXX^TX is not invertible.

If XTXX^TX is noninvertible, 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.