교차 검증

Python에서의 모델 검증

Kasey Jones

Data Scientist

교차 검증

훈련, 검증, 테스트 세트를 만들려면 전체 데이터를 3개로 나눕니다. 예: 훈련 60%, 검증 20%, 테스트 20%.

Python에서의 모델 검증

교차 검증

교차 검증은 데이터를 한 번의 80:20이 아니라 여러 번 훈련/검증으로 분할합니다. 예를 들어 80:20 분할을 5번 사용합니다.

Python에서의 모델 검증

n_splits: 교차 검증 분할 수

shuffle: 분할 전 셔플 여부

random_state: 난수 시드

from sklearn.model_selection import KFold

X = np.array(range(40))
y = np.array([0] * 20 + [1] * 20)

kf = KFold(n_splits=5)

splits = kf.split(X)
Python에서의 모델 검증
kf = KFold(n_splits=5)
splits = kf.split(X)

for train_index, test_index in splits: print(len(train_index), len(test_index))
32 8 32 8 32 8 32 8 32 8
# 인덱스 세트 중 하나 출력:
print(train_index, test_index)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 ...]
[32 33 34 35 36 37 38 39]
Python에서의 모델 검증
rfr = RandomForestRegressor(n_estimators=25, random_state=1111)

errors = [] for train_index, val_index in splits: X_train, y_train = X[train_index], y[train_index] X_val, y_val = X[val_index], y[val_index] rfr.fit(X_train, y_train) predictions = rfr.predict(X_val) errors.append(<some_accuracy_metric>)
print(np.mean(errors))
4.25
Python에서의 모델 검증

연습 시간

Python에서의 모델 검증

Preparing Video For Download...