Python 使用SelectKBest以降序显示要素选择
我想将特征选择的结果以降序显示为条形图(仅前10个特征)。如何使用matplotlib实现这一点?在下面您可以看到代码Python 使用SelectKBest以降序显示要素选择,python,python-2.7,numpy,matplotlib,scikit-learn,Python,Python 2.7,Numpy,Matplotlib,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[:,
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_)
您必须首先获得分数的指数,然后才能绘制:
# 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()
您必须首先获得分数的指数,然后才能绘制:
# 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()