線形判別分析

  • クラスの分離を最適化する特徴部分空間を見つけようとする。
  • 教師ありのアルゴリズム

下記が前提条件となる

  • データが正規分布に従っていること
  • クラスの共分散行列が全く同じであること
  • 特徴量が統計的に見て互いに独立していること

手順

  1. $ d $次元のデータセットを標準化する($ d $は特徴量の個数)
  2. クラスごとに$ d $次元のベクトルを計算する
  3. クラス間変動行列$ S_B $とクラス内変動行列$ S_W $を生成する
  4. 行列$ S_W^{-1}S_B $の固有ベクトルと対応する固有値を計算する
  5. $ d \times k $次元の変換行列$ {\boldsymbol W} $を生成するために、最も大きい$ k $個の固有値に対応する$ k $個の固有ベクトルを選択する。固有ベクトルはこの行列の行
  6. 変換行列$ {\boldsymbol W} $を使ってサンプルを新しい特徴部分空間へ射影する。

In [1]:
# データの取得と前処理(標準化)
import pandas as pd
df_wine = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data', header=None)

from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler

X, y = df_wine.iloc[:, 1:].values, df_wine.iloc[:, 0].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
sc = StandardScaler()
X_train_std = sc.fit_transform(X_train)
X_test_std = sc.transform(X_test)


C:\Users\maeda\Miniconda3\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)

変動行列を計算する

  • 手順3の「クラス間変動行列$ S_B $とクラス内変動行列$ S_W $を生成する」について。
  • 上記は平均ベクトルから生成できる。
  • 各平均ベクトル$ m_i $は下記のとおり
$$ m_i = \frac{1}{n_i}\sum_{x \in D_i}x $$

In [ ]: