scikit-learn - 使用 GridSearchCV 中嵌套的 RFECV 时,如何避免使用 estimator_params?

标签 scikit-learn feature-selection grid-search

我目前正在使用 scikit-learn 在基于树的方法的网格搜索 (GridSearchCV) 中研究递归特征消除 (RFECV)。为此,我使用 GitHub 上的当前开发版本 (0.17),该版本允许 RFECV 使用树方法中的特征重要性来选择要丢弃的特征。

为了清楚起见,这意味着:

  • 循环当前树方法的超参数
  • 对每组参数执行递归特征消除以获得最优数量的特征
  • 报告“分数”(例如准确性)
  • 确定哪组参数产生最佳得分

此代码目前工作正常 - 但我收到有关使用 estimator_params 的折旧警告。这是当前代码:

# set up list of parameter dictionaries (better way to do this?)
depth = [1, 5, None]
weight = ['balanced', None]
params = []

for d in depth:
    for w in weight:
    params.append(dict(max_depth=d, 
                       class_weight=w))

# specify the classifier
estimator = DecisionTreeClassifier(random_state=0, 
                                   max_depth=None, 
                                   class_weight='balanced')

# specify the feature selection method
selector = RFECV(estimator,
                 step=1, 
                 cv=3, 
                 scoring='accuracy')

# set up the parameter search
clf = GridSearchCV(selector, 
                   {'estimator_params': param_grid}, 
                   cv=3)

clf.fit(X_train, y_train)

clf.best_estimator_.estimator_

以下是完整的折旧警告:

home/csw34/git/scikit-learn/sklearn/feature_selection/rfe.py:154: DeprecationWarning:

The parameter 'estimator_params' is deprecated as of version 0.16 and will be removed in 0.18. The parameter is no longer necessary because the value is set via the estimator initialisation or set_params method.

如果不使用 GridSearchCV 中的 estimator_params 通过 RFECV 将参数传递给估计器,我如何能够获得相同的结果?

最佳答案

这解决了您的问题:

params = {'estimator__max_depth': [1, 5, None],
          'estimator__class_weight': ['balanced', None]}
estimator = DecisionTreeClassifier()
selector = RFECV(estimator, step=1, cv=3, scoring='accuracy')
clf = GridSearchCV(selector, params, cv=3)
clf.fit(X_train, y_train)
clf.best_estimator_.estimator_

要查看更多信息,请使用:

print(selector.get_params())

关于scikit-learn - 使用 GridSearchCV 中嵌套的 RFECV 时,如何避免使用 estimator_params?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31784392/

相关文章:

python - 一些数据的Scipy拟合多项式模型

python - 在 Windows 7 计算机上安装 scikit-learn 时遇到问题

python - 对于分类模型, `eli5.show_weights` 究竟显示了什么?

python - 在 fit 方法需要 3 个参数的自定义类上使用 sklearn GridSearchCV

python - 用于特征选择的详尽网格搜索

python - 在递归特征消除的每一步中对估计器进行超参数估计

python - 为什么 GridSearchCV 方法的精度低于标准方法?

python - GridSearchCV 对平均绝对误差进行评分

grid-search - 如何设置 `paradox` 中的具体值?

machine-learning - 在 H2O 中使用分类变量的最佳实践?