第二周

第一部分 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 (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 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≤x(i)≤1

要么

-0.5≤x(i)≤0.5

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

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

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

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

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

xi:=
xi-μi/si

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

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

例如,如果xi代表房价,范围为100到2000,平均值为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

特征选择

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

多项式回归

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

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

在立方体版本中,我们创建了新的特征x2和x3,其中x2
= x21和x3
= x31。

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

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

例如。如果x1具有范围1至1000,则x21的范围变为1至1000000,而x31的范围变为1至1000000000

第二部分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具有复杂度O(n3)。因此,如果我们有非常大量的特征,正态方程将是缓慢的。在实践中,当n超过10,000时,可能适合用梯度下降法。

Normal
Equation Noninvertibility正态方程不可逆性

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

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

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

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

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

最新文章

  1. 数据结构《17》---- 自动补齐之《二》----Ternary Search Tree
  2. Lua Coroutine详解
  3. Sources
  4. Access denied for user 'root'@'localhost' (using password:YES) 解决方案
  5. C语言中的字符串截取函数
  6. Java学习笔记51:数组转ArrayList和ArrayList转数组技巧
  7. 【MOS】在不同版本和平台之间进行还原或复制 (文档 ID 1526162.1)--跨版本恢复
  8. Salesforce 数据备份和恢复小知识
  9. mybatis 中 foreach collection的三种用法(转)
  10. linux中apt-get和yum和wget的区别
  11. fio 测试 磁盘I/O: ls -1 /sys/block/sda/queue/ | awk '{cmd="cat "i$0; print i$0; system(cmd) }' i=`pwd`'/'
  12. c#运用反射获取属性和设置属性值
  13. 使用devenv.exe自动编译项目
  14. 将Excel的数据更新至SqlServer
  15. 迷你MVVM框架 avalonjs 0.91发布
  16. android 播放视频时切换全屏隐藏状态栏
  17. Java日期格式化参数对照表
  18. Python字符串/元祖/列表/字典互转
  19. FTP文件上传以及获取ftp配置帮助类
  20. Linq 与 Lambda 简单使用

热门文章

  1. API网关Ocelot 使用Polly 处理部分失败问题
  2. 【easyui】之treegrid的分页
  3. 由merge into引起的序列跳号
  4. redux 初识
  5. Gulp实现css、js、图片的压缩以及css、js文件的MD5命名
  6. Unity3d中如何混用三种脚本语言?
  7. 2017-2-20 C#基础 运算符
  8. 使用Nuget管理dll
  9. 最近发现的.net core中的一些bugs
  10. 每天一个linux命令(39)--ifconfig命令