Random forests

Machine Learning สำหรับการเงินด้วย Python

Nathan George

Data Science Professor

การฟิตแบบพหุนาม

Machine Learning สำหรับการเงินด้วย Python

การฟิตแบบเส้นตรง

Machine Learning สำหรับการเงินด้วย Python

Random forests

random forest graphviz

Machine Learning สำหรับการเงินด้วย Python

Bootstrap aggregating (bagging)

bootstrapping

Machine Learning สำหรับการเงินด้วย Python

Feature sampling

Random Forests

  • กลุ่ม (ensemble) ของ decision trees
  • Bootstrap aggregating (bagging)
  • สุ่มเลือก feature ที่แต่ละจุดแบ่ง
Machine Learning สำหรับการเงินด้วย Python

การใช้งานใน sklearn

from sklearn.ensemble import RandomForestRegressor

random_forest = RandomForestRegressor()
random_forest.fit(train_features, train_targets)
print(random_forest.score(train_features, train_targets))
Machine Learning สำหรับการเงินด้วย Python

Hyperparameters

random_forest = RandomForestRegressor(n_estimators=200,
                                      max_depth=5,
                                      max_features=4,
                                      random_state=42)
Machine Learning สำหรับการเงินด้วย Python

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}]
Machine Learning สำหรับการเงินด้วย Python

ParameterGrid

test_scores = []
# วนลูปผ่าน parameter grid, กำหนด hyperparameters และบันทึกคะแนน
for g in ParameterGrid(grid):
    rfr.set_params(**g)  # ** คือการ "unpack" dictionary
    rfr.fit(train_features, train_targets)
    test_scores.append(rfr.score(test_features, test_targets))

# หา hyperparameters ที่ดีที่สุดจากคะแนนทดสอบและแสดงผล 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}
Machine Learning สำหรับการเงินด้วย Python

มาสร้าง random forests กันเถอะ!

Machine Learning สำหรับการเงินด้วย Python

Preparing Video For Download...