アンサンブル

Machine Learning with PySpark

Andrew Collier

Data Scientist, Fathom Data

アンサンブルとは?

これは複数モデルの集合です。

類似モデルの集合

Crowdの知恵 — 集団の総意は単独の専門家より優れることが多い。

Machine Learning with PySpark

アンサンブルの多様性

 

 

 

多様性 と 独立性 は重要だ。最良の集合的意思決定は、合意や妥協ではなく、不一致と競合の産物である。

― James Surowiecki, The Wisdom of Crowds(邦題: 『「みんなの意見」は案外正しい』)

Machine Learning with PySpark

ランダムフォレスト

ランダムフォレスト — 決定木のアンサンブル

多様性の作り方:

  • 各木はデータの ランダムサブセット で学習
  • 各ノードの分割に ランダムサブセット の特徴量を使用

森の木は同一にならないはずです。

木の集合

Machine Learning with PySpark

木の森を作る

車データに戻ります: 米国製か (0.0) それ以外か (1.0)。

ランダムフォレスト分類器を作成。

from pyspark.ml.classification import RandomForestClassifier

forest = RandomForestClassifier(numTrees=5)

訓練データに適合。

forest = forest.fit(cars_train)
Machine Learning with PySpark

木を確認する

森の中の木へはどうアクセスする?

forest.trees
[DecisionTreeClassificationModel (uid=dtc_aa66702a4ce9) of depth 5 with 17 nodes,
 DecisionTreeClassificationModel (uid=dtc_99f7efedafe9) of depth 5 with 31 nodes,
 DecisionTreeClassificationModel (uid=dtc_9306e4a5fa1d) of depth 5 with 21 nodes,
 DecisionTreeClassificationModel (uid=dtc_d643bd48a8dd) of depth 5 with 23 nodes,
 DecisionTreeClassificationModel (uid=dtc_a2d5abd67969) of depth 5 with 27 nodes]

各木は個別予測にも使えます。

Machine Learning with PySpark

各木の予測

各木はどんな予測を出すか?

+------+------+------+------+------+-----+
|tree 0|tree 1|tree 2|tree 3|tree 4|label|
+------+------+------+------+------+-----+
|   0.0|   0.0|   0.0|   0.0|   0.0|  0.0| <- 完全一致
|   1.0|   1.0|   0.0|   1.0|   0.0|  0.0|
|   0.0|   0.0|   0.0|   1.0|   1.0|  1.0|
|   0.0|   0.0|   0.0|   1.0|   0.0|  0.0|
|   0.0|   1.0|   1.0|   1.0|   0.0|  1.0|
|   1.0|   1.0|   0.0|   1.0|   1.0|  1.0|
|   1.0|   1.0|   1.0|   1.0|   1.0|  1.0| <- 完全一致
+------+------+------+------+------+-----+
Machine Learning with PySpark

合意予測

.transform() メソッドで合意予測を生成します。

+-----+----------------------------------------+----------+
|label|probability                             |prediction|
+-----+----------------------------------------+----------+
|0.0  |[0.8,0.2]                               |0.0       |
|0.0  |[0.4,0.6]                               |1.0       |
|1.0  |[0.5333333333333333,0.4666666666666666] |0.0       |
|0.0  |[0.7177777777777778,0.28222222222222226]|0.0       |
|1.0  |[0.39396825396825397,0.606031746031746] |1.0       |
|1.0  |[0.17660818713450294,0.823391812865497] |1.0       |
|1.0  |[0.053968253968253964,0.946031746031746]|1.0       |
+-----+----------------------------------------+----------+
Machine Learning with PySpark

特徴量の重要度

モデルは次の特徴量を使用:cyl、size、mass、length、rpm、consumption。

どれが最も/最も低く重要か?

forest.featureImportances
SparseVector(6, {0: 0.0205, 1: 0.2701, 2: 0.108, 3: 0.1895, 4: 0.2939, 5: 0.1181})

見え方:

  • rpm が最も重要
  • cyl が最も低い重要度
Machine Learning with PySpark

勾配ブースティング木

反復的ブースティング手順:

  1. 決定木を作りアンサンブルに追加する。
  2. アンサンブルで各訓練インスタンスのラベルを予測する。
  3. 予測と既知ラベルを比較する。
  4. 誤予測のインスタンスに重みを置く。
  5. 1 に戻る。

各反復でモデルが改善される。

Machine Learning with PySpark

木のブースティング

勾配ブースティング木分類器を作成します。

from pyspark.ml.classification import GBTClassifier

gbt = GBTClassifier(maxIter=10)

訓練データに適合させます。

gbt = gbt.fit(cars_train)
Machine Learning with PySpark

木の比較

3種の木モデルをテストデータで比較します。

# 決定木のAUC
0.5875

# ランダムフォレストのAUC
0.65

# 勾配ブースティング木のAUC
0.65

アンサンブル手法は単一の決定木より良好です。

Machine Learning with PySpark

すべてのモデルをアンサンブル化!

Machine Learning with PySpark

Preparing Video For Download...