Ensemble

Machine Learning with PySpark

Andrew Collier

Data Scientist, Fathom Data

Ensemble คืออะไร?

คือกลุ่มของโมเดลหลายตัว

กลุ่มของโมเดลที่คล้ายกัน

Wisdom of the Crowd — ความเห็นร่วมของกลุ่มมักแม่นยำกว่าผู้เชี่ยวชาญคนเดียว

Machine Learning with PySpark

ความหลากหลายของ Ensemble

 

 

 

ความหลากหลาย และ ความเป็นอิสระ มีความสำคัญ เพราะการตัดสินใจร่วมที่ดีที่สุดเกิดจากความขัดแย้งและการแข่งขัน ไม่ใช่ฉันทามติหรือการประนีประนอม

― James Surowiecki, The Wisdom of Crowds

Machine Learning with PySpark

Random Forest

Random Forest — Ensemble ของ Decision Tree

การสร้างความหลากหลายให้โมเดล:

  • แต่ละต้นไม้ฝึกด้วย ชุดข้อมูลสุ่ม
  • ใช้ ฟีเจอร์สุ่ม ในการแบ่งที่แต่ละโหนด

แต่ละต้นไม้ในป่าควรแตกต่างกัน

กลุ่มของต้นไม้

Machine Learning with PySpark

สร้าง Forest จาก Tree

กลับมาที่ข้อมูลรถยนต์: ผลิตในสหรัฐฯ (0.0) หรือไม่ (1.0)

สร้าง Random Forest classifier

from pyspark.ml.classification import RandomForestClassifier

forest = RandomForestClassifier(numTrees=5)

ฝึกกับข้อมูล training

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

มองเห็นต้นไม้แต่ละต้น

เข้าถึงต้นไม้แต่ละต้นใน forest ได้อย่างไร?

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| <- perfect agreement
|   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| <- perfect agreement
+------+------+------+------+------+-----+
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

Gradient-Boosted Trees

อัลกอริทึม boosting แบบวนซ้ำ:

  1. สร้าง Decision Tree แล้วเพิ่มเข้า ensemble
  2. ทำนาย label ของแต่ละข้อมูลด้วย ensemble
  3. เปรียบเทียบผลทำนายกับ label จริง
  4. เพิ่มน้ำหนักให้ข้อมูลที่ทำนายผิด
  5. กลับไปขั้นที่ 1

โมเดลดีขึ้นในแต่ละรอบ

Machine Learning with PySpark

Boosting Trees

สร้าง Gradient-Boosted Tree classifier

from pyspark.ml.classification import GBTClassifier

gbt = GBTClassifier(maxIter=10)

ฝึกกับข้อมูล training

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

เปรียบเทียบ Tree

เปรียบเทียบโมเดล tree ทั้ง 3 ประเภทบนข้อมูล testing

# AUC for Decision Tree
0.5875

# AUC for Random Forest
0.65

# AUC for Gradient-Boosted Tree
0.65

ทั้ง ensemble method ให้ผลดีกว่า Decision Tree แบบเดี่ยว

Machine Learning with PySpark

รวมโมเดลทั้งหมดเป็น Ensemble!

Machine Learning with PySpark

Preparing Video For Download...