首先对一堆样本点我们假设目标函数为:
在Gradient Descent Algorithm中,我们利用不断推导得到两个对此算法非常重要的公式,一个是J(θ)是代价函数:
使用偏导数的方式使得theta的值逐步取得满足当前代价函数的最小值,
我们在梯度下降中,直接使用这两个公式,来绘制J(θ)的分布曲面,以及θ的求解路径。
命题为:我们为一家连锁餐饮企业新店开张的选址进行利润估算,手中掌握了该连锁集团所辖店铺当地人口数据,及利润金额,需要使用线性回归算法来建立人口与利润的关系,进而为新店进行利润估算,以评估店铺运营前景。
首先我们将该企业的数据绘制在坐标图上,如下图所示,我们需要建立的模型是一条直线,能够在最佳程度上,拟合population与profit之间的关系。其模型为:
关键代码:
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(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
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
x = X.*((X*theta)-y);
theta = theta - (alpha*sum(x)/m)';
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
下图是J(θ)的分布曲面:
取得最小theta值时的h(x)函数图像:
我们求得的最佳θ值在在图像得最低点,极小值也是最小值: