Cách dùng validation

Chinh phục cuộc thi Kaggle bằng Python

Yauhen Babakhin

Kaggle Grandmaster

Rò rỉ dữ liệu

 

logo rò rỉ dữ liệu

 

  • Rò rỉ ở đặc trưng – dùng dữ liệu sẽ không có trong thực tế

  • Rò rỉ ở chiến lược validation – chiến lược validation khác tình huống thực tế

Chinh phục cuộc thi Kaggle bằng Python

Dữ liệu thời gian

 

chiến lược validation sai cho dữ liệu thời gian

Chinh phục cuộc thi Kaggle bằng Python

K-fold theo thời gian

 

 

lược đồ cross-validation K-fold theo thời gian

Chinh phục cuộc thi Kaggle bằng Python

K-fold theo thời gian

# 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]
Chinh phục cuộc thi Kaggle bằng Python

Quy trình validation

# 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)
Chinh phục cuộc thi Kaggle bằng Python

So sánh mô hình

 

Số fold MSE Mô hình A MSE Mô hình B
Fold 1 2.95 2.97
Fold 2 2.84 2.45
Fold 3 2.62 2.73
Fold 4 2.79 2.83
Chinh phục cuộc thi Kaggle bằng Python

Điểm validation tổng thể

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)
Chinh phục cuộc thi Kaggle bằng Python

So sánh mô hình

Số fold MSE Mô hình A MSE Mô hình B
Fold 1 2.95 2.97
Fold 2 2.84 2.45
Fold 3 2.62 2.73
Fold 4 2.79 2.83
Trung bình 2.80 2.75
Tổng thể 2.919 2.935
Chinh phục cuộc thi Kaggle bằng Python

Ayo berlatih!

Chinh phục cuộc thi Kaggle bằng Python

Preparing Video For Download...