斯坦福大学机器学习笔记 第二周
来源:互联网 发布:相似度对比软件 编辑:程序博客网 时间:2024/06/15 09:41
Multiple Features
Note: [7:25 - θT is 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.
xj(i)=value of feature j in the ith training examplex(i)=the column vector of all the feature inputs of the ith training examplem=the number of training examplesn=x(i);(the number of features)The multivariable form of the hypothesis function accommodating these multiple features is as follows:
hθ(x)=θ0+θ1x1+θ2x2+θ3x3+⋯+θnxn
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:
hθ(x)=θ0θ1...θnx0x1⋮xn=θTxThis 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 (i∈1,…,m). This allows us to do matrix operations with theta and x. Hence making the two vectors 'θ' and x(i) match each other element-wise (that is, have the same number of elements: n+1).]
The following example shows us the reason behind setting x0(i)=1 :
X=x0(1)x0(2)x0(3)x1(1)x1(2)x1(3),θ=θ0θ1
As a result, you can calculate the hypothesis as a vector with:
hθ(X)=θTX
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))⋅x0(i)θ1:=θ1−α1m∑i=1m(hθ(x(i))−y(i))⋅x1(i)θ2:=θ2−α1m∑i=1m(hθ(x(i))−y(i))⋅x2(i)⋯}In other words:
repeat until convergence:{θj:=θj−α1m∑i=1m(hθ(x(i))−y(i))⋅xj(i)for 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) ≤ 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:
xi:=xi−μisi
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,xi:=price−10001900.
Gradient Descent in Practice II - Learning Rate
Note: [5:20 - the x -axis label in the right graph should be θ rather than No. of iterations ]
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 as10−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 α is too small: slow convergence.
If α is too large: may not decrease onevery 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 combinex1 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θ(x)=θ0+θ1x1 then we can create additional features based on x1, to get the quadratic function hθ(x)=θ0+θ1x1+θ2x12 or the cubic function hθ(x)=θ0+θ1x1+θ2x12+θ3x13
In the cubic version, we have created new features x2 and x3 where x2=x12 and x3=x13.
To make it a square root function, we could do: 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. if x1 has range 1 - 1000 then range of x12 becomes 1 - 1000000 and that of x13 becomes 1 - 1000000000
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 featuresx0 and x1. 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 wayof doing so, this time performing the minimization explicitly and withoutresorting to an iterative algorithm. In the "Normal Equation" method, we will minimize J byexplicitly taking its derivatives with respect to the θj’s, and setting them tozero. This allows us to find the optimum theta without iteration. The normal equation formula is given below:
θ=(XTX)−1XTy
There is no need to do feature scaling with the normal equation.
The following is a comparison of gradient descent and the normal equation:
With the normal equation, computing the inversion has complexity 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θ even if XTX is not invertible.
If XTX 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.
- 斯坦福大学机器学习笔记 第二周
- 斯坦福大学《机器学习》笔记
- 斯坦福大学机器学习笔记(1)
- 斯坦福大学机器学习笔记(2)
- 斯坦福大学-机器学习20讲-第二讲
- 斯坦福大学机器学习笔记--第二周(1.多元线性回归及多元线性回归的梯度下降)
- 斯坦福大学机器学习笔记(英文版)
- 斯坦福大学机器学习笔记(4)-logistic回归
- 斯坦福大学机器学习笔记 第一周
- 斯坦福大学机器学习课程笔记一概述
- 斯坦福大学机器学习第二课 “单变量线性回归”
- Machine Learning(Stanford)| 斯坦福大学机器学习笔记--第二周(1.多元线性回归及多元线性回归的梯度下降)
- Coursera公开课笔记: 斯坦福大学机器学习第二课“单变量线性回归(Linear regression with one variable)”
- Coursera公开课笔记: 斯坦福大学机器学习第二课“单变量线性回归(Linear regression with one variable)”
- 机器学习-斯坦福大学翻译
- 斯坦福大学机器学习-note2
- 斯坦福大学机器学习-note9
- 【斯坦福大学-吴恩达-机器学习】
- java中的枚举类型
- 多态性
- onCreateContextMenu在滚动列表中的实现
- 欢迎使用CSDN-markdown编辑器
- 理解计算机(1)—熟悉编码
- 斯坦福大学机器学习笔记 第二周
- nginx负载均衡的一些问题和具体配置
- VS2010-MFC获取某个树控件某个树节点下所有子节点的文本
- 常用内存数据库介绍
- leetcode 539. Minimum Time Difference
- Python库Numpy的argpartition函数浅析
- 两台虚拟机ping不通原因
- webpack-2-基本配置
- TensorFlow教程之深入MNIST测试