为什么RandomizedSearchCV慢?

2024-04-19 15:37:06 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试对随机分类器进行超参数优化。看起来RandomizedSearchCV比一组等效的RandomForestClassifier运行慢14倍。你知道吗

下面提供的两个示例使用相同的训练数据和相同的折叠次数(6)。示例#1是一个经典的RandomForestClassifier()健身跑。示例2是在1点随机网格上运行的RandomizedSearchCV()。你知道吗

运行时间:1分钟8秒对14分钟13秒。我错过了什么?你知道吗

例1:

%%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

例2:

%%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 

Tags: 示例timejobsrandomfoldcpucvsplit