我有一段代码如下:
# transforming data to best 20 features
from sklearn.feature_selection import SelectKBest, chi2
import matplotlib.pyplot as plt
fs = SelectKBest(score_func=chi2, k=20)
fs.fit(X_train, y_train)
X_train = fs.transform(X_train)
X_test = fs.transform(X_test)
# what are scores for the features
for i in range(len(fs.scores_)):
print('Feature %d: %f' % (i, fs.scores_[i]))
# plot the scores
plt.bar([i for i in range(len(fs.scores_))], fs.scores_)
plt.show()
绘图给出了this picture中所示的输出,我想知道如何识别这些特性的实际特性名称,而不是“1-20”?我尝试了get_support(),但它给出了一个错误,因为我的数据是数组格式的,因为我使用了train_test_split
这些特征与X_序列阵列中的数据顺序相同。因此,为了获得功能名称,您应该在将X_序列转换为numpy数组之前提取它们。如果您正在使用名为
df
的熊猫数据帧,您可以做的是:相关问题 更多 >
编程相关推荐