原文:http://blog.csdn.net/abcjennifer/article/details/7716281php
本欄目(Machine learning)包括單參數的線性迴歸、多參數的線性迴歸、Octave Tutorial、Logistic Regression、Regularization、神經網絡、機器學習系統設計、SVM(Support Vector Machines 支持向量機)、聚類、降維、異常檢測、大規模機器學習等章節。全部內容均來自Standford公開課machine learning中Andrew老師的講解。(https://class.coursera.org/ml/class/index)算法
第三講-------Logistic Regression & Regularization網絡
本講內容:less
Logistic Regression機器學習
=========================函數
(一)、Classification工具
(二)、Hypothesis Representation學習
(三)、Decision Boundary優化
(四)、Cost Functionthis
(五)、Simplified Cost Function and Gradient Descent
(六)、Parameter Optimization in Matlab
(七)、Multiclass classification : One-vs-all
The problem of overfitting and how to solve it
=========================
(八)、The problem of overfitting
(九)、Cost Function
(十)、Regularized Linear Regression
(十一)、Regularized Logistic Regression
本章主要講述邏輯迴歸和Regularization解決過擬合的問題,很是很是重要,是機器學習中很是經常使用的迴歸工具,下面分別進行兩部分的講解。
第一部分:Logistic Regression
/*************(一)~(二)、Classification / Hypothesis Representation***********/
假設隨Tumor Size變化,預測病人的腫瘤是惡性(malignant)仍是良性(benign)的狀況。
給出8個數據以下:
假設進行linear regression獲得的hypothesis線性方程如上圖中粉線所示,則能夠肯定一個threshold:0.5進行predict
y=1, if h(x)>=0.5
y=0, if h(x)<0.5
即malignant=0.5的點投影下來,其右邊的點預測y=1;左邊預測y=0;則可以很好地進行分類。
那麼,若是數據集是這樣的呢?
這種狀況下,假設linear regression預測爲藍線,那麼由0.5的boundary獲得的線性方程中,不能很好地進行分類。由於不知足
y=1, h(x)>0.5
y=0, h(x)<=0.5
這時,咱們引入logistic regression model:
所謂Sigmoid function或Logistic function就是這樣一個函數g(z)見上圖所示
當z>=0時,g(z)>=0.5;當z<0時,g(z)<0.5
由下圖中公式知,給定了數據x和參數θ,y=0和y=1的機率和=1
/*****************************(三)、decision boundary**************************/
所謂Decision Boundary就是可以將全部數據點進行很好地分類的h(x)邊界。
以下圖所示,假設形如h(x)=g(θ0+θ1x1+θ2x2)的hypothesis參數θ=[-3,1,1]T, 則有
predict Y=1, if -3+x1+x2>=0
predict Y=0, if -3+x1+x2<0
恰好可以將圖中所示數據集進行很好地分類
Another Example:
answer:
除了線性boundary還有非線性decision boundaries,好比
下圖中,進行分類的decision boundary就是一個半徑爲1的圓,如圖所示:
/********************(四)~(五)Simplified cost function and gradient descent<很是重要>*******************/
該部分講述簡化的logistic regression系統中how to implement gradient descents for logistic regression.
假設咱們的數據點中y只會取0和1, 對於一個logistic regression model系統,有,那麼cost function定義以下:
因爲y只會取0,1,那麼就能夠寫成
不信的話能夠把y=0,y=1分別代入,能夠發現這個J(θ)和上面的Cost(hθ(x),y)是同樣的(*^__^*) ,那麼剩下的工做就是求能最小化 J(θ)的θ了~
在第一章中咱們已經講了如何應用Gradient Descent, 也就是下圖Repeat中的部分,將θ中全部維同時進行更新,而J(θ)的導數能夠由下面的式子求得,結果以下圖手寫所示:
如今將其帶入Repeat中:
這是咱們驚奇的發現,它和第一章中咱們獲得的公式是同樣滴~
也就是說,下圖中所示,無論h(x)的表達式是線性的仍是logistic regression model, 都能獲得以下的參數更新過程。
那麼如何用vectorization來作呢?換言之,咱們不要用for循環一個個更新θj,而用一個矩陣乘法同時更新整個θ。也就是解決下面這個問題:
上面的公式給出了參數矩陣θ的更新,那麼下面再問個問題,第二講中說了如何判斷學習率α大小是否合適,那麼在logistic regression系統中怎麼評判呢?
Q:Suppose you are running gradient descent to fit a logistic regression model with parameter θ∈Rn+1. Which of the following is a reasonable way to make sure the learning rate α is set properly and that gradient descent is running correctly?
A:
/*************(六)、Parameter Optimization in Matlab***********/
這部份內容將對logistic regression 作一些優化措施,使得可以更快地進行參數梯度降低。本段實現了matlab下用梯度方法計算最優參數的過程。
首先聲明,除了gradient descent 方法以外,咱們還有不少方法可使用,以下圖所示,左邊是另外三種方法,右邊是這三種方法共同的優缺點,無需選擇學習率α,更快,可是更復雜。
也就是matlab中已經幫咱們實現好了一些優化參數θ的方法,那麼這裏咱們須要完成的事情只是寫好cost function,並告訴系統,要用哪一個方法進行最優化參數。好比咱們用‘GradObj’, 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.
如上圖所示,給定了參數θ,咱們須要給出cost Function. 其中,
jVal 是 cost function 的表示,好比設有兩個點(1,0,5)和(0,1,5)進行迴歸,那麼就設方程爲hθ(x)=θ1x1+θ2x2;
則有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),即hθ(x)=θ1x1+θ2x2=5*x1+5*x2
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
最後獲得的結果顯示出優化參數optTheta=[5,5], functionVal = costFunction(迭代後) = 0
/*****************************(七)、Multi-class Classification One-vs-all**************************/
所謂one-vs-all method就是將binary分類的方法應用到多類分類中。
好比我想分紅K類,那麼就將其中一類做爲positive,另(k-1)合起來做爲negative,這樣進行K個h(θ)的參數優化,每次獲得的一個hθ(x)是指給定θ和x,它屬於positive的類的機率。
按照上面這種方法,給定一個輸入向量x,得到最大hθ(x)的類就是x所分到的類。
第二部分:The problem of overfitting and how to solve it
/************(八)、The problem of overfitting***********/
The Problem of overfitting:
overfitting就是過擬合,以下圖中最右邊的那幅圖。對於以上講述的兩類(logistic regression和linear regression)都有overfitting的問題,下面分別用兩幅圖進行解釋:
<Linear Regression>:
<logistic regression>:
怎樣解決過擬合問題呢?兩個方法:
1. 減小feature個數(人工定義留多少個feature、算法選取這些feature)
2. 規格化(留下全部的feature,但對於部分feature定義其parameter很是小)
下面咱們將對regularization進行詳細的講解。
對於linear regression model, 咱們的問題是最小化
寫做矩陣表示即
i.e. the loss function can be written as
there we can get:
After regularization, however,we have:
對於Regularization,方法以下,定義cost function中θ3,θ4的parameter很是大,那麼最小化cost function後就有很是小的θ3,θ4了。
寫做公式以下,在cost function中加入θ1~θn的懲罰項:
這裏要注意λ的設置,見下面這個題目:
Q:
A:λ很大會致使全部θ≈0
下面呢,咱們分linear regression 和 logistic regression分別進行regularization步驟.
/************(十)、Regularized Linear Regression***********/
<Linear regression>:
首先看一下,按照上面的cost function的公式,如何應用gradient descent進行參數更新。
對於θ0,沒有懲罰項,更新公式跟原來同樣
對於其餘θj,J(θ)對其求導後還要加上一項(λ/m)*θj,見下圖:
若是不使用梯度降低法(gradient descent+regularization),而是用矩陣計算(normal equation)來求θ,也就求使J(θ)min的θ,令J(θ)對θj求導的全部導數等於0,有公式以下:
並且已經證實,上面公式中括號內的東西是可逆的。
/************(十一)、Regularized Logistic Regression***********/
<Logistic regression>:
前面已經講過Logisitic Regression的cost function和overfitting的狀況,以下圖中所示:
和linear regression同樣,咱們給J(θ)加入關於θ的懲罰項來抑制過擬合:
用Gradient Descent的方法,令J(θ)對θj求導都等於0,獲得
這裏咱們發現,其實和線性迴歸的θ更新方法是同樣的。
When using regularized logistic regression, which of these is the best way to monitor whether gradient descent is working correctly?
和上面matlab中調用那個例子類似,咱們能夠定義logistic regression的cost function以下所示:
圖中,jval表示cost function 表達式,其中最後一項是參數θ的懲罰項;下面是對各θj求導的梯度,其中θ0沒有在懲罰項中,所以gradient不變,θ1~θn分別多了一項(λ/m)*θj;
至此,regularization能夠解決linear和logistic的overfitting regression問題了~