Andrew Ng ML(2)——linear regressi
2018-12-05 本文已影响0人
tmax
linear regressing with multiple variables(supervised learning)
-
m:
numbers of training examples
n:
numbers of features
x^(i):
input (features) ofexample
: values of feature j in
traing example
e.g.example
,
=2
-
Hypothesis:
(for convenience,define
,means
)
,
,
梯度下降(多变量)
-
Hypothesis:
Parameters:
(n+1维向量
)
Cost function:
![](https://img.haomeiwen.com/i7712148/05f32a3b250b54b0.png)
-
Gradient descent in practice I
-
Feature Scaling(特征收缩)
-
在两个或者多个特征值范围
差距太大时,cost function的等高线图会呈现出细长的椭圆形
,会导致梯度下降缓慢
(可以做一定的处理,使多个特征值范围限制在同一个范围内)
![](https://img.haomeiwen.com/i7712148/00dc5c0c4b98ede2.png)
对于特征值范围的选择,
不一定
要限制在-1~1之间,但是范围不能太大
或者太小
![](https://img.haomeiwen.com/i7712148/36ab319cf5651715.png)
-
Mean normalization(归一化处理)
,
代表第
个特征值的
平均值
,代表第
个特征变量的
标准差
或
-
Gradient descent in practice II(about
)
确定梯度下降正常工作的方法:1:画出cost function的值与对应迭代次数的函数图像,观察是否收敛(通常使用的方法) 2.确定一个的值,自动收敛测试
![](https://img.haomeiwen.com/i7712148/d2954277f1aa27ea.png)
![](https://img.haomeiwen.com/i7712148/0b94f0b72bd3b130.png)
summary
if too small
: slow convergence
if too large
: cost function may not decrease on every iteration,may not converge(slow converge also possible)
To choose ,try
-
特征选择
如:在使用房屋的临街长度
和深度
预测房价时,可以定义一个新的特征——面积
-
polynomial regression (多项式回归)
根据所给出的数据集的特征,用不同的多项式模型
拟合数据
e.g.:
对于上图
1、用三次模型
拟合
,
,
P.S. 注意特征值缩放!
2、用平方根模型
拟合
,
-
正规方程——
最优解的另一种解法(即使特征范围差距
很大
也不需要
特征缩放)
![](https://img.haomeiwen.com/i7712148/4d3b78ec5c00bb0c.png)
![](https://img.haomeiwen.com/i7712148/14005175bdf0a4c6.png)
Q:
是如何求出来的???
(其中
)
由于X并不是方阵
,也就没有逆矩阵,所以首先需要两边同乘
即:(其中
为方阵)
易得,
Q:
不可逆怎么办???
1.检查特征之间是否线性相关
2.检查是否特征太多(样本太少)
或者使用正规化
![](https://img.haomeiwen.com/i7712148/90003beded76b90e.png)
总结
![](https://img.haomeiwen.com/i7712148/f4424a5c35fccb6f.png)
梯度下降的向量计算方式
![](https://img.haomeiwen.com/i7712148/573077f6a5cf177b.png)