【问题标题】:Feature Selection: AttributeError when trying to see which columns have been retained功能选择:尝试查看保留了哪些列时出现 AttributeError
【发布时间】:2021-04-16 12:06:13
【问题描述】:

尝试将特征选择应用于我的分类模型时出现错误。我认为这个错误很简单,很容易解决,但我不确定如何解决。

这是我的代码:

X = df[['S_LENGTH', 'S_WIDTH', 'P_LENGTH', 'P_WIDTH']].values
y = df['SPECIES'].values

feature_select = SelectKBest(f_classif, k=20).fit(X, y)

但是当我尝试执行此操作以查看保留的功能时...

feature_select = X.columns[selection.get_support()]
print(features)

...我收到此错误:

AttributeError: 'numpy.ndarray' object has no attribute 'columns'

非常感谢任何有关如何修复此错误的帮助。

谢谢!

【问题讨论】:

    标签: python machine-learning scikit-learn feature-selection sklearn-pandas


    【解决方案1】:

    当您定义 X 时,您会从 pandas DataFrame df 中获取值。这些值为numpy.ndarray,因此没有属性columns

    要修复,您需要更新您的行 feature_select = X.columns[selection.get_support()]

    应该是features = df.columns[feature_select.get_support()]

    【讨论】:

    • 嗨 - 感谢您的回复,但在执行您告诉我使用的代码时,我收到了NameError: name 'selection' is not defined
    • 嗨@Bono 我已经编辑了我的代码来解决这个问题。有两个问题 - 1. 您想打印features,因此我们将列子集分配给此变量。 2.selection不存在,应该读到feature_select
    猜你喜欢
    • 1970-01-01
    • 2013-08-08
    • 2018-11-29
    • 1970-01-01
    • 2015-09-28
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    相关资源
    最近更新 更多