【问题标题】:Visualize feature selection in descending order with SelectKBest使用 SelectKBest 按降序可视化特征选择
【发布时间】:2017-03-07 19:35:21
【问题描述】:

我想将特征选择的结果可视化为按降序排列的条形图。(仅前 10 个特征)如何使用 matplotlib 做到这一点?在下面你可以看到代码。

filename_train = 'C:\Users\x.x\workspace\Dataset\x.csv'
names = ['a', 'b', 'c', 'd', 'e' ...........]
df_train = pd.read_csv(filename_train, names=names)
array = df_train.values
X = array[:,0:68]  
Y = df_train['RUL'].values

import numpy as np
from sklearn.feature_selection import SelectKBest

# feature extraction
test = SelectKBest(score_func=f_regression, k=10)
fit = test.fit(X, Y)

# summarize scores
np.set_printoptions(precision=2)
print(fit.scores_)

【问题讨论】:

    标签: python python-2.7 numpy matplotlib scikit-learn


    【解决方案1】:

    您必须先获取分数的索引,然后才能绘图:

    # Get the indices sorted by most important to least important
    indices = np.argsort(fit.scores_)[::-1]
    
    # To get your top 10 feature names
    features = []
    for i in range(10):
        features.append(your_data.columns[indices[i]])
    
    # Now plot
    plt.figure()
    plt.bar(features, fit.scores_[indices[range(10)]], color='r', align='center')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2022-08-16
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2022-01-16
      • 2018-05-30
      • 1970-01-01
      相关资源
      最近更新 更多