Matlab實現線性迴歸和邏輯迴歸: Linear Regression & Logistic Regression

本文爲Maching Learning 欄目補充內容,爲上幾章中所提到單參數線性迴歸多參數線性迴歸和 邏輯迴歸的總結版。旨在幫助大家更好地理解迴歸,所以我在Matlab中分別對他們予以實現,在本文中由易到難地逐個介紹。

本講內容:

Matlab 實現各種迴歸函數

=========================

基本模型

Y=θ0+θ1X1型---線性迴歸(直線擬合)

解決過擬合問題---Regularization

Y=1/(1+e^X)型---邏輯迴歸(sigmod 函數擬合)

=========================


第一部分:基本模型


在解決擬合問題的解決之前,我們首先回憶一下線性迴歸和邏輯迴歸的基本模型。

設待擬合參數 θn*1 和輸入參數[ xm*n, ym*1 ] 。


對於各類擬合我們都要根據梯度下降的算法,給出兩部分:

①   cost function(指出真實值y與擬合值h<hypothesis>之間的距離):給出cost function 的表達式,每次迭代保證cost function的量減小;給出梯度gradient,即cost function對每一個參數θ的求導結果。

function [ jVal,gradient ] = costFunction ( theta )

 

②   Gradient_descent(主函數):用來運行梯度下降算法,調用上面的cost function進行不斷迭代,直到最大迭代次數達到給定標準或者cost function返回值不再減小。

function [optTheta,functionVal,exitFlag]=Gradient_descent( )

 

線性迴歸:擬合方程爲hθ(x)=θ0x01x1+…+θnxn,當然也可以有xn的冪次方作爲線性迴歸項(如),這與普通意義上的線性不同,而是類似多項式的概念。

其cost function 爲:

 

邏輯迴歸:擬合方程爲hθ(x)=1/(1+e^(θTx)),其cost function 爲:

 

cost function對各θj的求導請自行求取,看第三章最後一圖,或者參見後文代碼。

後面,我們分別對幾個模型方程進行擬合,給出代碼,並用matlab中的fit函數進行驗證。




第二部分:Y=θ0+θ1X1型---線性迴歸(直線擬合)

Matlab 線性擬合 & 非線性擬合中我們已經講過如何用matlab自帶函數fit進行直線和曲線的擬合,非常實用。而這裏我們是進行ML課程的學習,因此研究如何利用前面講到的梯度下降法(gradient descent)進行擬合。


cost function:
  1. function [ jVal,gradient ] = costFunction2( theta )  
  2. %COSTFUNCTION2 Summary of this function goes here  
  3. %   linear regression -> y=theta0 + theta1*x  
  4. %   parameter: x:m*n  theta:n*1   y:m*1   (m=4,n=1)  
  5. %     
  6.   
  7. %Data  
  8. x=[1;2;3;4];  
  9. y=[1.1;2.2;2.7;3.8];  
  10. m=size(x,1);  
  11.   
  12. hypothesis = h_func(x,theta);  
  13. delta = hypothesis - y;  
  14. jVal=sum(delta.^2);  
  15.   
  16. gradient(1)=sum(delta)/m;  
  17. gradient(2)=sum(delta.*x)/m;  
  18.   
  19. end  

其中,h_func是hypothesis的結果:
  1. function [res] = h_func(inputx,theta)  
  2. %H_FUNC Summary of this function goes here  
  3. %   Detailed explanation goes here  
  4.   
  5.   
  6. %cost function 2  
  7. res= theta(1)+theta(2)*inputx;function [res] = h_func(inputx,theta)  
  8. end  


Gradient_descent:
  1. function [optTheta,functionVal,exitFlag]=Gradient_descent( )  
  2. %GRADIENT_DESCENT Summary of this function goes here  
  3. %   Detailed explanation goes here  
  4.   
  5.   options = optimset('GradObj','on','MaxIter',100);  
  6.   initialTheta = zeros(2,1);  
  7.   [optTheta,functionVal,exitFlag] = fminunc(@costFunction2,initialTheta,options);  
  8.   
  9. end  

