PySpark के साथ Machine Learning
Andrew Collier
Data Scientist, Fathom Data
यह मॉडलों का एक संग्रह है।
"Wisdom of the Crowd" — समूह की सामूहिक राय अक्सर एक अकेले विशेषज्ञ से बेहतर होती है।
Diversitiy और independence ज़रूरी हैं क्योंकि सबसे अच्छे सामूहिक निर्णय सहमति या समझौते से नहीं, बल्कि असहमति और बहस से बनते हैं।
― जेम्स सुरोविएकी, The Wisdom of Crowds
Random Forest — Decision Trees का एंसेंबल
मॉडल विविधता बनाना:
जंगल में कोई दो ट्री एक जैसे नहीं होने चाहिए।

कार्स डेटा पर लौटें: USA में निर्मित (0.0) या नहीं (1.0).
Random Forest classifier बनाएँ।
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| <- 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
+------+------+------+------+------+-----+
कंसेंसस प्रेडिक्शन बनाने के लिए .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 सबसे कम महत्वपूर्ण है।इटरेटिव बूस्टिंग एल्गोरिदम:
हर इटरेशन में मॉडल बेहतर होता है।
Gradient-Boosted Tree classifier बनाएँ।
from pyspark.ml.classification import GBTClassifier
gbt = GBTClassifier(maxIter=10)
ट्रेनिंग डेटा पर फिट करें।
gbt = gbt.fit(cars_train)
टेस्टिंग डेटा पर तीन तरह के ट्री मॉडलों की तुलना करें।
# AUC for Decision Tree
0.5875
# AUC for Random Forest
0.65
# AUC for Gradient-Boosted Tree
0.65
दोनों एंसेंबल मेथड साधारण Decision Tree से बेहतर प्रदर्शन करते हैं।
PySpark के साथ Machine Learning