原文:http://blog.csdn.net/abcjennifer/article/details/7732417php
本講內容:算法
Matlab 實現各類迴歸函數less
=========================ide
基本模型函數
Y=θ0+θ1X1型---線性迴歸(直線擬合)學習
解決過擬合問題---Regularization優化
Y=1/(1+e^X)型---邏輯迴歸(sigmod 函數擬合)this
在解決擬合問題的解決以前,咱們首先回憶一下線性迴歸和邏輯迴歸的基本模型。spa
設待擬合參數 θn*1 和輸入參數[ xm*n, ym*1 ] 。.net
對於各種擬合咱們都要根據梯度降低的算法,給出兩部分:
① 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)=θ0x0+θ1x1+…+θnxn,固然也能夠有xn的冪次方做爲線性迴歸項(如),這與普通意義上的線性不一樣,而是相似多項式的概念。
其cost function 爲:
邏輯迴歸:擬合方程爲hθ(x)=1/(1+e^(θTx)),其cost function 爲:
cost function對各θj的求導請自行求取,看第三章最後一圖,或者參見後文代碼。
後面,咱們分別對幾個模型方程進行擬合,給出代碼,並用matlab中的fit函數進行驗證。
在Matlab 線性擬合 & 非線性擬合中咱們已經講過如何用matlab自帶函數fit進行直線和曲線的擬合,很是實用。而這裏咱們是進行ML課程的學習,所以研究如何利用前面講到的梯度降低法(gradient descent)進行擬合。
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
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
在每次迭代中,按照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