Stratified K-fold
जैसा आपने अभी देखा, रैंडम स्प्लिट्स के कारण अलग-अलग फोल्ड्स में टारगेट वैरिएबल का वितरण काफी अलग है। यह इस विशेष प्रतियोगिता के लिए बहुत महत्वपूर्ण नहीं है, लेकिन अत्यधिक असंतुलित टारगेट वैरिएबल वाली क्लासिफिकेशन प्रतियोगिताओं में यह समस्या बन सकती है.
इसे दूर करने के लिए, आइए टारगेट वैरिएबल पर स्ट्रैटिफिकेशन के साथ stratified K-fold रणनीति लागू करें। train DataFrame आपके वर्कस्पेस में पहले से उपलब्ध है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Kaggle प्रतियोगिता जीतना
अभ्यास निर्देश
- 3 फोल्ड्स और शफलिंग के साथ एक
StratifiedKFoldऑब्जेक्ट बनाइए. str_kfऑब्जेक्ट का उपयोग करके हर स्प्लिट पर लूप चलाइए। स्ट्रैटिफिकेशन "interest_level" कॉलम पर आधारित है.- हर स्प्लिट के लिए
train_indexऔरtest_indexका उपयोग करके training और testing फोल्ड्स चुनिए.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Import StratifiedKFold
from sklearn.model_selection import StratifiedKFold
# Create a StratifiedKFold object
str_kf = ____(n_splits=____, shuffle=____, random_state=123)
# Loop through each split
fold = 0
for train_index, test_index in ____.____(train, train['interest_level']):
# Obtain training and testing folds
cv_train, cv_test = ____.iloc[____], ____.iloc[____]
print('Fold: {}'.format(fold))
print('CV train shape: {}'.format(cv_train.shape))
print('Medium interest listings in CV train: {}\n'.format(sum(cv_train.interest_level == 'medium')))
fold += 1