0
$\begingroup$

This question probably has a simple answer to it, so I will get to the point...

How do I retrieve the names of the columns from applying a wrapper method in feature selection?

Code I have used:

from mlxtend.feature_selection import SequentialFeatureSelector as SFS

X = df[['A','B','C','D']].values
y = df[['F']].values

classifier = KNeighborsClassifier(n_neighbors=7) 
code = SFS(classifier,
           k_features=5, 
           forward=True, 
           floating=False, 
           verbose=8,
           scoring='accuracy'
)

code.fit(X, y)
code.k_feature_names_ 

Output:

Features: 3/3 -- score: 0.78

('1' '2', '3')

Therefore, how do I retrieve the names of the features ('1' '2', '3')?

$\endgroup$
3
  • $\begingroup$ What is SFS? The parameters look like the SequentialFeatureSelector from the mlxtend package, not sklearn's, which will be an important difference. $\endgroup$
    – Ben Reiniger
    Jan 12, 2021 at 16:54
  • $\begingroup$ Apologies, it is the SequentialFeatureSelector from the mlxtend package $\endgroup$
    – user110236
    Jan 12, 2021 at 16:55
  • $\begingroup$ Your edit to the code didn't make much sense. I've edited it some more, please check that it represents your intent. $\endgroup$
    – Ben Reiniger
    Jan 12, 2021 at 19:58

2 Answers 2

0
$\begingroup$

Based on this scikit-learn documentation, you can get a boolean mask (in the same order) of the input features, via the get_support method:

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks. Though, when I try this, AttributeError: 'SequentialFeatureSelector' object has no attribute 'get_support' $\endgroup$
    – user110236
    Jan 12, 2021 at 17:07
  • $\begingroup$ that's because my code is not from Sebastian Raschka's mlxtend, but from a dev sklearn version; to use it, follow this scikit-learn.org/stable/developers/…; other option you have is to ask him via its github page if you want to use mlxtend, he is usually really nice answering your questions $\endgroup$
    – German C M
    Jan 12, 2021 at 17:11
  • $\begingroup$ Thanks for that $\endgroup$
    – user110236
    Jan 12, 2021 at 17:14
0
$\begingroup$

By using .values in your definition of X, you've converted to a numpy array and lost the column names. Just removing that, you'll provide a frame to SRS, and mlxtend will use the column names in the k_feature_names_ attribute; so that's probably the best approach.

There are two other approaches: one is to add the custom_feature_names parameter of mlxtend.SequentialFeatureSelector to replicate the dataframe column names, and the other is to use the k_feature_idx_ parameter to slice a list of your columns. Both of these require you saving the column names somewhere before casting to numpy, so the first solution is generally easier.

See the first example, and the API documentation, on the mlxtend.SequentialFeatureSelector page.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.