Linear Regression (Gradient Descent & Normal Equation)
2018-02-01 13:48
393 查看
#机器学习中的线性回归有两种方法:
(1)梯度下降-Gradient Descent
(2)正规方程-Normal Equation
#如果满足以下两个条件,个人推荐优先使用正规方程:
(1)样本变量不存在线性相关,如果有,请删除某列样本变量。例如:‘出生年份’与‘年龄’存在线性相关,则删除其中的某项;
(2)样本变量n不是非常多。因为,如果有10000个变量等,正规方程计算耗时会很长,梯度下降的时间复杂度是o(kn^2),而正规方程式o(n^3)。但如果样本变量很多,那么就推荐使用梯度下降,因为会节省时间。
#之所以优先推荐正规方程是因为以下几个原因:
(1)梯度下降需要根据数据集的变化选择合适的回归数学模型,例如:
h_theta(x)=theta_0+theta_1*x_1+theta_1*x_2
有可能出现X_2=(X_1)^3,这样数据走势可能是呈现指数曲线的走势。
(2)随机梯度下降(Stochastic Gradient Descent, SGD)可能会陷入局部最优的情况。 随机梯度下降在计算下降最快的方向时时随机选一个数据进行计算,而不是扫描全部训练数据集,这样就加快了迭代速度。随机梯度下降并不是沿着J(θ)下降最快的方向收敛,而是震荡的方式趋向极小点。
theta = zeros(n+1,1);
for i=1:m
theta = theta - alpha*(1/m)*(X*theta-y).*X; %Stochastic Gradient Descent code
end
但如果使用批梯度下降(batch gradient descent)就可以避免陷入局部最优的局面,可是这样的话,运算量也会变大,不过一般情况其实还好。
#对于如何在随机梯度下降和批梯度下降之间选择的方法是:
(1)计算代价函数:
(3)正规方程不需要设置alpha(learning rate)。
(1)梯度下降-Gradient Descent
(2)正规方程-Normal Equation
#如果满足以下两个条件,个人推荐优先使用正规方程:
(1)样本变量不存在线性相关,如果有,请删除某列样本变量。例如:‘出生年份’与‘年龄’存在线性相关,则删除其中的某项;
(2)样本变量n不是非常多。因为,如果有10000个变量等,正规方程计算耗时会很长,梯度下降的时间复杂度是o(kn^2),而正规方程式o(n^3)。但如果样本变量很多,那么就推荐使用梯度下降,因为会节省时间。
#之所以优先推荐正规方程是因为以下几个原因:
(1)梯度下降需要根据数据集的变化选择合适的回归数学模型,例如:
h_theta(x)=theta_0+theta_1*x_1+theta_1*x_2
有可能出现X_2=(X_1)^3,这样数据走势可能是呈现指数曲线的走势。
(2)随机梯度下降(Stochastic Gradient Descent, SGD)可能会陷入局部最优的情况。 随机梯度下降在计算下降最快的方向时时随机选一个数据进行计算,而不是扫描全部训练数据集,这样就加快了迭代速度。随机梯度下降并不是沿着J(θ)下降最快的方向收敛,而是震荡的方式趋向极小点。
theta = zeros(n+1,1);
for i=1:m
theta = theta - alpha*(1/m)*(X*theta-y).*X; %Stochastic Gradient Descent code
end
但如果使用批梯度下降(batch gradient descent)就可以避免陷入局部最优的局面,可是这样的话,运算量也会变大,不过一般情况其实还好。
for iter = 1:num_iters theta = theta - alpha*(1/m)*(sum((X*theta-y).*X))'; %batch gradient descent code end
#对于如何在随机梯度下降和批梯度下降之间选择的方法是:
(1)计算代价函数:
J(theta)=1/(2m)*sum((h_theta(x)-y)^2) J = 1/(2*m)*sum(((theta'*X')'-y).^2); %cost function(2)利用surf绘制代价函数得到的结果,观察是否为bowl-shaped(Convex function),如果是,则说明只存在一个最优情况,使用随机梯度下降,如果不是,说明存在局部极值的情况,使用批梯度下降。
(3)正规方程不需要设置alpha(learning rate)。
theta = ((X^T) *X)^(-1) * (X^T) * y theta = pinv(X'*X)*X'*y %normal equation code
相关文章推荐
- 线性回归 numpy normal equation & tensorflow gradient descent
- 多变量线性回归中的批量梯度下降法(Batch Gradient Descent in Linear Regression with Multiple Variable)
- 线性回归与梯度下降(linear regression and gradient descent)
- 2 - 7 - Gradient Descent For Linear Regression (10 min)
- [ML of Andrew Ng]Week 2 : Linear Regression with Multiple Variables and Normal Equation
- 我的第一个ML算法(linear regression gradient descent)
- Gradient Descent for Linear Regression,线性回归的梯度下降算法
- linear regression(4)-normal equation***compare with gradient descent
- 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent
- Linear regression and gradient descent algorithm
- 【转】Derivation of the Normal Equation for linear regression
- (一)How to Do Linear Regression using Gradient Descent
- [置顶] 线性回归和梯度下降(Linear Regression and Gradient Descent)
- [blabla]a quick code about linear regression using gradient descent
- Linear Regression and Normal Equation
- 第一讲.Liner_Regression and Gradient_Descent(Rui Xia) 单变量线性回归及梯度下降
- 机器学习中的数学(1)-回归(regression)、梯度下降(gradient descent)
- Linear regression with one variable算法实例讲解(绘制图像,cost_Function ,Gradient Desent, 拟合曲线, 轮廓图绘制)_矩阵操作
- Machine Learning --- Linear regression & Rige & Robust regression & Lasso
- Matlab实现线性回归和逻辑回归: Linear Regression & Logistic Regression