교차 검증

scikit-learn으로 배우는 지도 학습

George Boorman

Core Curriculum Manager, DataCamp

교차 검증 동기

  • 모델 성능은 데이터를 분할하는 방식에 따라 달라짐

  • 미학습 데이터에 대한 모델의 일반화 능력을 대표하지 않음

  • 해결책: 교차 검증!

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

table headings: split 1, fold 1, fold 2, fold 3, fold 4, and fold 5

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

split 1 reserved as a test set

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

folds 2-5 used as training data

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

compute metric on these folds

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

Fold 2 as test data

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

folds 1, 3, 4, and 5 as training data

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

calculate metric again

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

repeat with the third fold

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

repeat with fourth fold

scikit-learn으로 배우는 지도 학습

교차 검증의 기초

repeat with the fifth fold

scikit-learn으로 배우는 지도 학습

교차 검증과 모델 성능

  • 5개 폴드 = 5-겹 교차 검증

  • 10개 폴드 = 10-겹 교차 검증

  • k 폴드 = k-겹 교차 검증

  • 폴드가 많을수록 = 계산 비용 증가

scikit-learn으로 배우는 지도 학습

scikit-learn의 교차 검증

from sklearn.model_selection import cross_val_score, KFold

kf = KFold(n_splits=6, shuffle=True, random_state=42)
reg = LinearRegression()
cv_results = cross_val_score(reg, X, y, cv=kf)
scikit-learn으로 배우는 지도 학습

교차 검증 성능 평가하기

print(cv_results)
[0.70262578, 0.7659624, 0.75188205, 0.76914482, 0.72551151, 0.73608277]
print(np.mean(cv_results), np.std(cv_results))
0.7418682216666667 0.023330243960652888
print(np.quantile(cv_results, [0.025, 0.975]))
array([0.7054865, 0.76874702])
scikit-learn으로 배우는 지도 학습

연습해 봅시다!

scikit-learn으로 배우는 지도 학습

Preparing Video For Download...