本內容將介紹機器學習中的 Logistic 迴歸 及 Python 代碼實現,和 Softmax 迴歸 。
Logistic 迴歸 (logistic regression,也稱邏輯迴歸和對數機率迴歸)是一種經典的分類模型,屬於廣義的線性迴歸分析模型。雖然名稱中包含了“迴歸”,但是實際上它不是迴歸模型,而是分類模型。
一、Logistic 迴歸
在閱讀本內容前,需要了解 線性迴歸模型 的基本概念。如果您還不瞭解,可以參閱 機器學習系列:線性迴歸模型 。
在 機器學習系列:線性迴歸模型 中介紹瞭如何使用線性模型進行迴歸預測。但是否可以進行分類預測呢?
考慮二分類任務,其輸出標記 y ∈ { 0 , 1 } y \in \{0,1\} y ∈ { 0 , 1 } ,線性迴歸模型產生的預測值 z = w T x z = \mathbf{w}^{T} \mathbf{x} z = w T x 是實值,於是我們需要將實值 z z z 轉換爲 { 0 , 1 } \{0,1\} { 0 , 1 } 值。最理想的是“單位階躍函數”(unit-step function)
(1) y = { 0 , z < 0 0.5 , z = 0 1 , z > 0
y = \left \{
\begin{array}{cc}
0,\quad z < 0 \\
0.5, \quad z = 0\\
1, \quad z > 0
\end{array}
\right.
\tag{1}
y = ⎩ ⎨ ⎧ 0 , z < 0 0 . 5 , z = 0 1 , z > 0 ( 1 )
即當 z > 0 z>0 z > 0 時輸出 1 1 1 (即正例),當 z < 0 z<0 z < 0 是輸出 0 0 0 (即反例),當 z = 0 z=0 z = 0 時可輸出 0 0 0 或 1 1 1 ,如圖-1所示。
圖-1 單位階躍函數與對數機率函數
但從圖-1 可看出,單位階躍函數不連續。可以使用對數機率函數(logistic function)對其進行替換:
(2) g ( z ) = 1 1 + e − z
g(z) = \frac{1}{1+e^{-z}} \tag{2}
g ( z ) = 1 + e − z 1 ( 2 )
從圖-1 可看出,對數機率函數是一種“sigmoid 函數”,它將 z z z 值轉化爲一個接近 0 0 0 或 1 1 1 的 y y y 值,並且其輸出值在 z = 0 z=0 z = 0 附近變化很快。
我們知道線性迴歸模型爲
(3) f w ( x ) = w 0 x 0 + w 1 x 1 + ⋯ + w n x n = ∑ j = 0 n w j x j = w T x
f_{w}(x) = w_0x_0 + w_1x_1 + \cdots + w_nx_n = \sum_{j=0}^{n}w_jx_j = \mathbf w^{T} \mathbf{x} \tag{3}
f w ( x ) = w 0 x 0 + w 1 x 1 + ⋯ + w n x n = j = 0 ∑ n w j x j = w T x ( 3 )
其中 w = ( w 0 ; w 1 ; ⋯  ; w n ) \mathbf{w}=(w_0;w_1;\cdots;w_n) w = ( w 0 ; w 1 ; ⋯ ; w n ) ,x = ( x 0 ; x 1 ; ⋯  ; x n ) \mathbf{x}=(x_0;x_1;\cdots;x_n) x = ( x 0 ; x 1 ; ⋯ ; x n ) ,x 0 = 1 x_0=1 x 0 = 1 。將其代入式(2),得到
(4) f w ( x ) = 1 1 + e − w T x
f_{\mathbf{w}} (\mathbf{x}) = \frac{1}{1 + e^{-\mathbf w^{T} \mathbf{x}}} \tag{4}
f w ( x ) = 1 + e − w T x 1 ( 4 )
二、損失函數
2.1 損失函數
對於二分類問題,單個樣本的損失函數爲
(5) C o s t ( f w ( x ( i ) , y ( i ) ) ) = { − l o g ( f w ( x ) ) , y = 1 − l o g ( 1 − f w ( x ) ) , y = 0
Cost\left( f_{\bf w}(\mathbf{x}^{(i)},y^{(i)}) \right) = \left \{\begin{array}{cc} -log(f_{\bf w}(\mathbf x)),\quad y=1 \\ -log(1-f_{\bf w}(\mathbf{x})), \quad y=0 \end{array} \right. \tag{5}
C o s t ( f w ( x ( i ) , y ( i ) ) ) = { − l o g ( f w ( x ) ) , y = 1 − l o g ( 1 − f w ( x ) ) , y = 0 ( 5 )
等價於
(6) C o s t ( f w ( x ( i ) , y ( i ) ) ) = − y l o g ( f w ( x ) ) − ( 1 − y ) l o g ( 1 − f w ( x ) )
Cost\left( f_{\bf w}(\mathbf x^{(i)},y^{(i)}) \right) = -ylog(f_{\bf w}(\mathbf x))-(1-y)log(1-f_{\bf w}(\mathbf x)) \tag{6}
C o s t ( f w ( x ( i ) , y ( i ) ) ) = − y l o g ( f w ( x ) ) − ( 1 − y ) l o g ( 1 − f w ( x ) ) ( 6 )
其也稱作爲交叉熵代價函數 。
對於訓練集所有樣本,其損失函數爲
(7) E ( w ) = 1 m ∑ i = 1 m C o s t ( f w ( x ( i ) ) , y ( i ) )
E({\bf w}) = \frac{1}{m} \sum_{i=1}^{m} Cost\left(f_{\bf w}(\mathbf x^{(i)}),y^{(i)}\right) \tag{7}
E ( w ) = m 1 i = 1 ∑ m C o s t ( f w ( x ( i ) ) , y ( i ) ) ( 7 )
將式(6)代入式(7)得
(8) E ( w ) = − 1 m ∑ i = 1 m ( y ( i ) l o g f w ( x ( i ) ) + ( 1 − y ( i ) ) l o g ( 1 − f w ( x ( i ) ) ) )
E({\bf w}) = -\frac{1}{m} \sum_{i=1}^{m} \bigg(y^{(i)}logf_{\bf w}(\mathbf x^{(i)}) + (1-y^{(i)})log\Big(1-f_{\bf w}(\mathbf x^{(i)})\Big)\bigg) \tag{8}
E ( w ) = − m 1 i = 1 ∑ m ( y ( i ) l o g f w ( x ( i ) ) + ( 1 − y ( i ) ) l o g ( 1 − f w ( x ( i ) ) ) ) ( 8 )
然後可以採用 梯度下降法 (Gradient descent method) 或者 牛頓法 (Newton method)求使 E ( w ) E({\bf w}) E ( w ) 取最小值的 w \mathbf{w} w 。
實際上,上面的 C o s t ( f w ( x ( i ) , y ( i ) ) ) Cost\left( f_{\bf w}(\mathbf x^{(i)},y^{(i)}) \right) C o s t ( f w ( x ( i ) , y ( i ) ) ) 和 E ( w ) E({\bf w}) E ( w ) 是基於最大似然估計推導出來的,下面我們來了解一下具體過程。
2.2 最大似然估計
f w ( x ) f_{\mathbf{w}} (\mathbf{x}) f w ( x ) 函數的值表示樣本輸出爲 1 的概率,則樣本輸出爲 1 和 0 的概率分別爲
(9) P ( y = 1 ∣ x ; w ) = f w ( x )
P(y=1|\mathbf{x};\mathbf{w}) = f_{\mathbf{w}} (\mathbf{x}) \tag{9}
P ( y = 1 ∣ x ; w ) = f w ( x ) ( 9 )
(10) P ( y = 0 ∣ x ; w ) = 1 − f w ( x )
P(y=0|\mathbf{x};\mathbf{w}) = 1- f_{\mathbf{w}} (\mathbf{x}) \tag{10}
P ( y = 0 ∣ x ; w ) = 1 − f w ( x ) ( 1 0 )
將式(9)和式(10)可簡化爲
(11) P ( y ∣ x ; w ) = ( f w ( x ) ) y ( 1 − f w ( x ) ) ( 1 − y )
P(y|\mathbf{x};\mathbf{w}) = (f_{\mathbf{w}}(\mathbf{x}))^{y} (1-f_{\mathbf{w}}(\mathbf{x}))^{(1-y)} \tag{11}
P ( y ∣ x ; w ) = ( f w ( x ) ) y ( 1 − f w ( x ) ) ( 1 − y ) ( 1 1 )
則對應的似然函數爲
(12) L ( w ) = ∏ i = 1 m P ( y ( i ) ∣ x ( i ) ; w )
L(\mathbf{w}) = \prod_{i=1}^{m} P\left(y^{(i)}|\mathbf{x}^{(i)};\mathbf{w}\right) \tag{12}
L ( w ) = i = 1 ∏ m P ( y ( i ) ∣ x ( i ) ; w ) ( 1 2 )
(13) = ∏ i = 1 m ( f w ( x ( i ) ) ) y ( i ) ( 1 − f w ( x ( i ) ) ) ( 1 − y ( i ) )
= \prod_{i=1}^{m} \left(f_{\mathbf{w}}(\mathbf{x}^{(i)})\right)^{y^{(i)}} \left(1-f_{\mathbf{w}}(\mathbf{x}^{(i)})\right)^{(1-y^{(i)})} \tag{13}
= i = 1 ∏ m ( f w ( x ( i ) ) ) y ( i ) ( 1 − f w ( x ( i ) ) ) ( 1 − y ( i ) ) ( 1 3 )
則對數似然函數爲
(14) l ( w ) = l o g L ( w ) = ∑ i = 1 m ( y ( i ) l o g f w ( x ( i ) ) + ( 1 − y ( i ) ) l o g ( 1 − f w ( x ( i ) ) ) )
l(\mathbf{w}) = logL(\mathbf{w}) = \sum_{i=1}^{m} \bigg( y^{(i)}logf_{\mathbf{w}}(\mathbf{x}^{(i)})+\left(1-y^{(i)}\right)log\Big(1-f_{\mathbf{w}}(\mathbf{x}^{(i)})\Big)\bigg) \tag{14}
l ( w ) = l o g L ( w ) = i = 1 ∑ m ( y ( i ) l o g f w ( x ( i ) ) + ( 1 − y ( i ) ) l o g ( 1 − f w ( x ( i ) ) ) ) ( 1 4 )
最大似然估計就是要求得使 l ( w ) l(\mathbf{w}) l ( w ) 取最大值時的 w \mathbf{w} w ,我們可以使用 梯度上升法 求得最優的 w \mathbf{w} w 。
對比式(8)和式(14),我們發現存在以下關係
(15) E ( w ) = − 1 m l ( w )
E(\mathbf{w}) = -\frac{1}{m}l(\mathbf{w}) \tag{15}
E ( w ) = − m 1 l ( w ) ( 1 5 )
在求解 w \mathbf{w} w 時,求解 l ( w ) l(\mathbf{w}) l ( w ) 的最大值或求解 E ( w ) E(\mathbf{w}) E ( w ) 的最小值,兩者實際上是一致的。
三、根據梯度下降法求解最優 w \mathbf{w} w
如果你還不瞭解梯度下降法,可以參閱 機器學習系列:梯度下降法及 Python 實現 。
對 E ( w ) E(w) E ( w ) 中的每個 w j w_j w j 求偏導數(即梯度),得到(注意:這裏的 l o g log l o g 的底爲 e e e )
(16) ∇ E ( w j ) = ∂ E ( w ) ∂ w j = 1 m ∑ i = 1 m ( f w ( x ( i ) ) − y ( i ) ) ) x j ( i )
\nabla E(w_j) = \frac{\partial E(w)}{\partial w_j} = \frac{1}{m} \sum_{i=1}^{m} \left(f_{\mathbf{w}}(\mathbf{x}^{(i)}) - y^{(i)}) \right) x_j^{(i)} \tag{16}
∇ E ( w j ) = ∂ w j ∂ E ( w ) = m 1 i = 1 ∑ m ( f w ( x ( i ) ) − y ( i ) ) ) x j ( i ) ( 1 6 )
∇ E ( w j ) \nabla E(w_j) ∇ E ( w j ) 的具體求解過程,在下面的 3.1 求解 ∇ E ( w j ) \nabla E(w_j) ∇ E ( w j ) 的具體過程 會進行介紹。
則 w j w_j w j 的迭代更新過程爲
(17) w j ^ = w j − η 1 m ∑ i = 1 m ( f w ( x ( i ) ) − y ( i ) ) ) x j ( i ) , j = 0 , 1 , ⋯  , n
\hat{w_{j}} = w_{j} - \eta \frac{1}{m} \sum_{i=1}^{m} \left(f_{\mathbf{w}}(\mathbf{x}^{(i)}) - y^{(i)}) \right) x_j^{(i)},\quad j=0,1,\cdots,n \tag{17}
w j ^ = w j − η m 1 i = 1 ∑ m ( f w ( x ( i ) ) − y ( i ) ) ) x j ( i ) , j = 0 , 1 , ⋯ , n ( 1 7 )
其中 η \eta η 爲學習速率。
3.1 ∇ E ( w j ) \nabla E(w_j) ∇ E ( w j ) 的具體求解過程
如果你對 ∇ E ( w j ) \nabla E(w_j) ∇ E ( w j ) 的具體求解過程很瞭解或者不感興趣,可以直接跳過這一部分。
因爲 ∇ E ( w j ) \nabla E(w_j) ∇ E ( w j ) 的求解需要用到 ∂ ∂ w j f w ( x ) \frac{\partial}{\partial w_j} f_{\mathbf{w}}(\mathbf{x}) ∂ w j ∂ f w ( x ) 。先來看一下 ∂ ∂ w j f w ( x ) \frac{\partial}{\partial w_j} f_{\mathbf{w}}(\mathbf{x}) ∂ w j ∂ f w ( x ) 的求解過程:
∂ ∂ w j f w ( x ) = ∂ ∂ w j ( 1 1 + e − w T x )
\frac{\partial}{\partial w_j} f_{\mathbf{w}}(\mathbf{x}) = \frac{\partial}{\partial w_j} \left(\frac{1}{1 + e^{-\mathbf w^{T} \mathbf{x}}}\right)
∂ w j ∂ f w ( x ) = ∂ w j ∂ ( 1 + e − w T x 1 )
= − 1 ( 1 + e − w T x ) 2 ∂ ∂ w j ( 1 + e − w T x )
= -\frac{1}{(1 + e^{-\mathbf{w}^T \mathbf{x}})^{2}} \frac{\partial }{\partial w_j} (1 + e^{-\mathbf{w}^T \mathbf{x}})
= − ( 1 + e − w T x ) 2 1 ∂ w j ∂ ( 1 + e − w T x )
= − 1 ( 1 + e − w T x ) 2 ∂ ∂ w j ( e − w T x )
= -\frac{1}{(1 + e^{-\mathbf{w}^T \mathbf{x}})^{2}} \frac{\partial }{\partial w_j} (e^{-\mathbf{w}^T \mathbf{x}})
= − ( 1 + e − w T x ) 2 1 ∂ w j ∂ ( e − w T x )
= − e − w T x ( 1 + e − w T x ) 2 ∂ ∂ w j ( − w T x )
= -\frac{e^{-\mathbf{w}^T \mathbf{x}}}{(1 + e^{-\mathbf{w}^T \mathbf{x}})^{2}} \frac{\partial }{\partial w_j} (-\mathbf{w}^T \mathbf{x})
= − ( 1 + e − w T x ) 2 e − w T x ∂ w j ∂ ( − w T x )
= e − w T x ( 1 + e − w T x ) 2 ∂ ∂ w j ( w 0 x 0 + w 1 x 1 + ⋯ + w n x n )
= \frac{e^{-\mathbf{w}^T \mathbf{x}}}{(1 + e^{-\mathbf{w}^T \mathbf{x}})^{2}} \frac{\partial }{\partial w_j} (w_0x_0 + w_1x_1 + \cdots + w_nx_n)
= ( 1 + e − w T x ) 2 e − w T x ∂ w j ∂ ( w 0 x 0 + w 1 x 1 + ⋯ + w n x n )
= e − w T x ( 1 + e − w T x ) 2 x j
= \frac{e^{-\mathbf{w}^T \mathbf{x}}}{(1 + e^{-\mathbf{w}^T \mathbf{x}})^{2}} x_j
= ( 1 + e − w T x ) 2 e − w T x x j
= 1 ( 1 + e − w T x ) e − w T x ( 1 + e − w T x ) x j
= \frac{1}{(1 + e^{-\mathbf{w}^T \mathbf{x}})} \frac{e^{-\mathbf{w}^T \mathbf{x}}}{(1 + e^{-\mathbf{w}^T \mathbf{x}})} x_j
= ( 1 + e − w T x ) 1 ( 1 + e − w T x ) e − w T x x j
= f w ( x ) ( 1 − f w ( x ) ) x j
= f_{\mathbf{w}}(\mathbf{x}) (1- f_{\mathbf{w}}(\mathbf{x})) x_j
= f w ( x ) ( 1 − f w ( x ) ) x j
∇ E ( w j ) \nabla E(w_j) ∇ E ( w j ) 的求解過程:
∇ E ( w j ) = ∂ E ( w ) ∂ w j
\nabla E(w_j) = \frac{\partial E(w)}{\partial w_j}
∇ E ( w j ) = ∂ w j ∂ E ( w )
= ∂ ∂ w j ( − 1 m ∑ i = 1 m ( y ( i ) l o g f w ( x ( i ) ) + ( 1 − y ( i ) ) l o g ( 1 − f w ( x ( i ) ) ) ) )
= \frac{\partial}{\partial w_j} \Bigg(-\frac{1}{m} \sum_{i=1}^{m} \bigg(y^{(i)}logf_{\bf w}(\mathbf x^{(i)}) + (1-y^{(i)})log\Big(1-f_{\bf w}(\mathbf x^{(i)})\Big)\bigg)\Bigg)
= ∂ w j ∂ ( − m 1 i = 1 ∑ m ( y ( i ) l o g f w ( x ( i ) ) + ( 1 − y ( i ) ) l o g ( 1 − f w ( x ( i ) ) ) ) )
= − 1 m ∑ i = 1 m ( y ( i ) ∂ ∂ w j l o g f w ( x ( i ) ) + ( 1 − y ( i ) ) ∂ ∂ w j l o g ( 1 − f w ( x ( i ) ) ) )
= -\frac{1}{m} \sum_{i=1}^{m} \left(y^{(i)}\frac{\partial}{\partial w_j} logf_{\bf w}(\mathbf x^{(i)}) + \left(1-y^{(i)}\right) \frac{\partial}{\partial w_j} log\left(1-f_{\bf w}(\mathbf x^{(i)})\right)\right)
= − m 1 i = 1 ∑ m ( y ( i ) ∂ w j ∂ l o g f w ( x ( i ) ) + ( 1 − y ( i ) ) ∂ w j ∂ l o g ( 1 − f w ( x ( i ) ) ) )
= − 1 m ∑ i = 1 m ( y ( i ) 1 f w ( x ( i ) ) − 1 − y ( i ) 1 − f w ( x ( i ) ) ) ∂ ∂ w j f w ( x ( i ) )
= -\frac{1}{m} \sum_{i=1}^{m} \left( y^{(i)}\frac{1}{f_{\mathbf{w}}(\mathbf{x}^{(i)})} - \frac{1-y^{(i)}}{1-f_{\bf w}(\mathbf x^{(i)})} \right)\frac{\partial}{\partial w_j}f_{\mathbf{w}}(\mathbf{x}^{(i)})
= − m 1 i = 1 ∑ m ( y ( i ) f w ( x ( i ) ) 1 − 1 − f w ( x ( i ) ) 1 − y ( i ) ) ∂ w j ∂ f w ( x ( i ) )
= − 1 m ∑ i = 1 m ( y ( i ) ( 1 − f w ( x ( i ) ) ) − ( 1 − y ( i ) ) f w ( x ( i ) ) ) x j ( i )
= -\frac{1}{m} \sum_{i=1}^{m} \left( y^{(i)} \left(1-f_{\bf w}(\mathbf x^{(i)})\right) - \left(1-y^{(i)}\right)f_{\mathbf{w}}(\mathbf{x}^{(i)}) \right) x_j^{(i)}
= − m 1 i = 1 ∑ m ( y ( i ) ( 1 − f w ( x ( i ) ) ) − ( 1 − y ( i ) ) f w ( x ( i ) ) ) x j ( i )
= 1 m ∑ i = 1 m ( f w ( x ( i ) ) − y ( i ) ) ) x j ( i )
= \frac{1}{m} \sum_{i=1}^{m} \left(f_{\mathbf{w}}(\mathbf{x}^{(i)}) - y^{(i)}) \right) x_j^{(i)}
= m 1 i = 1 ∑ m ( f w ( x ( i ) ) − y ( i ) ) ) x j ( i )
四、Python 代碼實現
下面使用批量梯度下降法擬合一個 Logistic 迴歸模型。代碼如下(Python 3.x):
import numpy as np
import matplotlib. pyplot as plt
class LogisticRegression :
def __init__ ( self) :
self. weights = None
pass
def __str__ ( self) :
return 'weights: {}' . format ( self. weights)
def _sigmoid ( self, inx) :
"""
計算公式:1/(1 + exp(-inx))
"""
return 1.0 / ( 1 + np. exp( - inx) )
def train ( self, input_data, label_data, learning_rate, iteration) :
"""
進行模型訓練
:param input_data: 訓練數據,特徵值
:param label_data: 訓練數據,標籤值
:param learning_rate: 熟悉速率
:param iteration: 迭代次數
"""
input_data_mat = np. mat( input_data)
label_data_mat = np. mat( label_data) . transpose( )
m, n = np. shape( input_data_mat)
self. weights = np. ones( ( n, 1 ) )
for i in range ( iteration) :
h = self. _sigmoid( input_data_mat * self. weights)
error = h - label_data_mat
self. weights -= ( learning_rate * input_data_mat. transpose( ) * error)
def get_weights ( self) :
return self. weights
def load_data_set ( file_name) :
"""
從文件中獲取數據集
:param file_name: 文件名
:return: 返回從文件中獲取的數據集
input_data 存儲特徵值,label_data 存儲標籤值
"""
input_data, label_data = [ ] , [ ]
fr = open ( file_name)
for line in fr. readlines( ) :
cur_line = line. strip( ) . split( )
input_data. append( [ 1.0 , float ( cur_line[ 0 ] ) , float ( cur_line[ 1 ] ) ] )
label_data. append( int ( cur_line[ 2 ] ) )
return input_data, label_data
def plot_best_fit ( input_data, label_data, weights) :
input_data_arr = np. array( input_data)
x_cord_01, y_cord_01, x_cord_02, y_cord_02 = [ ] , [ ] , [ ] , [ ]
for i in range ( len ( input_data) ) :
if label_data[ i] == 1 :
x_cord_01. append( input_data_arr[ i] [ 1 ] )
y_cord_01. append( input_data_arr[ i] [ 2 ] )
else :
x_cord_02. append( input_data_arr[ i] [ 1 ] )
y_cord_02. append( input_data_arr[ i] [ 2 ] )
fig = plt. figure( )
ax = fig. add_subplot( 111 )
ax. scatter( x_cord_01, y_cord_01, s= 30 , c= 'red' , marker= 's' )
ax. scatter( x_cord_02, y_cord_02, s= 30 , c= 'green' )
x = np. arange( - 3.0 , 3.0 , 0.1 )
y = ( - weights[ 0 ] - weights[ 1 ] * x) / weights[ 2 ]
ax. plot( x, y)
plt. xlabel( 'X1' )
plt. ylabel( 'X2' )
plt. show( )
def test_logistic_regression ( ) :
input_data, label_data = load_data_set( 'testSet.txt' )
logistic_regression = LogisticRegression( )
logistic_regression. train( input_data, label_data, 0.001 , 500 )
print ( logistic_regression)
plot_best_fit( input_data, label_data, logistic_regression. get_weights( ) )
if __name__ == "__main__" :
test_logistic_regression( )
運行以上代碼,將打印如下信息及繪製如下圖形:
weights: [ [ 4.12414349 ]
[ 0.48007329 ]
[ - 0.6168482 ] ]
五、多分類
上面主要介紹了 Logistic 迴歸用於解決二分類問題。實際上,可以對 Logistic 迴歸進行擴展,用於解決多分類問題。下面將介紹兩種方法。
5.1 多個 Logistic 迴歸
將多分類任務拆分爲若干個二分類任務進行求解。具體來說,先對問題進行拆分,然後爲拆出的每個二分類任務訓練一個分類器;在預測時,對這些分類器的預測結果進行集成以獲得最終的多分類結果。最常用的拆分策略爲:“一對一”(One vs One)、“一對其餘”(One vs Rest)和“多對多”(Many vs Many)。
假設數據集有 k k k 個類別,“一對一”將爲任意兩個類別訓練一個分類器,將存在 k ( k − 1 ) k(k-1) k ( k − 1 ) 個分類器。在預測時,將得到 k ( k − 1 ) k(k-1) k ( k − 1 ) 個分類結果,然後通過投票得到最終的分類結果(即預測最多的類別作爲最終的結果)。
“一對其餘”將一個類別作爲正例,其他類別作爲反例,將存在 k k k 個分類器。在預測時,根據這 k k k 個分類器可以得到每個類別的概率,最後我們選擇概率值最大的類別作爲最終的分類結果。
5.2 多項 Logistic 迴歸
多項 Logistic 迴歸 也稱爲 Softmax 迴歸 。
假設離散型隨機變量 y y y 的取值集合是 { 1 , 2 , ⋯  , k } \{1,2,\cdots,k\} { 1 , 2 , ⋯ , k } ,那麼樣本輸出爲 l l l 概率爲
(18) P ( y = l ∣ x ) = e w l x ∑ l = 1 k e w l x , l = 1 , 2 , ⋯  , k
P(y=l|x) = \frac{e^{\mathbf{w}_l \mathbf{x}}}{\sum_{l=1}^{k} e^{\mathbf{w}_l\mathbf{x}}}, \quad l=1,2,\cdots,k \tag{18}
P ( y = l ∣ x ) = ∑ l = 1 k e w l x e w l x , l = 1 , 2 , ⋯ , k ( 1 8 )
參照二分類,可知損失函數爲
(19) E ( w ) = − 1 m ∑ i = 1 m ∑ j = 1 k 1 { y ( i ) = j } l o g e w l x ( i ) ∑ l = 1 k e w l x ( i )
E(\mathbf{w}) = -\frac{1}{m} \sum_{i=1}^{m} \sum_{j=1}^{k} 1\{y^{(i)}=j\}log\frac{e^{\mathbf{w}_l \mathbf{x}^{(i)}}}{\sum_{l=1}^{k} e^{\mathbf{w}_l\mathbf{x}^{(i)}}} \tag{19}
E ( w ) = − m 1 i = 1 ∑ m j = 1 ∑ k 1 { y ( i ) = j } l o g ∑ l = 1 k e w l x ( i ) e w l x ( i ) ( 1 9 )
其中,m m m 表示樣本個數,k k k 表示類別的個數;1 { y ( i ) = j } 1\{y^{(i)} = j\} 1 { y ( i ) = j } 函數表示:當 y ( i ) = j y^{(i)} = j y ( i ) = j 時,函數值爲 1,否則爲 0。
然後參照上面的梯度下降法求出各個最優 w l \mathbf{w}_{l} w l 。這裏就不再詳細介紹求解過程了。
5.3 選擇原則
解決多分類問題時,選擇上面介紹的兩種方法的具體原則:
參考:
[1] 周志華《機器學習》
[2] 李航《統計學習方法》
[3] 《機器學習實戰》
[4] https://www.cnblogs.com/alfred2017/p/6627824.html
[5] https://blog.csdn.net/u011734144/article/details/79717470