Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"numpy.ndarray' object has no attribute 'get_support" error message after running SelectKBest in Scikit Learn

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

like image 740
JeffZheng Avatar asked Oct 26 '25 07:10

JeffZheng


2 Answers

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()
like image 145
Vivek Kumar Avatar answered Oct 28 '25 21:10

Vivek Kumar


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

like image 25
JeffZheng Avatar answered Oct 28 '25 20:10

JeffZheng