AttributeError: 'numpy.ndarray' object has no attribute

Question:

I am applying selectKbest feature selection technique but it is giving me the following error:

AttributeError: 'numpy.ndarray' object has no attribute 'columns'

here is the portion of my code:

bestfeatures = SelectKBest(score_func=chi2, k=10)
fit = bestfeatures.fit(X,y)
dfscores = pd.DataFrame(fit.scores_)
dfcolumns = pd.DataFrame(X.columns)

(Note: the original data is in CSV format)

Asked By: Piyush Choudhary

||

Answers:

X is a numpy array, and you can only call the .columns method on a dataframe.
You need to convert to a dataframe first, then call the method.

dfcolumns = pd.DataFrame(X).columns
Answered By: mrw
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.