Mic*_*Das 6 python machine-learning feature-extraction feature-selection
尝试使用 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)
Run Code Online (Sandbox Code Playgroud)
我使用了乳腺癌数据集并做了一些小的调整,例如添加标题、特征缩放和缺失值处理。
我遇到了同样的错误。我可以追溯到 pandas IndexEngine,但无法弄清楚到底出了什么问题。您可以通过将数据帧转换为 numpy 数组来运行模型,例如:
feat_selector.fit(x.values, y.values)
Run Code Online (Sandbox Code Playgroud)
此外,您之前将您的xand yas命名为X和Y该行,但我确信如果您实际使用这样的代码,则会显示为不同的错误。
| 归档时间: |
|
| 查看次数: |
5096 次 |
| 最近记录: |