랜덤 포레스트

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

Nathan George

Data Science Professor

다항식 적합

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

선형 적합

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

랜덤 포레스트

랜덤 포레스트 graphviz

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

부트스트랩 집계(배깅)

부트스트래핑

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

특성 샘플링

랜덤 포레스트

  • 의사결정 트리의 집합(앙상블)
  • 부트스트랩 집계(배깅)
  • 각 분기에서 특성 샘플링
Python으로 배우는 금융 분야 Machine Learning

sklearn 구현

from sklearn.ensemble import RandomForestRegressor

random_forest = RandomForestRegressor()
random_forest.fit(train_features, train_targets)
print(random_forest.score(train_features, train_targets))
Python으로 배우는 금융 분야 Machine Learning

하이퍼파라미터

random_forest = RandomForestRegressor(n_estimators=200,
                                      max_depth=5,
                                      max_features=4,
                                      random_state=42)
Python으로 배우는 금융 분야 Machine Learning

ParameterGrid

from sklearn.model_selection import ParameterGrid

grid = {'n_estimators': [200], 
        'max_depth':[3, 5], 
        'max_features': [4, 8]}

from pprint import pprint pprint(list(ParameterGrid(grid)))
[{'max_depth': 3, 'max_features': 4, 'n_estimators': 200},
 {'max_depth': 3, 'max_features': 8, 'n_estimators': 200},
 {'max_depth': 5, 'max_features': 4, 'n_estimators': 200},
 {'max_depth': 5, 'max_features': 8, 'n_estimators': 200}]
Python으로 배우는 금융 분야 Machine Learning

ParameterGrid

test_scores = []
# 파라미터 그리드를 순회하며 하이퍼파라미터를 설정하고 점수를 저장합니다
for g in ParameterGrid(grid):
    rfr.set_params(**g)  # **는 딕셔너리 "언패킹"을 의미합니다
    rfr.fit(train_features, train_targets)
    test_scores.append(rfr.score(test_features, test_targets))

# 테스트 점수에서 최적 하이퍼파라미터를 찾아 출력합니다 best_idx = np.argmax(test_scores) print(test_scores[best_idx]) print(ParameterGrid(grid)[best_idx])
0.05594252725411142
{'max_depth': 5, 'max_features': 8, 'n_estimators': 200}
Python으로 배우는 금융 분야 Machine Learning

랜덤 포레스트를 심어 보세요!

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

Preparing Video For Download...