我正在使用下面的工作流来训练随机森林分类器以供生产使用。我正在使用 RandomizedSearchCV 通过打印结果来调整分类器的参数,然后使用 RandomizedSearchCV 的结果创建一个新管道。我认为必须有一种方法可以将 RandomizedSearchCV 的最佳结果简单地指向分类器,这样我就不必手动操作,但我不知道如何操作。
select = sklearn.feature_selection.SelectKBest(k=40)
clf = sklearn.ensemble.RandomForestClassifier()
steps = [('feature_selection', select),
('random_forest', clf)]
parameters = {"random_forest__max_depth": [3, None],
"random_forest__max_features": sp_randint(1, 21),
"random_forest__min_samples_split": sp_randint(1, 21),
"random_forest__min_samples_leaf": sp_randint(1, 21),
"random_forest__bootstrap": [True, False],
"random_forest__criterion": ["gini", "entropy"]}
pipeline = sklearn.pipeline.Pipeline(steps)
n_iter_search = 20
cv = RandomizedSearchCV(pipeline, param_distributions = parameters, n_iter=n_iter_search)
cv.fit(X,y)