隨機森林

Python 金融 Machine Learning

Nathan George

Data Science Professor

多項式擬合

Python 金融 Machine Learning

線性擬合

Python 金融 Machine Learning

隨機森林

隨機森林 graphviz

Python 金融 Machine Learning

自助聚合(bagging)

自助抽樣

Python 金融 Machine Learning

特徵抽樣

Random Forests

  • 多棵決策樹的集成
  • 自助聚合(bagging)
  • 每次分割隨機抽樣部分特徵
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 = []
# loop through the parameter grid, set hyperparameters, save the scores
for g in ParameterGrid(grid):
    rfr.set_params(**g)  # ** is "unpacking" the dictionary
    rfr.fit(train_features, train_targets)
    test_scores.append(rfr.score(test_features, test_targets))

# find best hyperparameters from the test score and print 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...