使用嵌套在 GridSearchCV 中的 RFECV 时,如何避免使用 estimator_params? [英] How can I avoid using estimator_params when using RFECV nested within GridSearchCV?

查看:29
本文介绍了使用嵌套在 GridSearchCV 中的 RFECV 时,如何避免使用 estimator_params?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently working on recursive feature elimination (RFECV) within a grid search (GridSearchCV) for tree based methods using scikit-learn. To do this, I'm using the current dev version on GitHub (0.17) which allows RFECV to use feature importance from the tree methods to select features to discard.

为清楚起见,这意味着:

For clarity this means:

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

这段代码目前运行良好 - 但我收到了关于使用 estimator_params 的折旧警告.这是当前的代码:

This code is working fine at the moment - but I'm getting a depreciation warning about using estimator_params. Here is the current code:

# 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_

这里是完整的折旧警告:

Here is the depreciation warning in full:

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 传递给估算器的情况下获得相同的结果?

How I would be able to achieve the same result without using estimator_params in GridSearchCV to pass the parameters through RFECV to the estimator?

推荐答案

这可以解决您的问题:

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())

这篇关于使用嵌套在 GridSearchCV 中的 RFECV 时,如何避免使用 estimator_params?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