Time K-fold
จำการแข่งขัน "Store Item Demand Forecasting Challenge" ได้ไหม? ในการแข่งขันนั้น เราได้รับข้อมูลยอดขายของสินค้าในร้าน และต้องพยากรณ์ยอดขายในอนาคต
นี่คือการแข่งขันที่ใช้ข้อมูล time series ดังนั้นจึงควรใช้การ cross-validation แบบ time K-fold โดยเป้าหมายของแบบฝึกหัดนี้คือการสร้างกลยุทธ์ cross-validation ดังกล่าวและตรวจสอบว่าทำงานได้ถูกต้อง
โปรดทราบว่า DataFrame train พร้อมใช้งานใน workspace แล้ว และ TimeSeriesSplit ถูก import มาจาก sklearn.model_selection เรียบร้อยแล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การแข่งขัน Kaggle ด้วย Python
คำแนะนำการฝึกหัด
- สร้างออบเจกต์
TimeSeriesSplitโดยกำหนดให้มี 3 splits - เรียงลำดับข้อมูลฝึกตามคอลัมน์ "date" เพื่อใช้กับ time K-fold
- วนลูปผ่านแต่ละ time split โดยใช้ออบเจกต์
time_kfold - สำหรับแต่ละ split ให้เลือกชุดข้อมูลสำหรับฝึกและทดสอบโดยใช้
train_indexและtest_index
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Create TimeSeriesSplit object
time_kfold = TimeSeriesSplit(n_splits=____)
# Sort train data by date
train = train.sort_values(____)
# Iterate through each split
fold = 0
for train_index, test_index in ____.____(____):
cv_train, cv_test = ____.____[____], ____.____[____]
print('Fold :', fold)
print('Train date range: from {} to {}'.format(cv_train.date.min(), cv_train.date.max()))
print('Test date range: from {} to {}\n'.format(cv_test.date.min(), cv_test.date.max()))
fold += 1