Machine Learning with PySpark
Andrew Collier
Data Scientist, Fathom Data
これは複数モデルの集合です。
Crowdの知恵 — 集団の総意は単独の専門家より優れることが多い。
多様性 と 独立性 は重要だ。最良の集合的意思決定は、合意や妥協ではなく、不一致と競合の産物である。
― James Surowiecki, The Wisdom of Crowds(邦題: 『「みんなの意見」は案外正しい』)
ランダムフォレスト — 決定木のアンサンブル
多様性の作り方:
森の木は同一にならないはずです。

車データに戻ります: 米国製か (0.0) それ以外か (1.0)。
ランダムフォレスト分類器を作成。
from pyspark.ml.classification import RandomForestClassifier
forest = RandomForestClassifier(numTrees=5)
訓練データに適合。
forest = forest.fit(cars_train)
森の中の木へはどうアクセスする?
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]
各木は個別予測にも使えます。
各木はどんな予測を出すか?
+------+------+------+------+------+-----+
|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| <- 完全一致
+------+------+------+------+------+-----+
.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 |
+-----+----------------------------------------+----------+
モデルは次の特徴量を使用: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 が最も低い重要度反復的ブースティング手順:
各反復でモデルが改善される。
勾配ブースティング木分類器を作成します。
from pyspark.ml.classification import GBTClassifier
gbt = GBTClassifier(maxIter=10)
訓練データに適合させます。
gbt = gbt.fit(cars_train)
3種の木モデルをテストデータで比較します。
# 決定木のAUC
0.5875
# ランダムフォレストのAUC
0.65
# 勾配ブースティング木のAUC
0.65
アンサンブル手法は単一の決定木より良好です。
Machine Learning with PySpark