Standford機器學習 邏輯迴歸(Logistic Regission)以及過擬合問題解決(Regularization)

1.分類問題
    判斷一封郵件是否爲垃圾郵件,判斷腫瘤是良性的還是惡性的,這些都是分類問題。在分類問題中,通常輸出值只有兩個(一般是兩類的問題,多類問題其實是兩類問題的推廣)(0叫做負類,1叫做正類)。給定一組數據,標記有特徵和類別,數據如(x(i),y(i)),由於輸出只有兩個值,如果用迴歸來解決會取得非常不好的效果。
   在良性腫瘤和惡性腫瘤的預測中,樣本數據如下
 
上圖是用線性歸回得到的結果,那麼可以選定一個閾值0.5,建立該模型後就可以預測:
 

如果訓練數據是這樣的


很明顯,這樣得到的結果是非常不準確的。線性迴歸中,雖然我們的樣本輸出數據都只有0和1,但是得到的輸出卻可以有大於1和小於0的,這不免有點奇怪。Logistic Regission的假設就是在0和1之間的。

 

2.Logistic Regression
    我們希望的是模型的輸出值在0和1之間,邏輯迴歸的假設,這個假設的推導在網易公開課的廣義線性模型中有提到(分類的概率滿足伯努利分佈),這個以後再說

g(z)的函數圖象是這樣的一個S型曲線

    現在只要假定,預測輸出爲正類的概率爲H (x;theta)(因爲根據該曲線,H是1的時候輸出剛好是1),根據概率之和爲1,可以得出如下式子

根據這個式子就可以來預測輸出的分類了。和前面的線性迴歸一樣,h(x)大於0.5的話,輸出有更大的概率是正類,所以把它預測成正類。

    從S型曲線可以看出,h(x)是單調遞增的,如果h(x)>0.5則x*theta>0反之,x*theta<0,這個反映到x的座標下,x*theta=0剛好是一條直線,x*theta>0和x*theta<0分佈在該直線的兩側,剛好可以把兩類樣本分開。

如果數據是下面這樣的,很明顯一條直線無法將它隔開

因此需要像多項式迴歸一樣在x中添加一些feature,如

    和前面一樣y=theta0+theta1*x1+theta2*x2+theta3*x1^2+theta4*x2^2=0是一條曲線,y>0和y<0分佈在該曲線兩側。得到了以上模型,只要用學習算法學習出最優的theta值就行了。
要學習參數theta,首先要確定學習的目標,即Cost Function。在線性迴歸中,我們選取的Cost Function是
使得每個樣本點到曲線的均方誤差最小,要注意Logistic Regission中,h(x)帶入J中得到的一個函數不是Convex的,形狀如這樣
   因此這樣的一個J(theta)不能用梯度下降法得到最優值,因爲有多個極值點。
 
由於這個文類問題中,兩類的概率滿足伯努利分佈,所以

 
這兩個式子可以寫成

 
給定一些樣本點,可以使用極大似然估計來估計這個模型,似然函數爲:


這裏要求L(theta)的最大值,所以在前面添個負號就變成了求最小值,就可以用梯度下降法求解了。

觀察J的前後兩項,都是單調函數,因此J是Convex函數,目標就是要最小化這個函數,因此可以用梯度下降法。


求偏導之後發現這個式子和線性迴歸中的那個式子的相同的,要注意的是這裏的h(theta)和線性迴歸中的是不一樣的,需要區分。這樣就得到了邏輯迴歸的分類模型!
 
3.過擬合問題以及解決方法(Regularization)
    
    下面三個例子中,二是擬合的比較好的,一中有着較大的MSE,不是很好的模型,這種情況叫做 under fit,第三種情況雖然準確得擬合了每一個樣本點,但是它的泛華能力會很差,這種情況叫做overfit。
在LogisticRegression中,上面三種情況對應的就是

Underfit和Overfit是實踐過程中需要避免的問題,那麼如何避免過擬合問題呢?
    第一種方法就是減少feature,上面的例子中可以減少x^2這樣的多項式項。
    第二種方法就是這裏要介紹的Regularization,Regularization是一種可以自動減少對預測結果沒有影響(或影響較小)的feature的方法。
 
在下面這個例子中,如果我們學習得到theta3和theta4都是0或者非常接近於0,那麼x的三次方項和四次方項這兩個feature可以忽略,而得到的模型就是左邊這個。

方法就是在原來的J後面加上懲罰項lambda*theta^2,這個例子中

優化過程中就會使得theta3和theta4儘量小,從而加懲罰因子的這些feature對模型的影響越小。
加上lambda後面的懲罰項(regularization parameter),這樣就得到了Regularization後的新的模型

    這裏懲罰項式從1開始到n的,沒有把0加進去,事實上,把0加進去對結果的影響非常小。
還有一個就是懲罰項係數lambda的選取問題,如果lambda選取的過大,那麼最後的theta會接近於0,那麼分割的曲線就會接近於直線,從而導致underfit(因爲如果lambda非常非常大,要得到和前面的(h-y)相當大小的數值theta裏面的所有元素就要很小),如果lambda過小,就相當於沒有懲罰項,就是overfit。
求偏導後,梯度下降法中的更新式就變成了
 

最後還要說一下,對convex函數的優化,matlab提供了相應的優化工具,你可以把它看成是一個黑盒,你只需要把你的Cost Function和初始的theta值給他,並告訴它你需要用到什麼樣的優化方法,他就會幫你優化。下面是具體的使用方法:
 
