验证用法

用 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 竞赛

Vamos praticar!

用 Python 赢下 Kaggle 竞赛

Preparing Video For Download...