【问题标题】:Automatic Feature Selection from a 3D numpy array using SelectKBest使用 SelectKBest 从 3D numpy 数组中自动选择特征
【发布时间】:2022-08-16 19:17:37
【问题描述】:

我是机器学习的新手,我正在处理一个相当复杂的问题。我有一个名为 \"psd_data\" 的 3D numpy 数组,其中包含来自执行运动图像试验的人类受试者的 EEG 数据。该数组的大小为 (240, 16, 129),代表(试验、通道、PSD 特征)。我还有一个名为标签的一维 numpy 数组,其中包含每个试验的标签,大小为 (240,)。

我需要自动执行特征选择然后分类,到目前为止我在特征选择方面遇到了麻烦。我试过这个:

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

但我收到一个错误:

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

您对如何正确操作 3D 数组 \"psd_data\" 以获得有用的结果有任何建议吗?

    标签: python feature-selection array-broadcasting


    【解决方案1】:

    对我有用的内容如下:

    #Reduce features
    from sklearn.feature_selection import SelectKBest
    from sklearn.feature_selection import mutual_info_classif
    topK = 20
    SKB = SelectKBest(mutual_info_classif, k=topK) 
    
    num_instances, num_time_steps, num_features = train_data.shape
    train_data = np.reshape(train_data, newshape=[-1, num_features])
    new_trClass = np.resize(y_train, num_time_steps*num_instances)
    train_data_skb = SKB.fit_transform(train_data, new_trClass)
    train_data_skb = np.reshape(train_data_skb, newshape=(num_instances, num_time_steps, topK))
    
    num_instances, num_time_steps, num_features = test_data.shape
    test_data = np.reshape(test_data, newshape=(-1, num_features))
    test_data_skb = SKB.transform(test_data)
    test_data_skb = np.reshape(test_data_skb, newshape=(num_instances, num_time_steps, topK))
    
    feat_indices = SKB.get_support()
    

    简单地说,您需要重塑您的阵列以匹配 2-dimesions。 希望这可以帮助。

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 2020-07-30
      • 2019-06-30
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多