I used SelectKBest(k=10).fit_transform(X, y) on a pandas DataFrame and got back a plain NumPy array with no column names. How do I find out which columns were kept?
Why this happens
By default, scikit-learn transformers return NumPy arrays, and arrays don't carry column names. The selector still remembers which columns it kept. It stores this as a boolean mask internally, so you only need to ask for it.
Answer 1: Use get_support() with your DataFrame columns
get_support() returns a boolean array with one entry per input feature. An entry is True if that feature was selected. Because the mask has the same order as your columns, you can use it to index X.columns.
from sklearn.feature_selection import SelectKBest, f_classif
selector = SelectKBest(score_func=f_classif, k=10)
selector.fit(X, y)
mask = selector.get_support() # array([True, False, True, ...])
selected_cols = X.columns[mask]
print(selected_cols.tolist())
This works in every scikit-learn version. It is also the most explicit option, which helps when you need the mask for something else, such as filtering a list of feature descriptions.
