Is there any way by which we can list out all the columns (indices of the columns) chosen by chi2 feature selector using selectkbest in sklearn?

Viewed 30

I was trying to figure out the features given by sklearn chi2 method, and I want to find out the columns given by it. It does give out the columns, but what I want is the indices of the columns, where they're located in the original X.

# load libraries
from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectKBest, chi2

# Load Data
# load iris data
iris = load_iris()

# create features and target
X = iris.data
y = iris.target

# convert to categorical data by converting data to integers
X = X.astype(int)

# Compare Chi-Squared Statistics
# select two features with highest chi-squared statistics
chi2_selector = SelectKBest(chi2, k=2)
X_kbest = chi2_selector.fit_transform(X, y)

In this above example, feature selector choses 3rd and 4th column, but it's seen by observation. If we've large dataset of 1000 rows, and we want to find out 100 best columns, how can we do that? If anyone can help me in figuring out, it would be great in understanding this tool.

Use

filter = chi2_selector.get_support()
0 Answers
Related