我想将特征选择的结果以降序形式显示为条形图。(仅限前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_)
答案 0 :(得分:2)
您必须先获得分数的索引,然后才能绘制:
# 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()