码迷,mamicode.com
首页 > 系统相关 > 详细

《Machine Learning》系列学习笔记之第二周

时间:2017-01-29 18:28:09      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:ldb   ota   srv   dmp   apc   sga   style   igp   pwa   

第二周

第一部分 Multivariate Linear Regression

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.

x(i)jx(i)mn=value of feature j in the ith training example=the column vector of all the feature inputs of the ith training example=the number of training examples=∣∣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...θn]?????x0x1?xn?????=θ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 x(i)0=1 for (i1,,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 training examples are stored in X row-wise. The following example shows us the reason behind setting x(i)0=1 :

X=?????x(1)0x(2)0x(3)0x(1)1x(2)1x(3)1?????,θ=[θ0θ1]

As a result, you can calculate the hypothesis as a column vector of size (m x 1) with:

hθ(X)=




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?α1mi=1m(hθ(x(i))?y(i))?x(i)0

θ1:=θ1?α1mi=1m(hθ(x(i))?y(i))?x(i)1

θ2:=θ2?α1mi=1m(hθ(x(i))?y(i))?x(i)2

}

In other words:

repeat until convergence:{θj:=θj?α1mi=1m(hθ(x(i))?y(i))?x(i)j

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(特征缩放)

1)我们可以通过使我们的每个输入值在大致相同的范围内加速梯度下降。因为θ将在小范围上快速下降,并且在大范围上缓慢下降,因此当变量非常不均匀时,θ将无效地振荡到最佳值。


防止这种情况的方法是修改输入变量的范围,使它们都大致相同。理想情况:


-1≤xi)≤1


要么


-0.5≤xi)≤0.5


这些不是确切的要求;我们只是想加快速度。目标是使所有输入变量大致在这些范围之一,给出或取几个。


2)有助于此的两种技术是特征缩放平均归一化

1.特征缩放:将输入值除以输入变量的范围(即,最大值减去最小值),得到刚刚为1的新范围。

2.平均归一化:涉及从该输入变量的值中减去输入变量的平均值输入变量导致输入变量的新平均值刚好为零。

要实施这两种技术,请按照以下公式调整输入值:


xi= xi-μi/si

其中μi是特征(i)的所有值的平均值si值的范围(max-min,或者si标准差


注意,除以范围或除以标准偏差,得到不同的结果。本课程中的测验使用范围 - 编程练习使用标准偏差。


例如,如果xi代表房价,范围为1002000,平均值为1000,那么xi= price-1000/1900



Gradient Descent in Practice II - Learning Rate

注意:[5:20 - 右图中的x轴标签应该是θ而不是迭代次数]


1)调试梯度下降。 在x轴上绘制迭代次数的绘图。 现在绘制成本函数,Jθ)在梯度下降的迭代次数。 如果Jθ)增加,则可能需要减小α


2)自动收敛测试。 如果Jθ)在一次迭代中减小小于E,则声明收敛,其中E是诸如10-3的一些小值。 然而,在实践中很难选择这个阈值。

技术分享

已经证明,如果学习速率α足够小,则Jθ)将在每次迭代时减小。

技术分享

总结:

如果α太小:收敛慢。

如果α太大:?在每次迭代时不会减少,因此可能不会收敛。


Features and Polynomial Regression

特征选择

有时我们可以转换研究问题的角度,比如将房子的宽度和长度(原始数据)转化为房子的占地面积,从而得到更好的数据模型。


多项式回归

如果不能很好地拟合数据,我们可以通过使它是二次,立方或平方根函数(或任何其他形式)来改变我们的假设函数的行为或曲线。

例如,如果我们的假设函数是x=θ0+θ1x1,则我们可以基于x1创建附加特征,以获得二次函数hθ(x)=θ0+θ1x1+θ2x21或三次函数hθ(x)=θ0+θ1x1+θ2x21+θ3x31

在立方体版本中,我们创建了新的特征x2x3,其中x2 = x21x3 = x31

为了使其为平方根函数,我们可以做:hθ(x)=θ0+θ1x1+θ2x1

要记住的一个重要的事情是,如果你选择你的功能这种方式,然后功能缩放变得非常重要。


例如。如果x1具有范围11000,则x21的范围变为11000000,而x31的范围变为11000000000



第二部分Computing Parameters Analytically

Normal Equation正态方程法

梯度下降给出了使J最小化的一种方式。让我们讨论这样做的第二种方式,这次显式地执行最小化,而不诉诸迭代算法。在“正常方程”方法中,我们将通过显式地取其相对于θj的导数并将它们设置为零来最小化J。这允许我们找到最佳的theta而不迭代。正规方程公式如下:

θ=(XTX)?1XTy

技术分享
不需要使用正规方程进行特征缩放。


以下是梯度下降和正规方程的比较:


Gradient Descent

Normal Equation

Need to choose alpha

No need to choose alpha

Needs many iterations

No need to iterate

O (kn2)

O (n3), need to calculate inverse of XTX

Works well when n is large

Slow if n is very large


使用正规方程,计算XTX具有复杂度On3)。因此,如果我们有非常大量的特征,正态方程将是缓慢的。在实践中,当n超过10,000时,可能适合用梯度下降法。

 

Normal Equation Noninvertibility正态方程不可逆性

当以octave实现正规方程时,我们想使用‘pinv‘函数而不是‘inv‘。 即使XTX不可逆,‘pinv‘函数也会给出一个值θ


如果XTX是不可逆的,常见的原因可能是:

冗余特征,其中两个特征非常密切相关(即,它们是线性相关的)

太多特征(例如m≤n)。 在这种情况下,删除一些功能或使用“正则化”(将在以后的课程中解释)。

上述问题的解决方案包括删除与另一个线性相关的特征或者当存在太多特征时删除一个或多个特征。

《Machine Learning》系列学习笔记之第二周

标签:ldb   ota   srv   dmp   apc   sga   style   igp   pwa   

原文地址:http://www.cnblogs.com/flippedkiki/p/6357580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!