zoukankan      html  css  js  c++  java
  • 4. 机器学习第二周(1)

    Multivariate Linear regression

    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.
    $$
    egin{align}x_j^{(i)} &= ext{value of feature } j ext{ in the }i^{th} ext{ training example} ewline x^{(i)}& = ext{the column vector of all the feature inputs of the }i^{th} ext{ training example} ewline m &= ext{the number of training examples} ewline n &= left| x^{(i)} ight| ; ext{(the number of features)} end{align}
    $$
    The multivariable form of the hypothesis function accommodating these multiple features is as follows:

    $$h_ heta (x) = heta_0 + heta_1 x_1 + heta_2 x_2 + heta_3 x_3 + cdots + heta_n x_n$$

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

    Using the definition of matrix multiplication, our multivariable hypothesis function can be concisely represented as:
    $$
    egin{align}h_ heta(x) =egin{bmatrix} heta_0 hspace{2em} heta_1 hspace{2em} ... hspace{2em} heta_nend{bmatrix}egin{bmatrix}x_0 ewline x_1 ewline vdots ewline x_nend{bmatrix}= heta^T xend{align}
    $$
    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_{0}^{(i)} =1 ext{ for } (iin { 1,dots, m } )$$. This allows us to do matrix operations with theta and x. Hence making the two vectors 'theta' and $$x_{(i)}$$ match each other element-wise (that is, have the same number of elements: n+1).]

    The training examples are stored in X row-wise. The following example shows us the reason behind setting $$x_{0}^{(i)} =1$$ :
    $$
    egin{align}X = egin{bmatrix}x^{(1)}_0 & x^{(1)}_1 ewline x^{(2)}_0 & x^{(2)}_1 ewline x^{(3)}_0 & x^{(3)}_1 end{bmatrix}&, heta = egin{bmatrix} heta_0 ewline heta_1 ewlineend{bmatrix}end{align}
    $$
    As a result, you can calculate the hypothesis as a column vector of size (m x 1) with:

    $$
    h_ heta(X) = X heta
    $$

    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:
    $$
    egin{align} & ext{repeat until convergence:} ; lbrace ewline ; & heta_0 := heta_0 - alpha frac{1}{m} sumlimits_{i=1}^{m} (h_ heta(x^{(i)}) - y^{(i)}) cdot x_0^{(i)} ewline ; & heta_1 := heta_1 - alpha frac{1}{m} sumlimits_{i=1}^{m} (h_ heta(x^{(i)}) - y^{(i)}) cdot x_1^{(i)} ewline ; & heta_2 := heta_2 - alpha frac{1}{m} sumlimits_{i=1}^{m} (h_ heta(x^{(i)}) - y^{(i)}) cdot x_2^{(i)} ewline & cdots ewline brace end{align}
    $$
    In other words:
    $$
    egin{align}& ext{repeat until convergence:} ; lbrace ewline ; & heta_j := heta_j - alpha frac{1}{m} sumlimits_{i=1}^{m} (h_ heta(x^{(i)}) - y^{(i)}) cdot x_j^{(i)} ; & ext{for j := 0...n} ewline braceend{align}
    $$
    The following image compares gradient descent with one variable to gradient descent with multiple variables:

    img

    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:

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

    or

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

    $$
    x_i := dfrac{x_i - mu_i}{s_i}
    $$
    Where μi is the average of all the values for feature (i) and si is the range of values (max - min), or si 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 xi represents housing prices with a range of 100 to 2000 and a mean value of 1000, then,
    $$
    x_i := dfrac{price-1000}{1900}
    $$

    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(θ) 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 10−3. However in practice it's difficult to choose this threshold value.

    img

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

    img

    To summarize:

    If α is too small: slow convergence.

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

    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 and x2 into a new feature x3 by taking x1⋅x2.

    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_ heta(x) = heta_0 + heta_1 x_1$$ then we can create additional features based on x1, to get the quadratic function$$h_ heta(x) = heta_0 + heta_1 x_1 + heta_2 x_1^2$$ or the cubic function $$h_ heta(x) = heta_0 + heta_1 x_1 + heta_2 x_1^2 + heta_3 x_1^3$$

    In the cubic version, we have created new features x2 and x3 where$$x_2 = x_1^2$$ and $$x_3 = x_1^3$$.

    To make it a square root function, we could do: $$h_ heta(x) = heta_0 + heta_1 x_1 + heta_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 x1 has range 1 - 1000 then range of $$x_1^2$$ becomes 1 - 1000000 and that of $$x_1^3$$ becomes 1 - 1000000000

  • 相关阅读:
    杭电2007
    杭电 2004
    杭电2005
    杭电2001
    杭电 2000
    Section One
    杭电oj 1002
    杭电oj 1001
    JavaScript高级程序设计第14章表单脚本 (学习笔记)
    JavaScript高级程序设计(学习笔记)
  • 原文地址:https://www.cnblogs.com/flysevenwu/p/6143047.html
Copyright © 2011-2022 走看看