将 RandomizedSearchCV 指向分类器

Pointing RandomizedSearchCV to a Classifier

我正在使用下面的工作流程来训练用于生产的随机森林分类器。我正在使用 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)

不知道在RandomizedSearchCV对象中,剩余的estimator是最好的还是最后一个拟合的。您可以访问 best_estimator_ 属性以确保您获得最佳模型。