【发布时间】:2020-10-13 16:10:39
【问题描述】:
在Sklearn#15075 中提出了实现此功能的建议,但与此同时,建议将eli5 作为解决方案。但是,我不确定我是否以正确的方式使用它。这是我的代码:
from sklearn.datasets import make_friedman1
from sklearn.feature_selection import RFECV
from sklearn.svm import SVR
import eli5
X, y = make_friedman1(n_samples=50, n_features=10, random_state=0)
estimator = SVR(kernel="linear")
perm = eli5.sklearn.PermutationImportance(estimator, scoring='r2', n_iter=10, random_state=42, cv=3)
selector = RFECV(perm, step=1, min_features_to_select=1, scoring='r2', cv=3)
selector = selector.fit(X, y)
selector.ranking_
#eli5.show_weights(perm) # fails: AttributeError: 'PermutationImportance' object has no attribute 'feature_importances_'
有几个问题:
-
我不确定我是否以正确的方式使用交叉验证。
PermutationImportance是使用cv来验证验证集的重要性,或者交叉验证应该只使用RFECV? (在示例中,我在两种情况下都使用了cv=3,但不确定这样做是否正确) -
如果我取消注释最后一行,我会得到一个
AttributeError: 'PermutationImportance' ...这是因为我适合使用RFECV?我正在做的类似于这里的最后一个 sn-p:https://eli5.readthedocs.io/en/latest/blackbox/permutation_importance.html -
作为一个不太重要的问题,当我在
eli5.sklearn.PermutationImportance中设置cv时,这会给我一个警告:
.../lib/python3.8/site-packages/sklearn/utils/validation.py:68: FutureWarning: Pass classifier=False as keyword args. From version 0.25 passing these as positional arguments will result in an error warnings.warn("Pass {} as keyword args. From version 0.25 "
整个过程有点模糊。 有没有办法直接在 Sklearn 中进行操作? 例如通过添加feature_importances 属性?
【问题讨论】:
标签: python scikit-learn cross-validation feature-selection eli5