I am selecting best features and then doing grid search. When finished, I want to print the best features that have been selected.
JavaScript
x
25
25
1
knn = KNeighborsRegressor()
2
sfs = SequentialFeatureSelector(knn,
3
scoring=custom_scorer,
4
n_features_to_select='auto',
5
tol=.01,
6
n_jobs=-1)
7
8
pipe = Pipeline([('sfs', sfs),
9
('knn', knn)])
10
11
param_grid = {
12
'sfs__estimator__n_neighbors': [4, 5, 6],
13
'sfs__estimator__weights': ['uniform', 'distance'],
14
'sfs__estimator__algorithm': ['ball_tree', 'kd_tree', 'brute'],
15
'sfs__estimator__leaf_size': [15,30,60],
16
}
17
18
gs = GridSearchCV(estimator=pipe,
19
param_grid=param_grid,
20
scoring=custom_scorer,
21
n_jobs=-1,
22
cv=cv,
23
refit=False)
24
gs = gs.fit(X, y)
25
When trying to print with
JavaScript
1
2
1
pipe.named_steps["sfs"].support_
2
I get the following error
JavaScript
1
2
1
'SequentialFeatureSelector' object has no attribute 'support_'
2
Ive also tried
JavaScript
1
2
1
pipe[1].support_
2
but have gotten an error.
Advertisement
Answer
The grid search clones its estimator before fitting, so your pipe
itself remains unfitted. You can access the refitted-to-best-hyperparameters best_estimator_
:
JavaScript
1
2
1
gs.best_estimator_.named_steps["sfs"].support_
2