【机器学习笔记4】Stanford公开课Exercise 3——Multivariate Linear Regression

Stanford公开课Exercise 3原题地址:http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=MachineLearning&doc=exercises/ex3/ex3.html,下面是我完成的笔记。



第一部分,gradient descent方法


(一)原理回顾


简单重复一下gradient descent实现的过程,具体的看前面的文章(【机器学习笔记2】Linear Regression总结):

1. h(θ)函数

                                                                  (公式1)

2. J(θ)函数

                                                            (公式2)

向量化后简化为:

                                                                                         (公式3)

3. θ迭代过程

                                                     (公式4)

向量化后简化为:

                                                       (公式5)

4. Feature Scaling

将不同特征的取值转换到差不多的范围内,具体做法是:特征值减去该组特征值的均值,然后除以该组特征值的标准差,将所有特征值归一化至[-1,1]的范围。
               (公式6)


(二)实现代码


[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. %=================================================================  
  2. % Exercise 3: Multivariate Linear Regression (gradient descent)  
  3. % author : liubing (liubing80386@163.com)  
  4. %  
  5.   
  6. %LB_c: 加载数据 ===============  
  7. x = load("ex3x.dat");  
  8. y = load("ex3y.dat");  
  9. %LB_c: 矩阵x第一列加上全1  
  10. x = [ones(size(x)(1),1), x];  
  11. %==============================  
  12.   
  13. %LB_c: scaling(缩放到相同范围),参考上面的(公式6)  
  14. sigma = std(x); %x按列求标准差  
  15. mu = mean(x);   %x按列求均值  
  16. %第2、3列scaling(第1列全为1,不用做)  
  17. x(:,2) = (x(:,2) - mu(2)) ./ sigma(2);  
  18. x(:,3) = (x(:,3) - mu(3)) ./ sigma(3);  
  19. %==================================================  
  20.   
  21. %LB_c: 常量数据准备=============================================================  
  22. %theta更新的迭代次数  
  23. iter_total = 50;      
  24. %m为样本数,n为特征数(包含第一列的常数项,实际特征数为n-1)  
  25. [m,n] = size(x);      
  26. %学习率的不同取值,共7个,实验结果表明前6个收敛,最后一个发散  
  27. alpha = [0.01, 0.03, 0.1, 0.3, 1, 1.3, 1.4];      
  28. %对应不同学习率结果绘制的属性  
  29. plot_arg  = ['c', 'r', 'g', 'b', 'm', 'k', 'r', 'b'];  
  30. alpha_total = length(alpha);    %不同学习率种数  
  31. theta_arr = zeros(alpha_total, n);  %存储所有alpha值对应的theta结果  
  32. %===============================================================================  
  33.   
  34. %LB_c ==========================================================================  
  35. %尝试不同的alpha值,绘制J值迭代趋势,theta结果保存到theta_arr中  
  36. figure;  
  37. title("J(theta) converge");  
  38. xlabel('iteration (times)');  
  39. ylabel('J(theta)');  
  40. for alpha_index = [1:alpha_total]  
  41.   
  42.     theta = zeros(n,1); %theta初始化为全0,其他值也可以  
  43.     J = zeros(iter_total,1);    %存储每一步迭代的J(theta)值  
  44.       
  45.     %迭代过程  
  46.     for iter_index = [1:iter_total]       
  47.         J(iter_index) = (x*theta-y)' * (x*theta-y) / (2*m); %求当前的J(theta),参考上面的(公式3)  
  48.         err = x * theta - y;  
  49.         grad = ( x' * err ) / m;    %求gradient  
  50.         theta = theta - alpha(alpha_index) * grad;  %梯度下降法更新theta,参考上面的(公式5)  
  51.     end  
  52.       
  53.     %保存当前alpha的theta结果  
  54.     theta_arr(alpha_index,:) = theta';    
  55.     %绘制当前alpha的J值迭代趋势  
  56.     if ( alpha_index == 7 )  
  57.         legend('0.01', '0.03', '0.1', '0.3', '1', '1.3');  
  58.         figure;  
  59.         title("J(theta) diverge");  
  60.         xlabel('iteration (times)');  
  61.         ylabel('J(theta)');  
  62.     end  
  63.     hold on;  
  64.     plot([0:49], J, plot_arg(alpha_index), 'LineWidth', 2);  
  65.       
  66.     %a = input("continue : ");  
  67. end  
  68. legend('1.4');  
  69. %===============================================================================  
  70.   
  71. %LB_c:结果输出 ======================================================================  
  72. alpha = theta_arr(5);  
  73. printf("theta for alpha=1 : \n");  
  74. theta_arr(5,:)  
  75. test_x = [1, 1650, 3];  %测试数据  
  76. test_x(2) = (test_x(2) - mu(2)) / sigma(2); %scaling  
  77. test_x(3) = (test_x(3) - mu(3)) / sigma(3); %scaling  
  78. predict_price = test_x * theta_arr(5,:)';   %计算预测值  
  79. printf("predicted price for test data(1650-square-foot house with 3 bedrooms) : \n");  
  80. predict_price  
  81. %=====================================================================================  

(三)执行结果


1. J(θ)收敛的情况,改图对比了当学习率α分别为0.01、0.03、0.1、0.3、1和1.3时J(θ)的收敛趋势,根据对比来选择合适的学习率。本例中,因为α为1时收敛最快而且效果相当,所以选择了α为1。



2. J(θ)发散的情况,当α为1.4时,J(θ)发散的非常厉害,而且实验中发现当α为1.5,1.6甚至更大时发散更加快,J(θ)甚至相差多个数量级,无法在一个图中绘制出来。由此可以看出,α的选取在gradient descent中是非常重要的。



3. 下面是一些输出结果:当α=1时最终求得的回归系数theta,并用该theta值预测题目中给出的测试例子(1650平米,3个卧室),得到的预测房价。得到的结果与题目中给出的solution基本一致。





第二部分,normal equation方法


原题还要求用normal equation实现,并与gradient descent方法进行对比。normal equation的实现很简单,就是前面的文章(【机器学习笔记2】Linear Regression总结)中提到的公式:

                                                            (公式7)

实现代码如下:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. %=================================================================  
  2. % Exercise 3: Multivariate Linear Regression (normal equation)  
  3. % author : liubing (liubing80386@163.com)  
  4. %  
  5.   
  6. %LB_c: 加载数据 ===============  
  7. x = load("ex3x.dat");  
  8. y = load("ex3y.dat");  
  9. %LB_c: 矩阵x第一列加上全1  
  10. x = [ones(size(x)(1),1), x];  
  11. %==============================  
  12.   
  13. theta = inv(x'*x)*x'*y; %normal equation,参考上面的(公式7)  
  14. test_x = [1, 1650, 3];  %测试数据  
  15. price = test_x * theta; %计算预测值  
  16.   
  17. printf("theta from normal equation : \n");  
  18. theta  
  19. printf("predicted price for test data(1650-square-foot house with 3 bedrooms) : \n");  
  20. price  

执行结果



可以看出,normal equation方法得到的预测值与gradient descent方法得到的是一样(2.9308e+005)。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值