从gridsearchCV中的RFECV中检索所选特征

时间:2017-12-14 18:29:05

标签: python scikit-learn feature-selection grid-search rfe

我想编写一个代码,网格搜索几个处理器和预处理器,但也可以编写不同的功能组合。我是通过在gridsearchCV中使用RFECV来做到这一点的。但是,这需要很长时间才能运行。因此,我改变了顺序。我进行了网格搜索,然后将其放入RFECV。现在,我想查看并打印最佳模型中实际选择的功能。我从这个网站尝试了几个解决方案,但没有一个有效。如何访问所选功能? grid_dem.get_support(indices=True)grid_dem.support_都不起作用。我得到了这个和其他类似的错误:AttributeError: 'RFECV' object has no attribute 'support_'

我的代码的相关部分是:

pipe = Pipeline([('preprocessing', StandardScaler()), ('rbf_svm', SVC(max_iter=1e6))])
param_grid ={'preprocessing': [StandardScaler(), MinMaxScaler(), Normalizer(), RobustScaler()],
     'rbf_svm__kernel': ['rbf', 'poly', 'linear', 'sigmoid'],
     'rbf_svm__C': np.logspace(-3,2,5), 'rbf_svm__gamma': np.logspace(-3,2,5)}

    # {'preprocessing': [StandardScaler(), MinMaxScaler(), Normalizer(), RobustScaler()],
    #  'rbf_svm': [LogisticRegression(max_iter=1e6)],
    #  'logisticregression__C': np.logspace(-3,2,5)}]

grid_dem = GridSearchCV(pipe, param_grid, cv=5,verbose=5,n_jobs=3)
grid_dem.fit(X_democrat_train,y_democrat_train)
grid_dem.score(X_democrat_test,y_democrat_test)
print(grid_dem.best_estimator_)
rfecv=RFECV(grid_dem, verbose=3)
print(rfecv)
print(rfecv.get_support(indices=True))
# rfecv=rfecv.fit_transform(X_democrat_train, y_democrat_train)
# print(rfecv.get_params())

正如你在前两行中看到的那样,我也试图改变X,但这也不起作用。

1 个答案:

答案 0 :(得分:1)

您已向gridSearch发送了管道。所以<!DOCTYPE html> <html> <body> <?php $numberOfSpins = 10000; $numberArray = array(); // Start table echo '<table> <tr>'; // print out the table headers for ($x = 0; $x < 37; $x++) echo '<th style="font-weight:bold; color:#09f;">'.$x.'</th>'; // Fill $numberArray with random numbers for($i=0; $i < $numberOfSpins; $i++) array_push($numberArray, mt_rand(0,36)); echo '</tr> <tr>'; // Count value frequency using PHP function array_count_value() $resultArray = array_count_values($numberArray); // Start from 0 since you are generating numbers from 0 to 36 for($i=0; $i < 37; $i++) { // array_count_values() returns an associative array (the key of // each array item is the value it was counting and the value is the // occurrence count; [key]->value). if (isset($resultArray[$i])) echo '<td>'.$resultArray[$i].'</td>'; else echo '<td>0</td>'; } echo '</tr> </table>'; ?> </body> </html> 将返回一个管道。但是best_estimator_仍然是GridSearchCV对象。很明显grid_dem不起作用。之后,您将此get_support()传递给grid_dem,但未在其上调用RFECV。这就是fit()无法使用的原因。

请更新您的代码:

support_