Mean target encoding
सबसे पहले, आप एक फंक्शन बनाएँगे जो mean target encoding इम्प्लीमेंट करता है। याद रखिए कि आपको ये दो चरण विकसित करने हैं:
- ट्रेन पर mean निकालें, उसे टेस्ट पर अप्लाई करें
- ट्रेन को K folds में बाँटें। हर fold के लिए out-of-fold mean निकालें और उसी fold पर अप्लाई करें
इनमें से हर चरण अलग फंक्शन में इम्प्लीमेंट होगा: क्रमशः test_mean_target_encoding() और train_mean_target_encoding().
अंतिम फंक्शन mean_target_encoding() के आर्ग्युमेंट होंगे: train और test DataFrames, एन्कोड की जाने वाली categorical कॉलम का नाम, target कॉलम का नाम, और एक smoothing पैरामीटर alpha। यह दो वैल्यू लौटाता है: क्रमशः train और test DataFrames के लिए नया फीचर.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Kaggle प्रतियोगिता जीतना
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
def test_mean_target_encoding(train, test, target, categorical, alpha=5):
# Calculate global mean on the train data
global_mean = train[target].mean()
# Group by the categorical feature and calculate its properties
train_groups = train.groupby(categorical)
category_sum = train_groups[target].sum()
category_size = train_groups.size()
# Calculate smoothed mean target statistics
train_statistics = (category_sum + global_mean * alpha) / (category_size + ____)
# Apply statistics to the test data and fill new categories
test_feature = test[categorical].map(train_statistics).fillna(____)
return test_feature.values