我正在尝试在 scikit-learn 中结合递归特征消除和网格搜索。正如您从下面的代码(有效)中看到的那样,我能够从网格搜索中获得最佳估计器,然后将该估计器传递给 RFECV。但是,我宁愿先做 RFECV,然后再做网格搜索。问题是,当我将选择器从 RFECV 传递给网格搜索时,它不接受它:
ValueError:估计器 RFECV 的参数引导程序无效
是否可以从 RFECV 获取选择器并将其直接传递给 RandomizedSearchCV,或者这在程序上不是正确的做法?
from sklearn.datasets import make_classification
from sklearn.feature_selection import RFECV
from sklearn.grid_search import GridSearchCV, RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
from scipy.stats import randint as sp_randint
# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000, n_features=25, n_informative=5, n_redundant=2, n_repeated=0, n_classes=8, n_clusters_per_class=1, random_state=0)
grid = {"max_depth": [3, None],
"min_samples_split": sp_randint(1, 11),
"min_samples_leaf": sp_randint(1, 11),
"bootstrap": [True, False],
"criterion": ["gini", "entropy"]}
estimator = RandomForestClassifierCoef()
clf = RandomizedSearchCV(estimator, param_distributions=grid, cv=7)
clf.fit(X, y)
estimator = clf.best_estimator_
selector = RFECV(estimator, step=1, cv=4)
selector.fit(X, y)
selector.grid_scores_