4

I met a question related to this old one: The easiest way for getting feature names after running SelectKBest in Scikit Learn

When trying to use "get_support()" to get the selected features, I got the error message:

numpy.ndarray' object has no attribute 'get_support

I would greatly appreciate your kind help!

Jeff

JeffZheng
  • 1,277
  • 1
  • 10
  • 13

2 Answers2

5

Without doing fitting you cannot get support. You need to do the fitting so that the selector can analyze the data, and then call get_support() on the selector, not the output of fit_transform()

Currently you are doing something like:

selector = SelectKBest()

#fit_transform returns the data after selecting the best features
new_data = selector.fit_transform(old_data, labels)

#so you are trying to access get_support() on new data, which is not possible
new_data.get_support()

After you call fit() or fit_transform(), do this:

# get_support is a method of SelectKBest class
selector.get_support()
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
  • 1
    Thanks for your detailed explanation. What you said was exactly what happened. My own answer is definitely not as clear as what you said. Thanks! – JeffZheng May 22 '18 at 03:57
1

I think I found out the reason why I got the errors. I used "get_support()" on the results after fit() or fit_transform(), which led to the error message.

I should have used the "get_support()" on the selector itself (but still need to use selector to do fit() or fit_transform() first).

Thanks!

Jeff

JeffZheng
  • 1,277
  • 1
  • 10
  • 13