C.W.K.
Stream
Lesson 01 of 07 · published

Cross-Validation

~28 min · cv, validation, model-selection

Level 0Scout
0 XP0/48 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

왜 single split으론 부족한가

단일 held-out validation set이 모델 품질의 noisy estimator. small이나 imbalanced 데이터에선, noise가 고르려는 모델 사이 차이보다 클 수 있어. K-fold cross-validation이 multiple split을 평균 내서 비교가 정직해.

중요한 세 flavor

  • StratifiedKFold — classification에 fold 별 class 비율 보존.
  • GroupKFold — 한 group(user, session, patient)의 모든 row를 같은 fold 안에 유지.
  • TimeSeriesSplit — 과거에서 train, 미래에서 validate. 절대 반대 X.

CV score 정직하게 읽기

fold 사이 mean과 std 항상 보고. std가 후보 모델 두 개 사이 gap의 절반이면 비교가 noise야. fold 늘리거나 multiple seed 평균 내거나, 아직 구분 못 한다고 인정해.

Code

std 동반한 stratified 5-fold cross-validation·python
from sklearn.model_selection import StratifiedKFold, cross_val_score
import numpy as np

cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=7)
scores = cross_val_score(pipe, X, y, cv=cv, scoring="average_precision")
print(f"PR-AUC = {scores.mean():.3f} ± {scores.std():.3f}")
user에 row 묶일 때 group-aware CV·python
from sklearn.model_selection import GroupKFold

gkf = GroupKFold(n_splits=5)
scores = cross_val_score(pipe, X, y, cv=gkf, groups=df["user_id"], scoring="roc_auc")
forecasting을 위한 time-respecting CV·python
from sklearn.model_selection import TimeSeriesSplit

tscv = TimeSeriesSplit(n_splits=5, gap=14)  # 14-day gap to avoid leakage
scores = cross_val_score(pipe, X_sorted, y_sorted, cv=tscv, scoring="neg_root_mean_squared_error")

External links

Exercise

문제에 StratifiedKFold(n_splits=5, shuffle=True) 돌려. mean과 std 보고. n_splits=10으로 다시 돌려서 std 비교. 0.5% 차이 모델 두 개 비교에 충분히 안정된 fold count를 결정.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.