result:
  1. >> [optTheta,functionVal,exitFlag] = Gradient_descent()  
  2.   
  3. Local minimum found.  
  4.   
  5. Optimization completed because the size of the gradient is less than  
  6. the default value of the function tolerance.  
  7.   
  8. <stopping criteria details>  
  9.   
  10.   
  11. optTheta =  
  12.   
  13.     0.3000  
  14.     0.8600  
  15.   
  16.   
  17. functionVal =  
  18.   
  19.     0.0720  
  20.   
  21.   
  22. exitFlag =  
  23.   
  24.      1  



即得y=0.3+0.86x;
驗證:
  1. function [ parameter ] = checkcostfunc(  )  
  2. %CHECKC2 Summary of this function goes here  
  3. %   check if the cost function works well  
  4. %   check with the matlab fit function as standard  
  5.   
  6. %check cost function 2  
  7. x=[1;2;3;4];  
  8. y=[1.1;2.2;2.7;3.8];  
  9.   
  10. EXPR= {'x','1'};  
  11. p=fittype(EXPR);  
  12. parameter=fit(x,y,p);  
  13.   
  14. end  

運行結果:
  1. >> checkcostfunc()  
  2.   
  3. ans =   
  4.   
  5.      Linear model:  
  6.      ans(x) = a*x + b  
  7.      Coefficients (with 95% confidence bounds):  
  8.        a =        0.86  (0.4949, 1.225)  
  9.        b =         0.3  (-0.6998, 1.3)  

和我們的結果一樣。下面畫圖:
  1. function PlotFunc( xstart,xend )  
  2. %PLOTFUNC Summary of this function goes here  
  3. %   draw original data and the fitted   
  4.   
  5.   
  6.   
  7. %===================cost function 2====linear regression  
  8. %original data  
  9. x1=[1;2;3;4];  
  10. y1=[1.1;2.2;2.7;3.8];  
  11. %plot(x1,y1,'ro-','MarkerSize',10);  
  12. plot(x1,y1,'rx','MarkerSize',10);  
  13. hold on;  
  14.   
  15. %fitted line - 擬合曲線  
  16. x_co=xstart:0.1:xend;  
  17. y_co=0.3+0.86*x_co;  
  18. %plot(x_co,y_co,'g');  
  19. plot(x_co,y_co);  
  20.   
  21. hold off;  
  22. end  




第三部分:解決過擬合問題---Regularization

過擬合問題解決方法我們已在第三章中講過,利用Regularization的方法就是在cost function中加入關於θ的項,使得部分θ的值偏小,從而達到fit效果。
例如定義costfunction J(θ): jVal=(theta(1)-5)^2+(theta(2)-5)^2;

在每次迭代中,按照gradient descent的方法更新參數θ:θ(i)-=gradient(i),其中gradient(i)是J(θ)對θi求導的函數式,在此例中就有gradient(1)=2*(theta(1)-5), gradient(2)=2*(theta(2)-5)。


函數costFunction, 定義jVal=J(θ)和對兩個θ的gradient:


  1. function [ jVal,gradient ] = costFunction( theta )  
  2. %COSTFUNCTION Summary of this function goes here  
  3. %   Detailed explanation goes here  
  4.   
  5. jVal= (theta(1)-5)^2+(theta(2)-5)^2;  
  6.   
  7. gradient = zeros(2,1);  
  8. %code to compute derivative to theta  
  9. gradient(1) = 2 * (theta(1)-5);  
  10. gradient(2) = 2 * (theta(2)-5);  
  11.   
  12. end  

Gradient_descent,進行參數優化
  1. function [optTheta,functionVal,exitFlag]=Gradient_descent( )  
  2. %GRADIENT_DESCENT Summary of this function goes here  
  3. %   Detailed explanation goes here  
  4.   
  5.  options = optimset('GradObj','on','MaxIter',100);  
  6.  initialTheta = zeros(2,1)  
  7.  [optTheta,functionVal,exitFlag] = fminunc(@costFunction,initialTheta,options);  
  8.     
  9. end  

