Chris Rigano
Chris Rigano

Reputation: 701

How do I get feature names when I select only the 20% most important features, using a chi2 test?

Here is a code snipit I found in the book "Learning scikit-learn:

from sklearn import feature_selection
fs = feature_selection.SelectPercentile(feature_selection.chi2, percentile=20)
X_train_fs = fs.fit_transform(X_train, y_train)

What I want is an array of the feature names and relative importance.

I found this code in Stack (show feature names after feature selection) but don't totally understand it and have not been able to get it to work.

I would appreciate suggestions on how to get, store and print the features. Thanks for your assistance.

Upvotes: 2

Views: 834

Answers (1)

Chris Rigano
Chris Rigano

Reputation: 701

Here is the code thannks to EdChum

..... read data into numpy arrays

.... get feature names and place into feature_names_

from sklearn import feature_selection
fs = feature_selection.SelectPercentile(feature_selection.chi2, percentile=20)
X_train_fs = fs.fit_transform(plants_X, plants_y)

print feature_names, '\n', fs.scores_

Upvotes: 1

Related Questions