Partitioning
किसी मॉडल का सही मूल्यांकन करने के लिए, डेटा को train और test सेट में विभाजित किया जा सकता है। train सेट में वह डेटा होता है जिस पर मॉडल बनाया जाता है, और test डेटा मॉडल का मूल्यांकन करने के काम आता है। यह विभाजन रैंडम होता है, लेकिन जब target incidence कम हो, तो stratify करना ज़रूरी हो सकता है — यानी यह सुनिश्चित करना कि train और test डेटा में targets का प्रतिशत बराबर हो।
इस अभ्यास में आप stratification के साथ डेटा को विभाजित करेंगे और जाँचेंगे कि train और test डेटा में target incidence बराबर है। train_test_split मेथड पहले से इम्पोर्ट है, और X तथा y DataFrames आपके workspace में उपलब्ध हैं।
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में प्रिडिक्टिव एनालिटिक्स परिचय
अभ्यास निर्देश
train_test_splitमेथड का उपयोग करके इन DataFrames को stratify कीजिए। सुनिश्चित कीजिए कि train और test सेट का आकार बराबर हो और दोनों में target incidence समान हो।- train सेट का target incidence निकालिए। यह train सेट में targets की संख्या को train सेट में observations की संख्या से भाग देने पर मिलेगा।
- test सेट का target incidence निकालिए।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Load the partitioning module
from sklearn.model_selection import train_test_split
# Create DataFrames with variables and target
X = basetable.drop("target", 1)
y = basetable["target"]
# Carry out 50-50 partititioning with stratification
X_train, X_test, y_train, y_test = ____(X, y, test_size = ____, stratify = ____)
# Create the final train and test basetables
train = pd.concat([X_train, y_train], axis=1)
test = pd.concat([X_test, y_test], axis=1)
# Check whether train and test have same percentage targets
print(round(sum(train[____])/len(____), 2))
print(round(sum(test[____])/len(____), 2))