2

我目前正在使用 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 传递给估计器,我如何能够获得相同的结果?

4

1 回答 1

2

这解决了你的问题:

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())
于 2016-02-22T18:00:10.933 回答