Bagging का पहला प्रयास
आपने देखा कि bagging एन्सेम्बल की एक एकल iteration में क्या होता है. अब आइए एक कस्टम bagging मॉडल बनाते हैं!
आपके लिए दो फंक्शन तैयार किए गए हैं:
def build_decision_tree(X_train, y_train, random_state=None):
# Takes a sample with replacement,
# builds a "weak" decision tree,
# and fits it to the train set
def predict_voting(classifiers, X_test):
# Makes the individual predictions
# and then combines them using "Voting"
तकनीकी रूप से, build_decision_tree() फंक्शन वही है जो आपने पिछले अभ्यास में किया था. यहाँ, आप ऐसे कई पेड़ बनाएँगे और फिर उन्हें संयोजित करेंगे. देखें कि क्या इन "weak" मॉडलों का एन्सेम्बल प्रदर्शन में सुधार करता है!
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Ensemble Methods
अभ्यास निर्देश
- व्यक्तिगत मॉडल बनाने के लिए
build_decision_tree()कॉल करें, जिसमें training set और इंडेक्सiको random state के रूप में पास करें. - टेस्ट सेट के लेबल
predict_voting()का उपयोग करके प्रेडिक्ट करें, जहाँ क्लासिफायरों की सूचीclf_listऔर इनपुट टेस्ट फीचर्स पास किए जाएँ.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Build the list of individual models
clf_list = []
for i in range(21):
weak_dt = ____
clf_list.append(weak_dt)
# Predict on the test set
pred = ____
# Print the F1 score
print('F1 score: {:.3f}'.format(f1_score(y_test, pred)))