matlab主窗口中調用,得到優化厚的參數(θ1,θ2)=(5,5)
  1.  [optTheta,functionVal,exitFlag] = Gradient_descent()  
  2.   
  3. initialTheta =  
  4.   
  5.      0  
  6.      0  
  7.   
  8.   
  9. Local minimum found.  
  10.   
  11. Optimization completed because the size of the gradient is less than  
  12. the default value of the function tolerance.  
  13.   
  14. <stopping criteria details>  
  15.   
  16.   
  17. optTheta =  
  18.   
  19.      5  
  20.      5  
  21.   
  22.   
  23. functionVal =  
  24.   
  25.      0  
  26.   
  27.   
  28. exitFlag =  
  29.   
  30.      1  


第四部分:Y=1/(1+e^X)型---邏輯迴歸(sigmod 函數擬合)

hypothesis function:
  1. function [res] = h_func(inputx,theta)  
  2.   
  3. %cost function 3  
  4. tmp=theta(1)+theta(2)*inputx;%m*1  
  5. res=1./(1+exp(-tmp));%m*1  
  6.   
  7. end  

cost function:
  1. function [ jVal,gradient ] = costFunction3( theta )  
  2. %COSTFUNCTION3 Summary of this function goes here  
  3. %   Logistic Regression  
  4.   
  5. x=[-3;      -2;     -1;     0;      1;      2;     3];  
  6. y=[0.01;    0.05;   0.3;    0.45;   0.8;    1.1;    0.99];  
  7. m=size(x,1);  
  8.   
  9. %hypothesis  data  
  10. hypothesis = h_func(x,theta);  
  11.   
  12. %jVal-cost function  &  gradient updating  
  13. jVal=-sum(log(hypothesis+0.01).*y + (1-y).*log(1-hypothesis+0.01))/m;  
  14. gradient(1)=sum(hypothesis-y)/m;   %reflect to theta1  
  15. gradient(2)=sum((hypothesis-y).*x)/m;    %reflect to theta 2  
  16.   
  17. end  

Gradient_descent:
  1. function [optTheta,functionVal,exitFlag]=Gradient_descent( )  
  2.   
  3.  options = optimset('GradObj','on','MaxIter',100);  
  4.  initialTheta = [0;0];  
  5.  [optTheta,functionVal,exitFlag] = fminunc(@costFunction3,initialTheta,options);  
  6.   
  7. end  

運行結果:
  1.  [optTheta,functionVal,exitFlag] = Gradient_descent()  
  2.   
  3. Local minimum found.  
  4.   
  5. Optimization completed because the size of the gradient is less than  
  6. the default value of the function tolerance.  
  7.   
  8. <stopping criteria details>  
  9.   
  10.   
  11. optTheta =  
  12.   
  13.     0.3526  
  14.     1.7573  
  15.   
  16.   
  17. functionVal =  
  18.   
  19.     0.2498  
  20.   
  21.   
  22. exitFlag =  
  23.   
  24.      1  

畫圖驗證:

  1. function PlotFunc( xstart,xend )  
  2. %PLOTFUNC Summary of this function goes here  
  3. %   draw original data and the fitted   
  4.   
  5. %===================cost function 3=====logistic regression  
  6.   
  7. %original data  
  8. x=[-3;      -2;     -1;     0;      1;      2;     3];  
  9. y=[0.01;    0.05;   0.3;    0.45;   0.8;    1.1;    0.99];  
  10. plot(x,y,'rx','MarkerSize',10);  
  11. hold on  
  12.   
  13. %fitted line  
  14. x_co=xstart:0.1:xend;  
  15. theta = [0.3526,1.7573];  
  16. y_co=h_func(x_co,theta);  
  17. plot(x_co,y_co);  
  18. hold off  
  19.   
  20. end  



有朋友問,這裏就補充一下logistic regression中gradient的推導:
則有
由於cost function
可得
所以gradient = -J'(theta) = (z-y)x



關於Machine Learning更多的學習資料將繼續更新,敬請關注本博客和新浪微博Sophia_qing

==============================
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章