python - 详尽的特征选择
问题描述
我正在尝试使用详尽的特征选择为我的模型选择最佳特征,但我得到了一个 IndexError 并且我一直在试图找出它。
X = train.columns.difference(['Customer_ID', 'total_claim_amount'])
y = train['total_claim_amount']
# exhaustive feature selection
from mlxtend.feature_selection import ExhaustiveFeatureSelector as EFS
from sklearn.ensemble import RandomForestRegressor
feature_selector = EFS(rf_model,min_features=2, max_features=23, scoring='accuracy', print_progress=True, cv=5)
feature_selector.fit(X, y)
我得到的错误是:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-45-6c2f49995b6c> in <module>
7 feature_selector = EFS(rf_model,min_features=2, max_features=23, scoring='accuracy', print_progress=True, cv=5)
8
----> 9 feature_selector.fit(X, y)
10
11 print('Best R2 score: %.2f' % feature_selector.best_score_ * (-1))
~\anaconda3\lib\site-packages\mlxtend\feature_selection\exhaustive_feature_selector.py in fit(self, X, y, custom_feature_names, groups, **fit_params)
225
226 if (not isinstance(self.max_features, int) or
--> 227 (self.max_features > X.shape[1] or self.max_features < 1)):
228 raise AttributeError('max_features must be'
229 ' smaller than %d and larger than 0' %
IndexError: tuple index out of range
解决方案
最后一条轨迹的行中唯一的元组索引是X.shape[1]
,这表明您X
只是一维的。确实,
train.columns.difference(...)
只给出列名列表,而不是仅限于这些列的数据框。你要
X = train[train.columns.difference(...)]
(或者可能train.drop(...)
更容易解析)。
推荐阅读
- java - 用于相同目的的 JDBC 和 Hibernate?
- javascript - 如何让我的关闭按钮在不同尺寸的屏幕上工作?
- html - 如何确保边框半径应用于图像而不是其容器?
- python - Python:重新排列方程以采用特定形式
- c# - 如何获取鼠标相对于世界的坐标?
- python - TypeError:“RelatedManager”对象不可迭代 - serializers.ListField()
- xamarin.forms - 在 Xamarin.forms 中从 Json 写入文本文件(或 PDF 文件)
- arrays - 如何在swift中过滤对象的3维数组
- python - 将文件从客户端上传到服务器时出现问题
- python - 如何更改 QCombobox 下拉项背景颜色的颜色