一元线性回归梯度下降算法的Octave仿真

人工智能 机器学习 算法
在梯度下降算法理论篇中,曾经感叹推导过程如此苍白,如此期待仿真来给我更加直观的感觉。当我昨晚Octave仿真后,那种成就感着实难以抑制。分享一下仿真的过程和结果,并且将上篇中未理解透澈的内容补上。

[[190464]]

在梯度下降算法理论篇中,曾经感叹推导过程如此苍白,如此期待仿真来给我更加直观的感觉。当我昨晚Octave仿真后,那种成就感着实难以抑制。分享一下仿真的过程和结果,并且将上篇中未理解透澈的内容补上。

在Gradient Descent Algorithm中,我们利用不断推导得到两个对此算法非常重要的公式,一个是J(θ)的求解公式,另一个是θ的求解公式:

我们在仿真中,直接使用这两个公式,来绘制J(θ)的分布曲面,以及θ的求解路径。

命题为:我们为一家连锁餐饮企业新店开张的选址进行利润估算,手中掌握了该连锁集团所辖店铺当地人口数据,及利润金额,需要使用线性回归算法来建立人口与利润的关系,进而为新店进行利润估算,以评估店铺运营前景。

首先我们将该企业的数据绘制在坐标图上,如下图所示,我们需要建立的模型是一条直线,能够在最佳程度上,拟合population与profit之间的关系。其模型为:

在逼近θ的过程中,我们如下实现梯度下降:进行了1500次的迭代(相当于朝着最佳拟合点行走1500步),我们在1500步后,得到θ=[-3.630291,1.166362];在3000次迭代后,其值为[-3.878051,1.191253];而如果运行10万次,其值为[-3.895781,1.193034]。可见,最初的步子走的是非常大的,而后,由于距离最佳拟合点越来越近,梯度越来越小,所以步子也会越来越小。为了节约运算时间,1500步是一个完全够用的迭代次数。之后,我们绘制出拟合好的曲线,可以看得出,拟合程度还是不错的。

下图是J(θ)的分布曲面:

接来下是我们求得的最佳θ值在等高线图上所在的位置,和上一张图其实可以重合在一起:

关键代码如下:

1、计算j(theta)

  1. function J = computeCost(X, y, theta) 
  2. %COMPUTECOST Compute cost for linear regression 
  3. %   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the 
  4. %   parameter for linear regression to fit the data points in X and y 
  5.   
  6. % Initialize some useful values 
  7. m = length(y); % number of training examples 
  8.   
  9. % You need to return the following variables correctly 
  10. J = 0; 
  11.   
  12. % ====================== YOUR CODE HERE ====================== 
  13. % Instructions: Compute the cost of a particular choice of theta 
  14. %               You should set J to the cost. 
  15.     h = X*theta; 
  16.     e = h-y; 
  17.     J = e'*e/(2*m) 
  18. % ========================================================================= 
  19.   
  20. end 

2、梯度下降算法:

  1. function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters) 
  2. %GRADIENTDESCENT Performs gradient descent to learn theta 
  3. %   theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by 
  4. %   taking num_iters gradient steps with learning rate alpha 
  5.   
  6. % Initialize some useful values 
  7. m = length(y); % number of training examples 
  8. J_history = zeros(num_iters, 1); 
  9.   
  10. for iter = 1:num_iters 
  11.   
  12.     % ====================== YOUR CODE HERE ====================== 
  13.     % Instructions: Perform a single gradient step on the parameter vector 
  14.     %               theta. 
  15.     % 
  16.     % Hint: While debugging, it can be useful to print out the values 
  17.     %       of the cost function (computeCost) and gradient here. 
  18.     % 
  19.       
  20.     h=X*theta; 
  21.     e=h-y; 
  22.     theta = theta-alpha*(X'*e)/m; 
  23.   
  24.     % ============================================================ 
  25.   
  26.     % Save the cost J in every iteration    
  27.     J_history(iter) = computeCost(X, y, theta); 
  28.   
  29. end 
  30.   
  31. end 
责任编辑:武晓燕 来源: 博客园
相关推荐

2017-07-25 12:59:10

机器学习梯度下降算法

2014-06-19 14:14:35

机器学习

2022-06-06 12:53:17

吴恩达AI机器学习

2016-11-28 09:24:08

Python内存技巧

2020-12-19 10:54:25

机器学习线性回归算法

2021-01-21 12:13:23

算法梯度下降网络

2017-08-28 18:41:34

PythonLogistic回归随机梯度下降

2017-03-22 12:25:29

机器学习梯度下降法

2023-05-17 11:33:45

梯度下降机器学习

2020-08-03 11:43:01

神经网络优化机器学习

2019-03-28 07:31:03

2021-10-26 16:10:50

神经网络AI算法

2018-07-20 14:58:16

深度学习梯度下降损失函数

2017-11-21 13:00:20

机器学习决策树可视化

2022-06-08 19:10:27

MarcusLeCun算法

2018-11-21 09:22:54

策略梯度算法机器学习强化学习

2011-05-26 09:33:27

改装连供经验

2022-11-06 15:56:50

2023-11-10 15:47:06

线性回归内核技巧

2016-12-28 14:43:46

京东数据库架构
点赞
收藏

51CTO技术栈公众号