尝试对RandomForestClassifier进行超参数优化。看来RandomizedSearchCV比同等的RandomForestClassifier运行慢14倍。
下面提供的两个示例使用相同的训练数据和相同的倍数(6)。示例1是经典的RandomForestClassifier()
拟合运行。示例#2是在1点random_grid上运行的RandomizedSearchCV()
。
运行时间:1min 8s vs. 14min 13s。我想念什么?
%%time
n_fold = 6
time_split = TimeSeriesSplit(n_splits=n_fold)
clf = RandomForestClassifier()
cv_scores = cross_val_score(clf, X, y, cv=time_split, scoring='roc_auc', n_jobs=-1)
# CPU times: user 410 ms, sys: 868 ms, total: 1.28 s
# Wall time: 1min 8s
%%time
print(random_grid)
n_fold = 6
rf = RandomForestClassifier()
rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 6, scoring = 'roc_auc', cv = n_fold, verbose=True, random_state=42, n_jobs = -1)
rf_random.fit(X, y)
best_random = rf_random.best_estimator_
# {'n_estimators': [200], 'max_features': ['auto'], 'max_depth': [10], 'min_samples_split': [5], 'min_samples_leaf': [2], 'bootstrap': [True]}
# Fitting 6 folds for each of 1 candidates, totalling 6 fits
# CPU times: user 5min 15s, sys: 4.73 s, total: 5min 20s
# Wall time: 14min 13s