【问题标题】:Getting TypeError: '(slice(None, None, None), array([0, 1, 2, 3, 4]))' is an invalid key获取 TypeError: '(slice(None, None, None), array([0, 1, 2, 3, 4]))' 是无效键
【发布时间】:2020-03-19 19:23:39
【问题描述】:

尝试使用 BorutaPy 进行特征选择。但是得到一个 TypeError: '(slice(None, None, None), array([0, 1, 2, 3, 4]))' 是一个无效的键。

from sklearn.ensemble import RandomForestClassifier
from boruta import BorutaPy
rf = RandomForestClassifier(n_jobs=-1, max_depth=4)

# define Boruta feature selection method
feat_selector = BorutaPy(rf, n_estimators='auto', verbose=2, random_state=1)

X = train_dt[['age', 'menopause', 'tumor_size', 'inv_nodes', 'node_caps',
   'deg_malig', 'breast', 'breast_quad', 'irradiat']]
Y = train_dt.label

# find all relevant features - 5 features should be selected
feat_selector.fit(x, y)

# check selected features - first 5 features are selected
feat_selector.support_

# check ranking of features
feat_selector.ranking_

# call transform() on X to filter it down to selected features
X_filtered = feat_selector.transform(X)

我使用了乳腺癌 dataset 并进行了一些小的调整,例如添加标题、特征缩放和缺失值处理。

【问题讨论】:

  • 你在哪一行得到错误?

标签: python machine-learning feature-extraction feature-selection


【解决方案1】:

我遇到了同样的错误。我可以将其追溯到 pandas IndexEngine,但无法弄清楚到底出了什么问题。您可以通过将数据帧转换为 numpy 数组来运行模型,例如:

feat_selector.fit(x.values, y.values)

此外,您在前一行将xy 命名为XY,但我敢肯定,如果您实际使用这样的代码,这会显示为不同的错误.

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2019-08-12
    • 2023-03-30
    • 2021-04-01
    • 2021-12-09
    • 2019-07-31
    相关资源
    最近更新 更多