1. 程式人生 > >機器學習實戰(Machine Learning in Action)學習筆記————05.Logistic迴歸

機器學習實戰(Machine Learning in Action)學習筆記————05.Logistic迴歸

機器學習實戰(Machine Learning in Action)學習筆記————05.Logistic迴歸

關鍵字:Logistic迴歸、python、原始碼解析、測試
作者:米倉山下
時間:2018-10-26
機器學習實戰(Machine Learning in Action,@author: Peter Harrington)
原始碼下載地址:https://www.manning.com/books/machine-learning-in-action
[email protected]:pbharrin/machinelearninginaction.git

*************************************************************
一、Logistic迴歸

Sigmoid函式輸入為z,z=w0x0+w1x1+w2x2+…wnxn,又寫成z=WTX
Sigmoid函式為σ(z)=1/(1+exp(-z))

#Logistic迴歸分類的原理:訓練得到係數矩陣W,將位置特徵向量帶入Sigmoid,計算得到一個位於0~1之間的數,大於0.5則屬於1類,小於0.5則屬於0類。

梯度上升法:要找到某個函式的最大值,最好的方法就是沿著該函式梯度的方向探尋。如果梯度記為▽,則函式f(x,y)的梯度表示為:

梯度上升演算法到達每個點後都會重新估計移動的方向。從P0開始,計算該點的梯度,函式就根據梯度移動到下一個點P1。在P1點,梯度再次被重新計算,並沿新的梯度方向移動到P2。如此迭代,直到滿足停止條件。迭代的過程中,梯度運算元總是保證我們能夠取到最佳的移動方向。

梯度的方向就是導數最大值的方向,即函式變化率最快的方向。梯度可以通過對函式求導得到。向梯度相反方向移動保證每一次迭代都在減少下降區域性全域性最小值

用向量來表示的話,梯度演算法的迭代公式為:w:=w+α▽wf(w)公式一直迭代下去,直到某個指定值或演算法達到某個可以允許的誤差範圍。
這本書中用的是梯度上升,平時聽到比較多的是梯度下降法,其實是一樣的,只是移動的方向不同:梯度上升用來求解最大值,梯度下降用來求解最小值。接觸過深度學習就知道,梯度下降在求解引數矩陣時非常重要。

主要看兩個函式:

#Logistic函式σ(z)=1/(1+exp(-z))
def sigmoid(inX):
    return 1.0/(1+exp(-inX))

#Logistic迴歸梯度上升優化演算法
def gradAscent(dataMatIn, classLabels):
    dataMatrix = mat(dataMatIn)             #convert to NumPy matrix
    labelMat = mat(classLabels).transpose() #convert to NumPy matrix
    m,n = shape(dataMatrix)
    alpha 
= 0.001 #移動步長 maxCycles = 500 #迭代次數 weights = ones((n,1)) #初始化係數向量 for k in range(maxCycles): #heavy on matrix operations h = sigmoid(dataMatrix*weights) #matrix mult error = (labelMat - h) #
[注].vector subtraction weights = weights + alpha * dataMatrix.transpose()* error #[注].matrix mult return weights

[注]書中省略了梯度的推導過程。構造的損失函式為P(y|x;θ)=(hθ(x))^y * (1-hθ(x))^(1-y),其中h即Logistic函式σ,取其似然函式和最大似然函式,求最大似然估計,然後求導就可以得到上面的結果。參考網址************或則書*******
--------------------------------------------------------------
測試:

>>> import logRegres
>>> data,lable=logRegres.loadDataSet()
>>> w=logRegres.gradAscent(data,lable)
>>> w
matrix([[ 4.12414349],
        [ 0.48007329],
        [-0.6168482 ]])
>>>
#畫出決策邊界
>>> logRegres.plotBestFit(w.getA())
>>> 


(圖-畫出決策邊界)
--------------------------------------------------------------
方法優化1:隨機梯度上升————每次迭代僅用一個樣本點來更新迴歸係數。
對應logRegres.stocGradAscent0方法,迭代次數為資料的條數


方法優化2:改進的隨機梯度上升————每次迭代時,調整alpha大小,alpha = 4/(1.0+j+i)+0.0001
alpha隨著迭代次數增加不斷減小,但又不等於零

--------------------------------------------------------------
#分類函式,在求得引數weights後將其和測試資料inX(向量)帶入如下公式,就可以完成二類判別

def classifyVector(inX, weights):
    prob = sigmoid(sum(inX*weights))
    if prob > 0.5: return 1.0
    else: return 0.0

*************************************************************
二、示例:從疝氣病症預測馬的死亡率

處理資料中的缺失值的方法:
#使用可用特徵的均值來填補缺失值;
#使用特殊值來填補缺失值,如-1;
#忽略有缺失值的樣本;
使用相似樣本均值補缺缺失值;
使用另外的機器學習演算法預測缺失值

這個例子中用了0來補缺失值,資料包含28個特徵和1列標籤(分類兩類),horseColicTraining.txt為訓練資料,horseColicTest.txt為測試資料。
使用改進的隨機梯度上升stocGradAscent1演算法,對資料進行測試

>>> logRegres.colicTest()#colicTest()為迴圈訓練1000次再進行測試的效果
logRegres.py:18: RuntimeWarning: overflow encountered in exp
  return 1.0/(1+exp(-inX))
the error rate of this test is: 0.373134
0.373134328358209
>>>
>>> logRegres.multiTest()#colicTest()執行10次的平均錯誤率
the error rate of this test is: 0.343284
the error rate of this test is: 0.358209
the error rate of this test is: 0.343284
the error rate of this test is: 0.343284
the error rate of this test is: 0.268657
the error rate of this test is: 0.253731
the error rate of this test is: 0.343284
the error rate of this test is: 0.268657
the error rate of this test is: 0.447761
the error rate of this test is: 0.283582
after 10 iterations the average error rate is: 0.325373
>>>

其他程式碼:

sigmoidPlot.py  #s = 1/(1 + exp(-t))函式在[-5,5]和[-60,60]上的形態對比
plotSDerror.py  #stocGradAscent1演算法,在迭代過程中,三個引數的變化趨勢
plotGD.py       #梯度下降示意圖
plot2D.py       #stocGradAscent0進行梯度下降,決策邊界