Bagging हाइपरपैरामीटर्स ट्यून करना
हालाँकि आप डिफ़ॉल्ट पैरामीटर्स के साथ आसानी से एक bagging classifier बना सकते हैं, लेकिन बेहतर यह है कि आप उन्हें ट्यून करें ताकि परफॉर्मेंस बेहतरीन हो. आदर्श रूप से, इन्हें K-fold cross-validation का उपयोग करके ऑप्टिमाइज़ किया जाना चाहिए.
इस अभ्यास में, आइए देखें कि क्या हम bagging classifier के पैरामीटर्स बदलकर मॉडल की परफॉर्मेंस सुधार सकते हैं.
यहाँ हम computation समय कम करने के लिए LogisticRegression में पैरामीटर solver='liblinear' भी पास कर रहे हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Ensemble Methods
अभ्यास निर्देश
- लॉजिस्टिक रिग्रेशन को base बनाकर एक bagging classifier बनाइए, जिसमें
20base estimators,10अधिकतम features,0.65(65%) अधिकतम samples (max_samples), और sampling बिना replacement के हो. - टेस्ट सेट
X_testके labels प्रेडिक्ट करने के लिएclf_bagका उपयोग करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Build a balanced logistic regression
clf_base = LogisticRegression(class_weight='balanced', solver='liblinear', random_state=42)
# Build and fit a bagging classifier with custom parameters
clf_bag = ____(____, ____, ____, ____, ____, random_state=500)
clf_bag.fit(X_train, y_train)
# Calculate predictions and evaluate the accuracy on the test set
y_pred = ____
print('Accuracy: {:.2f}'.format(accuracy_score(y_test, y_pred)))
# Print the classification report
print(classification_report(y_test, y_pred))