线性回归
回归问题
回归分析(Regression Analysis)是一种统计学上分析数据的方法,目的在于了解两个或多个变数间是否相关、相关方向与强度,并建立数学模型以便观察特定变数来预测研究者感兴趣的变数。更具体的来说,回归分析可以帮助人们了解在只有一个自变量变化时因变量的变化量,因此可以用来做数据预测。
一维线性回归
假设我们获得了一份房子大小和价格数据集,如下表所示
Size(x) | Price(y) |
---|---|
2104 | 460 |
1035 | 224 |
868 | 230 |
642 | 126 |
… | … |
我们的任务是找到房价和房屋大小的关系函数,使我们可以根据房子大小来预测房价。显然,我们可以设计一个简单的一维线性函数
其中
代价函数
这个式子的含义是找到
二维梯度下降
由于我们的
那么
梯度下降是求多维函数的极值方法,因此上述公式是对
对上述例子,我们只需要分别对
注意,上述例子中我们的cost函数是凸函数(convex function),因此上述两个式子没有局部极值点,只有全局唯一的一个极值点。梯度下降法通常在离极值点远的地方下降很快,但在极值点附近时会收敛速度很慢。因此,梯度下降法的解是全局最优解。而在一般情况下,梯度下降法不保证求得全局最优解。
多维线性回归
回到第一节开头的例子,实际生活中影响房价的因素很多,比如房屋数量,楼层数等等,因此房价的变化是多个变量相互作用的
Size(x1) | #bed room (x2) | #floors(x3) | Price(y) |
---|---|---|---|
2104 | 5 | 2 | 460 |
1035 | 4 | 1 | 224 |
868 | 3 | 2 | 230 |
642 | 2 | 1 | 126 |
… | … | … | … |
由于多个feature的影响,此时我们的预测函数将变成多维:
注意上述式子中
默认为1,即
如果将
上述式子给出了单个样本的预测函数,实际应用中上我们的数据集里有多个样本,这里我们用上角标表示,如下
表示样本数 表示feature个数 表示第 组训练样本 表示第 个样本中的第 个feature
注意,前面一维线性归回的预测函数中,每条样本用
表示,原因是我们只有一个feature,我们可以用下角标表示每条数据,后面我们统一使用上角标来表示
举例来说,
多维梯度下降
参考前面二维梯度下降的求法,多维梯度下降的求法相同
线性回归梯度计算的 Ocatave Demo
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
% theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
num_features = size(X,2);
h = X*theta;
for j = 1:num_features
x = X(:,j);
theta(j) = theta(j) - alpha*(1/m)*sum((h-y).* x);
end
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCostMulti(X, y, theta);
end
end
Feature Scaling
Idea: Make sure features are on a similar scale, e.g.,
x1 = size(0-200 feet)
x2=number of bedrooms(1-5)
这种情况 contour 图是一个瘦长的椭圆, 在不优化的情况下,这类梯度下降速度很慢。如果我们将x1
和x2
做如下调整:
x1 = size(0-200 feet)/5
x2=(number of bedrooms)/5
则 contour 图会变为接近圆形,梯度下降收敛的速度会加快。通常为了加速收敛,会将每个 feature 值(每个xi
)统一到某个区间里,比如
Mean normalization
Replace
实际上就是将 feature 归一化,例如
x1=(size-1000)/2000
x2=(#bedrooms-2)/5
则有
是所有 是x_i
的区间范围,即
Note that dividing by the range, or dividing by the standard deviation, give different results. For example, if 100
to 2000
and a mean value of 1000
, then
μ
表示所有 feature 的平均值s = max - min
Learning Rate
这节讨论如何选取α
。为了求解J(θ)
的最小值,梯度下降会不断的迭代找出最小值,理论上来说随着迭代次数的增加,J(θ)
将逐渐减小,如图:
但是如果α
选取过大,则可能会导致越过极值点的情况,导致随着迭代次数的增加,J(θ)
的值增加或忽高忽低不稳定的情况:
解决办法都是选取较小的α
值
- if
α
is too small -> slow convergence - if
α
is too large ->J(θ)
may not decrease on every iteration; may not converge - To choose
α
, try: …, 0.001, 0.003, 0.01,0.03, 0.1,0.3, 1, …
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 combine x1 and x2 into a new feature x3 by taking x1⋅x2.
例如我们可以将两个 feature 合成一个:x3 = x1*x2
,使用x3
作为先行回归的 feature 值。
另外,如果只有一个 feature,而使用线性函数又不适合描述完整的数据集,可以考虑多项式函数,比如使用二次函数或者三次函数:
可以令
Normal Equation
对于 cost 函数:
前面提到的求 J(θ)
最小值的思路是使用梯度下降法,对
出了梯度下降法之外,还有一种方法叫做Normal Equation,这种方式不需要迭代,可以直接计算出θ值
假设我们有
令线性回归函数:
则有:
是 的矩阵 是 的矩阵 是 的矩阵
看个例子:
若希望
- 单位矩阵
, - 矩阵的逆
, 必须为方阵,
再来看看式子
-
先把
左边的矩阵变成一个方阵。通过乘以 可以实现,则有: -
把
左边的部分变成一个单位矩阵,这样左边就只剩下 : -
由于
,因此式子变为: 这就是 Normal Equation 的表达式。
如果用 Octave 表示,命令为:pinv(X'*X)*X'*Y
什么 case 适合使用 Normal Equation,什么 case 适合使用 Gradient Descent?
Gradient Descent | Normal Equation |
---|---|
Need to choose alpha | No need to choose alpha |
Needs many iterations | No need to iterate |
need to calculate inverse of | |
Works well when n is large | Slow if n is very large |
当样本数量
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
If
- 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.