'Automatic Feature Selection from a 3D numpy array using SelectKBest

I am new to Machine Learning and I am dealing with a quite complicated issue. I have a 3D numpy array called "psd_data" with EEG Data from a human subject that performed Motor Imagery trials. The array has size of (240, 16, 129) which stands for (trials, channels, PSD features). I also have an 1D numpy array called labels with the label of each trial and has a size of (240,).

I need to perform automatically feature selection and then classification and so far I am having trouble with the feature selection. I tried this:

from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2

X = psd_data  #independent columns
y = labels    #target - SelectKBest class to extract top 15 best features
bestfeatures = SelectKBest(score_func=chi2, k=15)
fit = bestfeatures.fit(X,y)
dfscores = pd.DataFrame(fit.scores_)
dfcolumns = pd.DataFrame(X.columns)
#concat two dataframes for better visualization 
featureScores = pd.concat([dfcolumns,dfscores],axis=1)
featureScores.columns = ['Specs','Score']  #naming the dataframe columns
print(featureScores.nlargest(15,'Score'))  #print 15 best features

But I am getting an error:

ValueError: Found array with dim 3. Estimator expected <= 2.

Do you have any suggestions on how to manipulate the 3D array "psd_data" correctly in order to get a useful result?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source