week 2
1 multivariate linear regression(多元线性回归)
1.1 notation
- m : the number of training example;
n: the number of features;
:input (features) of training example;
:value of feature j in training example;
:cost function ;
:n+1维向量(加一是由于下角标从0开始);
1.2 gradient descent in practice
1.2.1 feature scaling(特征缩放)
- 将各个特征值的范围缩放到接近于 的区间上;
在同一数量级上为宜;
有利于提高 gradient descent 的收敛速度;
1.2.2 mean normalization(均值归一化);
- 用 取代 ,使各个特征值的均值为0;
是该特征值的均值;
除以该特征的范围(最大值 - 最小值)即可实现均值归一化;
该特征的范围也可使用标准差替代,这两种方式所得结果不相同;
不可应用于 ,因为 ;
1.2.3 learning rate
-
:learning rate;
若 过大,则 cost function 会越过最小值点不断增大;
若 过小,一定可收敛,但需耗费较长时间; - To choose , try
… , 0.001 , 0.003 , 0.01 , 0.03 , 0.1 , 0.3 , 1 , … (三倍速增加)
- 上图中的两种情况均由于 过大引起;
1.3 选择特征
- 可将 排列组合相城,构成新的 features , e.g. ;
- 选择新的 features ,注意使用 feature scaling ,使得各个 feature 范围接近于 的区间上;
2 normal equation(正规方程)
2.1 concept
- Normal equation:一种求解θ的解析解法,不再需要多次迭代求解θ,而是直接求解θ的最优值;
该方法不需要做 feature scaling , 不需要选择 learning rate ;
求 对 的偏导,解得令偏导为0时的 值,即为 最小时的 值; - 结论: ,即可解得使 最小的 值;
对于 linear regression 问题,normal equation 是一个很好的替代方法;
comparison
gradient descent | normal equation |
---|---|
need to choose | no need to choose |
need many iterations | do not need to iterate |
works well even when is large | slow if n is very large |
, need to compute inverse of |
- 经验参考:若 n > 10000,则不再考虑 normal equation ;
2.2 当矩阵不可逆时的处理方法
- 当 为不可逆矩阵时:
point 1: redundant features (存在冗余特征).
point 2: too many features (e.g.m ≤ n).
solution to point 2: delete some features, or use regularization.