5-6 Quadratic Classifiers (二次分類器)

[english][all]

(請注意:中文版本並未隨英文版本同步更新!)

Slides

如果我們將每一筆資料視為在高維空間中的一點,而且這些同一類別的資料點是由一個高維高斯機率密度函數所產生,那麼我們就可以使用 MLE 的方法來求出這個高斯密度函數的最佳參數值。使用這種方法所求得的分類器,稱為「二次分類器」(Quadratic classifier),因為此方法所產生的決策分界線(Decision Boundaries)都是輸入特徵的二次函數。其做法可以簡述如下:

  1. 假設每一個類別的資料均是由 d 維的高斯機率密度函數(Gaussian probability density function)所產生::
    gi(x, m, S) = (2p)-d/2*det(S)-0.5*exp[-(x-m)TS-1(x-m)/2]
    其中 m 是此高斯機率密度函數的平均向量(Mean vector),S 則是其共變異矩陣(Covariance matrix),我們可以根據 MLE,產生最佳的平均向量 m 和共變異矩陣 S
  2. 若有需要,可以對每一個高斯機率密度函數乘上一個權重 wi
  3. 在實際進行分類時,wi*gi(x, m, S) 越大,則資料 x 隸屬於類別 i 的可能性就越高。

在實際進行運算時,我們通常不去計算 wi*gi(x, m, S) ,而是計算 log(wi*gi(x, m, S)) = log(wi) + log(gi(x, m, S)),以便避開計算指數時可能發生的種種問題(如精確度不足、計算耗時),log(gi(x, m, S)) 的公式如下: $$ \ln \left( p(c_i)g(\mathbf{x}|\mathbf{\mu}, \Sigma) \right) = \ln p(c_i) - \frac{d \ln(2\pi)+ \ln |\Sigma|}{2} - \frac{(\mathbf{x}-\mathbf{\mu})^T\Sigma^{-1}(\mathbf{x}-\mathbf{\mu})}{2} $$ The decision boundary between class i and j is represented by the following trajectory: $$ p(c_i)g(\mathbf{x}|\mathbf{\mu}_i, \Sigma_i) = p(c_j)g(\mathbf{x}|\mathbf{\mu}_j, \Sigma_j) $$ Taking the logrithm of both sides, we have $$ \ln p(c_i) - \frac{d \ln(2\pi)+ \ln |\Sigma_i|}{2} - \frac{(\mathbf{x}-\mathbf{\mu}_i)^T\Sigma_i^{-1}(\mathbf{x}-\mathbf{\mu}_i)}{2} = \ln p(c_j) - \frac{d \ln(2\pi)+ \ln |\Sigma_j|}{2} - \frac{(\mathbf{x}-\mathbf{\mu}_j)^T\Sigma_j^{-1}(\mathbf{x}-\mathbf{\mu}_j)}{2} $$ After simplification, we have the decision boundary as the following equation: $$ 2\ln p(c_i) + \ln |\Sigma_i| - (\mathbf{x}-\mathbf{\mu}_i)^T\Sigma_i^{-1}(\mathbf{x}-\mathbf{\mu}_i) = 2\ln p(c_j) + \ln |\Sigma_j| - (\mathbf{x}-\mathbf{\mu}_j)^T\Sigma_j^{-1}(\mathbf{x}-\mathbf{\mu}_j) $$ $$ (\mathbf{x}-\mathbf{\mu}_i)^T\Sigma_i^{-1}(\mathbf{x}-\mathbf{\mu}_i) - (\mathbf{x}-\mathbf{\mu}_j)^T\Sigma_j^{-1}(\mathbf{x}-\mathbf{\mu}_j) = \ln \left( \frac{p^2(c_i)|\Sigma_i|}{p^2(c_j)|\Sigma_j|} \right) $$ where the right-hand side is a constant. Since both $(\mathbf{x}-\mathbf{\mu}_i)^T\Sigma_i^{-1}(\mathbf{x}-\mathbf{\mu}_i)$ and $(\mathbf{x}-\mathbf{\mu}_j)^T\Sigma_j^{-1}(\mathbf{x}-\mathbf{\mu}_j)$ are quadratic, the above equation represents a decision boundary of the quadratic form in the d-dimensional feature space.

In particular, if $\Sigma_i=\Sigma_j=\Sigma$, the decision boundary is reduced to a linear equation, as follows: $$ \underbrace{(\mathbf{\mu}_i-\mathbf{\mu}_j) \Sigma}_{\mathbf{c}} \mathbf{x} = \underbrace{\mathbf{\mu}_i \Sigma \mathbf{\mu}_i - \mathbf{\mu}_j \Sigma \mathbf{\mu}_j - \ln \left( \frac{p^2(c_i)|\Sigma_i|}{p^2(c_j)|\Sigma_j|} \right)}_{constant} \Longrightarrow \mathbf{cx}=constant $$

在以下範例,我們將使用二次分類器來對 IRIS 資料的第三維及第四維進行分類。首先,我們先畫出資料的分佈圖:

Example 1: qcDataPlot01.mDS = prData('iris'); DS.input=DS.input(3:4, :); % Only take dimensions 3 and 4 for 2d visualization dsScatterPlot(DS); % Scatter plot of the dataset

接著,我們可以使用 qcTrain 來建造一個 QC:

Example 2: qcTrain01.mDS = prData('iris'); DS.input=DS.input(3:4, :); % Only take dimensions 3 and 4 for 2d visualization [qcPrm, logProb, recogRate, hitIndex]=qcTrain(DS); fprintf('Recog. rate = %f%%\n', recogRate*100);Recog. rate = 98.000000%

由上例可知,若只使用第三維和第四維特徵,QC 的辨識率是 98%,此類測試稱為內部測試。

上圖秀出資料點,以及分類錯誤的點(叉叉)。特別需要注意的是,在上述的程式碼中,我們用到 classWeight,這是一個向量,用來指定每一個類別的權重,通常有兩種做法:

我們也可以將每個類別的高斯密度函數以三維曲面呈現,並畫出其等高線,請見下列範例:

Example 3: qcPlot01.mDS=prData('iris'); DS.input=DS.input(3:4, :); [qcPrm, logProb, recogRate, hitIndex]=qcTrain(DS); qcPlot(DS, qcPrm, '2dPdf');

根據這些高斯密度函數,我們就可以畫出每個類別的邊界,如下:

Example 4: qcPlot02.mDS=prData('iris'); DS.input=DS.input(3:4, :); [qcPrm, logProb, recogRate, hitIndex]=qcTrain(DS); DS.hitIndex=hitIndex; % Attach hitIndex to DS for plotting qcPlot(DS, qcPrm, 'decBoundary');

事實上,如之前之推導,這些邊界都是二次函數,可由上述圖形驗證之。

如果每個類別的資料量相差太大,二次分類器可能得到看似錯誤的結果,但實際上卻是對的。這種情況我們留待作業來討論。

我們也可以根據 false positive 和 false negative 所帶來的 cost 來進行權重的設定,後續詳述。

另一方面,如果訓練資料比較複雜,無法以簡單的高斯機率密度函數來進行滿意的分類,此時我們就可以選用類似但較複雜的方法,例如 Gaussian Mixture Models,簡稱 GMM,詳見後述。


Data Clustering and Pattern Recognition (資料分群與樣式辨認)