시작하기무료로 시작하기

KFold 인덱스 사용하기

이미 5-겹 교차 검증을 완료하기 위해 candy-data 데이터셋의 인덱스를 담은 splits를 만들어 두었어요. 동료의 랜덤 포레스트 모델이 새로운 데이터에서 얼마나 잘 작동할지 더 정확히 추정하기 위해, 방금 만든 다섯 개의 학습/검증 인덱스에서 이 모델을 실행하려고 해요.

이 연습 문제에서는 이 인덱스들을 사용해 다섯 개 분할 각각에 대해 모델의 정확도를 확인해 볼 거예요. 이 과정을 도와줄 for 루프가 제공되어 있어요.

이 연습은 강의의 일부입니다

Python에서의 모델 검증

강의 보기

연습 안내

  • 학습 및 검증 데이터를 만들 때 train_indexval_index를 사용해 Xy에서 올바른 인덱스를 선택하세요.
  • 학습 데이터셋으로 rfc를 학습(fit)하세요.
  • 검증 데이터셋에 대해 rfc로 예측을 만들고 검증 정확도를 출력하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error

rfc = RandomForestRegressor(n_estimators=25, random_state=1111)

# Access the training and validation indices of splits
for train_index, val_index in splits:
    # Setup the training and validation data
    X_train, y_train = X[____], y[____]
    X_val, y_val = X[____], y[____]
    # Fit the random forest model
    rfc.____(____, ____)
    # Make predictions, and print the accuracy
    predictions = rfc.____(____)
    print("Split accuracy: " + str(mean_squared_error(y_val, predictions)))
코드 편집 및 실행