从数据框中标识要素名称

2024-09-28 01:33:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一段代码如下:

# 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


Tags: theintestimportforlentransformrange
1条回答
网友
1楼 · 发布于 2024-09-28 01:33:44

这些特征与X_序列阵列中的数据顺序相同。因此,为了获得功能名称,您应该在将X_序列转换为numpy数组之前提取它们。如果您正在使用名为df的熊猫数据帧,您可以做的是:

for i in range(len(fs.scores_)):
    print(f'Feature {df.columns[i]}: {fs.scores_[i]}')

相关问题 更多 >

    热门问题