MachineLearning笔记week2 Linear Regression

2.1 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.

x(i)j x j ( i ) = value of feature j in the ith i t h training example

x(i) x ( i ) = the input(features) of the ith i t h training example

m = the number of training examples

n = the number of features

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

hθ(x)=θ0+θ1x1++θ2x2++θnxn h θ ( x ) = θ 0 + θ 1 x 1 + + θ 2 x 2 + ⋯ + θ n x n

In order to develop intuition about this function, we can think about θθ θ θ as the basic price of a house, θ1 θ 1 as the price per square meter, θ2 θ 2 as the price per floor, etc. x1 x 1 will be the number of square meters in the house, x2 x 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)θ0θ1θn=θTx h θ ( x ) = ( θ 0 θ 1 ⋯ θ n ) ( θ 0 θ 1 ⋮ θ n ) = θ T x

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 x(i)0=1 for (i1,...,m) x 0 ( i ) = 1  for  ( i ∈ 1 , . . . , m ) . This allows us to do matrix operations with theta and x. Hence making the two vectors θ θ and x(i) x ( i ) match each other element-wise (that is, have the same number of elements: n+1).

2.2 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:

θj:=θjα1mmi=1(hθ(x(i))y(i))x(i)j forj:=0n θ j := θ j − α 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i )  for j := 0 ⋯ n

2.3 Gradient Descent in Practice I - 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μisi x i := x i − μ i s i

Where μi μ i is the average of all the values for feature (i) ( i ) and si s i is the range of values (max - min), or si s i is the standard deviation.

2.4 Gradient Descent in Practice II - Learning Rate

Debugging gradient descent. Make a plot with number of iterations on the x-axis. Now plot the cost function, J(θ) J ( θ ) over the number of iterations of gradient descent. If J(θ) J ( θ ) ever increases, then you probably need to decrease α α .

Automatic convergence test. Declare convergence if J(θ) J ( θ ) decreases by less than E in one iteration, where E is some small value such as 103 10 − 3 . However in practice it’s difficult to choose this threshold value.

To summarize:

If α α is too small: slow convergence.

If α α is too large: may not decrease on every iteration and thus may not converge.

2.5 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 x1 x 1 and x2 x 2 into a new feature x3 x 3 by taking x1x2 x 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 h θ ( x ) = θ 0 + θ 1 x 1 h then we can create additional features based on x1 x 1 , to get the quadratic function hθ(x)=θ0+θ1x1+θ2x21 h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 x 1 2 or the cubic function hθ(x)=θ0+θ1x1+θ2x21+θ3x31 h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 x 1 2 + θ 3 x 1 3

In the cubic version, we have created new features x2 x 2 and x3 x 3 where x2=x21 x 2 = x 1 2 and x3=x31 x 3 = x 1 3 .

To make it a square root function, we could do: hθ(x)=θ0+θ1x1+θ2x1 h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 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 x1 x 1 has range 1 - 1000 then range of x21 x 1 2 becomes 1 - 1000000 and that of x31 x 1 3 becomes 1 - 1000000000

2.6 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 θ 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 θ = ( X T X ) − 1 X T y

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 DescentNormal Equation
Need to choose alphaNo need to choose alpha
Needs many iterationsNo need to iterate
O(kn2) O ( k n 2 ) O(n3) O ( n 3 ) ,need to calculate inverse of XTX X T X
Works well when n is largeSlow if n is very large

With the normal equation, computing the inversion has complexity O(n3) 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.

2.7 Normal Equation

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 θ θ even if XTX X T X is not invertible.

If XTX X T X 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值