1
1

More than 3 years have passed since last update.

【ラビットチャレンジ】【機械学習】主成分分析

Last updated at Posted at 2020-01-06

「ラビットチャレンジ」 提出レポート

1.主成分分析とは

主成分分析(しゅせいぶんぶんせき、英: principal component analysis; PCA)とは、相関のある多数の変数から相関のない少数で全体のばらつきを最もよく表す主成分と呼ばれる変数を合成する多変量解析の一手法。データの次元を削減するために用いられる。
(引用:Wikipedia)

■ 変量の個数を減らすことに伴う、情報の損失はなるべく小さくする必要がある。

2.主成分

■ 係数ベクトルが変われば、線形変換後の値が変化する
■ 主成分を見つけるためには、線形変換後の変数の分散が最大となる射影軸を探索する。
■ 制約つき最適化問題の解き方について、ラグランジュラグランジュ関数を最大にする係数ベクトルを探索する(微分して0になる点)。
■ 射影先の分散は固有値と一致すること。
■ 元データの総分散は固有値の総計に一致すること。

3.寄与率と累積寄与率

■ 寄与率: 第 $k$ 主成分の寄与率は第 $k$ 主成分の分散の全分散に対する割合
■ 累積寄与率: 第 $1-k$ 主成分まで圧縮した際の情報損失量の割合

4.ハンズオン

【実装演習結果】

設定:
 ● 乳がん検査データを利用してロジスティクス回帰モデルを作成
 ● 主成分を利用し2次元空間上に次元圧縮
■ 必要モジュールとデータのインポート
image.png
image.png
image.png
■ ロジスティック回帰で学習
image.png

"TrainingScore&TestScore"
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
Train score: 0.988
Test score: 0.972
Confustion matrix:
[[89  1]
 [ 3 50]]
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)

【結果】ロジスティック回帰より、検証スコア97%で分類できることを確認
■ 主成分分析より次元圧縮
image.png
元データの30次元から2次元に圧縮
image.png
image.png

【考察】

主成分分析を利用して元のデータ(30次元)を2次元に圧縮することができた。圧縮後の2次元に対して、ロジスティック回帰を適用し、検証スコアを確認してみた。
テストデータにも2次元に圧縮する
image.png
image.png
ロジスティクス回帰を適用する
image.png

【考察】上の結果を見ると、次元数を2に下げても、診断結果 (良性がB / 悪性がM) の分類はできたと見えます。ロジスティック回帰(データの次元数:30)と比べて、検証スコアが減るということが分かる(97.2→91.6)。

【機械学習】レポート一覧

【ラビットチャレンジ】【機械学習】線形回帰モデル
【ラビットチャレンジ】【機械学習】非線形回帰モデル
【ラビットチャレンジ】【機械学習】ロジスティク回帰モデル
【ラビットチャレンジ】【機械学習】アルゴリズム
【ラビットチャレンジ】【機械学習】サポートベクターマシン

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1