简体   繁体   中英

Visualize feature selection in descending order with SelectKBest

I want to visualize the result of feature selection as a bar chart in descending order.(Only the top 10 features) How can I do this with matplotlib? In the following you can see the code.

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_)

You have to first get the indices for the scores first and then you can plot:

# 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM