검증 활용

Python으로 Kaggle 대회 공략하기

Yauhen Babakhin

Kaggle Grandmaster

데이터 누수

 

누수 로고

 

  • 피처 누수 – 실제 환경에서 사용 불가한 데이터를 사용함

  • 검증 전략 누수 – 검증 전략이 실제 상황과 다름

Python으로 Kaggle 대회 공략하기

시계열 데이터

 

시계열 데이터에 부적절한 검증 전략

Python으로 Kaggle 대회 공략하기

시계열 K-폴드 교차검증

 

 

시계열 K-폴드 교차 검증 구조

Python으로 Kaggle 대회 공략하기

시계열 K-폴드 교차검증

# Import TimeSeriesSplit
from sklearn.model_selection import TimeSeriesSplit

# Create a TimeSeriesSplit object
time_kfold = TimeSeriesSplit(n_splits=5)
# Sort train by date
train = train.sort_values('date')

# Loop through each cross-validation split
for train_index, test_index in time_kfold.split(train):
    cv_train, cv_test = train.iloc[train_index], train.iloc[test_index]
Python으로 Kaggle 대회 공략하기

검증 파이프라인

# List for the results
fold_metrics = []

for train_index, test_index in CV_STRATEGY.split(train): cv_train, cv_test = train.iloc[train_index], train.iloc[test_index]
# Train a model model.fit(cv_train)
# Make predictions predictions = model.predict(cv_test)
# Calculate the metric metric = evaluate(cv_test, predictions) fold_metrics.append(metric)
Python으로 Kaggle 대회 공략하기

모델 비교

 

폴드 번호 모델 A MSE 모델 B MSE
폴드 1 2.95 2.97
폴드 2 2.84 2.45
폴드 3 2.62 2.73
폴드 4 2.79 2.83
Python으로 Kaggle 대회 공략하기

전체 검증 점수

import numpy as np

# Simple mean over the folds
mean_score = np.mean(fold_metrics)
# Overall validation score
overall_score_minimizing = np.mean(fold_metrics) + np.std(fold_metrics)
# Or
overall_score_maximizing = np.mean(fold_metrics) - np.std(fold_metrics)
Python으로 Kaggle 대회 공략하기

모델 비교

폴드 번호 모델 A MSE 모델 B MSE
폴드 1 2.95 2.97
폴드 2 2.84 2.45
폴드 3 2.62 2.73
폴드 4 2.79 2.83
평균 2.80 2.75
전체 2.919 2.935
Python으로 Kaggle 대회 공략하기

Ayo berlatih!

Python으로 Kaggle 대회 공략하기

Preparing Video For Download...