使用 SelectKBest 按降序可视化特征选择

mat*_*hew 2 python numpy matplotlib python-2.7 scikit-learn

我想以降序将特征选择的结果可视化为条形图。(仅前 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_)
Run Code Online (Sandbox Code Playgroud)

Scr*_*urr 5

您必须首先获得分数的索引,然后才能绘制:

# 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()
Run Code Online (Sandbox Code Playgroud)