시작하기무료로 시작하기

Time K-fold

가게-상품 판매 데이터가 주어지고, 향후 판매량을 예측해야 했던 "Store Item Demand Forecasting Challenge"를 기억하시나요?

이 대회는 시계열 데이터가 있는 문제예요. 따라서 time K-fold 교차 검증을 적용해야 합니다. 목표는 이 교차 검증 전략을 만들고, 기대한 대로 동작하는지 확인하는 것입니다.

train DataFrame은 이미 작업 공간에 준비되어 있고, sklearn.model_selection에서 TimeSeriesSplit이 임포트되어 있다는 점에 유의하세요.

이 연습은 강의의 일부입니다

Python으로 Kaggle 대회 공략하기

강의 보기

연습 안내

  • 분할을 3개로 하여 TimeSeriesSplit 객체를 생성하세요.
  • time K-fold를 적용하기 위해 train 데이터를 "date" 열 기준으로 정렬하세요.
  • time_kfold 객체를 사용해 각 시점 분할을 순회하세요.
  • 각 분할마다 train_indextest_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
코드 편집 및 실행