एंसेंबल

PySpark के साथ Machine Learning

Andrew Collier

Data Scientist, Fathom Data

एंसेंबल क्या है?

यह मॉडलों का एक संग्रह है।

मिलते-जुलते मॉडलों का संग्रह

"Wisdom of the Crowd" — समूह की सामूहिक राय अक्सर एक अकेले विशेषज्ञ से बेहतर होती है।

PySpark के साथ Machine Learning

एंसेंबल विविधता

 

 

 

Diversitiy और independence ज़रूरी हैं क्योंकि सबसे अच्छे सामूहिक निर्णय सहमति या समझौते से नहीं, बल्कि असहमति और बहस से बनते हैं।

― जेम्स सुरोविएकी, The Wisdom of Crowds

PySpark के साथ Machine Learning

Random Forest

Random Forest — Decision Trees का एंसेंबल

मॉडल विविधता बनाना:

  • हर ट्री को डेटा के रैंडम सबसेट पर ट्रेन किया जाता है
  • हर नोड पर स्प्लिट के लिए फीचर्स का रैंडम सबसेट लिया जाता है

जंगल में कोई दो ट्री एक जैसे नहीं होने चाहिए।

पेड़ों का समूह

PySpark के साथ Machine Learning

पेड़ों का जंगल बनाएँ

कार्स डेटा पर लौटें: USA में निर्मित (0.0) या नहीं (1.0).

Random Forest classifier बनाएँ।

from pyspark.ml.classification import RandomForestClassifier

forest = RandomForestClassifier(numTrees=5)

ट्रेनिंग डेटा पर फिट करें।

forest = forest.fit(cars_train)
PySpark के साथ Machine Learning

ट्रीज़ देखना

फॉरेस्ट के अंदर ट्रीज़ तक कैसे पहुँचें?

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]

इनमें से हर एक से अलग-अलग प्रेडिक्शन किए जा सकते हैं।

PySpark के साथ Machine Learning

अलग-अलग ट्री के प्रेडिक्शन

हर ट्री कौन से प्रेडिक्शन बनाता है?

+------+------+------+------+------+-----+
|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
+------+------+------+------+------+-----+
PySpark के साथ Machine Learning

कंसेंसस प्रेडिक्शन

कंसेंसस प्रेडिक्शन बनाने के लिए .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       |
+-----+----------------------------------------+----------+
PySpark के साथ Machine Learning

फीचर इम्पॉर्टेंस

मॉडल ये फीचर्स उपयोग करता है: 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 सबसे कम महत्वपूर्ण है।
PySpark के साथ Machine Learning

Gradient-Boosted Trees

इटरेटिव बूस्टिंग एल्गोरिदम:

  1. एक Decision Tree बनाएँ और एंसेंबल में जोड़ें।
  2. एंसेंबल से हर ट्रेनिंग इंस्टेंस के लिए लेबल प्रेडिक्ट करें।
  3. प्रेडिक्शन को ज्ञात लेबल से तुलना करें।
  4. जिन इंस्टेंस के प्रेडिक्शन गलत हों, उन्हें अधिक वेट दें।
  5. फिर से 1 पर लौटें।

हर इटरेशन में मॉडल बेहतर होता है।

PySpark के साथ Machine Learning

ट्रीज़ को बूस्ट करना

Gradient-Boosted Tree classifier बनाएँ।

from pyspark.ml.classification import GBTClassifier

gbt = GBTClassifier(maxIter=10)

ट्रेनिंग डेटा पर फिट करें।

gbt = gbt.fit(cars_train)
PySpark के साथ Machine Learning

ट्रीज़ की तुलना

टेस्टिंग डेटा पर तीन तरह के ट्री मॉडलों की तुलना करें।

# AUC for Decision Tree
0.5875

# AUC for Random Forest
0.65

# AUC for Gradient-Boosted Tree
0.65

दोनों एंसेंबल मेथड साधारण Decision Tree से बेहतर प्रदर्शन करते हैं।

PySpark के साथ Machine Learning

सभी मॉडलों को एंसेंबल करें!

PySpark के साथ Machine Learning

Preparing Video For Download...