% Set Options
options = optimset('GradObj', 'on', 'MaxIter', 400);

% Optimize
[theta, J, exit_flag] = ...
	fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);

 

 

設置好選項,參數t會去調用你的costfunction,並用相應的你指定的方法優化相應的迭代次數。

總結:Logistic Regression和過擬合問題的解決方法是機器學習中非常重要的方法。貌似Google的搜索廣告的擺放就是用了邏輯迴歸算法。

 

 

 

附fminunc的文檔介紹

 

fminunc finds a local minimum of a function of several variables.
    X = fminunc(FUN,X0) starts at X0 and attempts to find a local minimizer
    X of the function FUN. FUN accepts input X and returns a scalar
    function value F evaluated at X. X0 can be a scalar, vector or matrix. 
 
    X = fminunc(FUN,X0,OPTIONS) minimizes with the default optimization
    parameters replaced by values in the structure OPTIONS, an argument
    created with the OPTIMSET function.  See OPTIMSET for details.  Used
    options are Display, TolX, TolFun, DerivativeCheck, Diagnostics,
    FunValCheck, GradObj, HessPattern, Hessian, HessMult, HessUpdate,
    InitialHessType, InitialHessMatrix, MaxFunEvals, MaxIter, DiffMinChange
    and DiffMaxChange, LargeScale, MaxPCGIter, PrecondBandWidth, TolPCG,
    PlotFcns, OutputFcn, and TypicalX. Use the GradObj option to specify
    that FUN also returns a second output argument G that is the partial
    derivatives of the function df/dX, at the point X. Use the Hessian
    option to specify that FUN also returns a third output argument H that
    is the 2nd partial derivatives of the function (the Hessian) at the
    point X. The Hessian is only used by the large-scale algorithm. 
 
    X = fminunc(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a
    structure with the function FUN in PROBLEM.objective, the start point
    in PROBLEM.x0, the options structure in PROBLEM.options, and solver
    name 'fminunc' in PROBLEM.solver. Use this syntax to solve at the 
    command line a problem exported from OPTIMTOOL. The structure PROBLEM 
    must have all the fields.
 
    [X,FVAL] = fminunc(FUN,X0,...) returns the value of the objective 
    function FUN at the solution X.
 
    [X,FVAL,EXITFLAG] = fminunc(FUN,X0,...) returns an EXITFLAG that 
    describes the exit condition of fminunc. Possible values of EXITFLAG 
    and the corresponding exit conditions are listed below. See the
    documentation for a complete description.
 
      1  Magnitude of gradient small enough. 
      2  Change in X too small.
      3  Change in objective function too small.
      5  Cannot decrease function along search direction.
      0  Too many function evaluations or iterations.
     -1  Stopped by output/plot function.
     -3  Problem seems unbounded. 
    
    [X,FVAL,EXITFLAG,OUTPUT] = fminunc(FUN,X0,...) returns a structure 
    OUTPUT with the number of iterations taken in OUTPUT.iterations, the 
    number of function evaluations in OUTPUT.funcCount, the algorithm used 
    in OUTPUT.algorithm, the number of CG iterations (if used) in
    OUTPUT.cgiterations, the first-order optimality (if used) in
    OUTPUT.firstorderopt, and the exit message in OUTPUT.message.
 
    [X,FVAL,EXITFLAG,OUTPUT,GRAD] = fminunc(FUN,X0,...) returns the value 
    of the gradient of FUN at the solution X.
 
    [X,FVAL,EXITFLAG,OUTPUT,GRAD,HESSIAN] = fminunc(FUN,X0,...) returns the 
    value of the Hessian of the objective function FUN at the solution X.
 
    Examples
      FUN can be specified using @:
         X = fminunc(@myfun,2)
 
    where myfun is a MATLAB function such as:
 
        function F = myfun(x)
        F = sin(x) + 3;
 
      To minimize this function with the gradient provided, modify
      the function myfun so the gradient is the second output argument:
         function [f,g] = myfun(x)
          f = sin(x) + 3;
          g = cos(x);
      and indicate the gradient value is available by creating an options
      structure with OPTIONS.GradObj set to 'on' (using OPTIMSET):
         options = optimset('GradObj','on');
         x = fminunc(@myfun,4,options);
 
      FUN can also be an anonymous function:
         x = fminunc(@(x) 5*x(1)^2 + x(2)^2,[5;1])
 
    If FUN is parameterized, you can use anonymous functions to capture the
    problem-dependent parameters. Suppose you want to minimize the 
    objective given in the function myfun, which is parameterized by its 
    second argument c. Here myfun is a MATLAB file function such as
 
      function [f,g] = myfun(x,c)
 
      f = c*x(1)^2 + 2*x(1)*x(2) + x(2)^2; % function
      g = [2*c*x(1) + 2*x(2)               % gradient
           2*x(1) + 2*x(2)];
 
    To optimize for a specific value of c, first assign the value to c. 
    Then create a one-argument anonymous function that captures that value 
    of c and calls myfun with two arguments. Finally, pass this anonymous 
    function to fminunc:
 
      c = 3;                              % define parameter first
      options = optimset('GradObj','on'); % indicate gradient is provided 
      x = fminunc(@(x) myfun(x,c),[1;1],options)
 
    See also optimset, fminsearch, fminbnd, fmincon, @, inline.

    Reference page in Help browser
       doc fminunc


 

 

 

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