시작하기무료로 시작하기

성능 평가하기

마지막으로, 그리고 늘 그렇듯이, 가장 성능이 좋은 모델의 성능을 평가해 얼마나 잘(혹은 못) 예측하는지 확인해 보려고 합니다. 이상적으로는 백테스트를 하는 것이 가장 좋지만, 이 과정은 다루기엔 분량이 많아 본 강의에서는 진행하지 않습니다.

이미 R\(^2\) 점수는 확인했으니, 이제 matplotlib을 사용해 예측값과 실제값의 산점도를 살펴보겠습니다. 완벽한 예측이라면 왼쪽 아래에서 오른쪽 위로 향하는 대각선 모양이 됩니다.

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

Python으로 배우는 금융 분야 Machine Learning

강의 보기

연습 안내

  • 이전 연습 문제에서 찾은 최적의 max_features 값을 RandomForestRegressor(rfr)에 사용하세요(값은 4였습니다).
  • 모델을 사용해 train_featurestest_features로 예측을 만드세요.
  • 실제 타깃값(train/test_targets)과 예측값(train/test_predictions)을 산점도로 그린 뒤, 데이터셋 레이블은 각각 traintest로 지정하세요.

실습형 인터랙티브 연습

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

# Use the best hyperparameters from before to fit a random forest model
rfr = RandomForestRegressor(n_estimators=200, max_depth=3, max_features=____, random_state=42)
rfr.fit(train_features, train_targets)

# Make predictions with our model
train_predictions = rfr.predict(____)
test_predictions = ____

# Create a scatter plot with train and test actual vs predictions
plt.scatter(train_targets, train_predictions, label='train')
plt.scatter(____)
plt.legend()
plt.show()
코드 편집 및 실행