我使用了 eli5
来执行特征重要性的置换过程。在文档中,有一些解释和一个小例子,但不够清晰。
我正在使用 sklearn SVC
模型进行分类问题。
我的问题是: 这些权重是指当特定特征被打乱时准确率的变化(减少/增加)还是这些特征的 SVC 权重?
在这篇 Medium 文章中,作者指出这些值显示了“该特征重新洗牌后模型性能的降低”。但我不确定是否确实如此。
小例子:
from sklearn import datasetsimport eli5from eli5.sklearn import PermutationImportancefrom sklearn.svm import SVC, SVR# import some data to play withiris = datasets.load_iris()X = iris.data[:, :2]y = iris.targetclf = SVC(kernel='linear')perms = PermutationImportance(clf, n_iter=1000, cv=10, scoring='accuracy').fit(X, y)print(perms.feature_importances_)print(perms.feature_importances_std_)[0.38117333 0.16214 ][0.1349115 0.11182505]eli5.show_weights(perms)
回答:
我进行了深入的研究。在查看源代码后,我对使用了 cv
且不是 prefit
或 None
的情况有了以下理解。我在应用中使用了 K-Folds 方案。我也使用了 SVC 模型,因此,score
在这种情况下是准确率。
通过查看 PermutationImportance
对象的 fit
方法,可以看到计算了 _cv_scores_importances
(https://github.com/TeamHG-Memex/eli5/blob/master/eli5/sklearn/permutation_importance.py#L202)。使用指定的交叉验证方案,并使用测试数据返回 base_scores, feature_importances
(函数:_get_score_importances
在 _cv_scores_importances
内)。
通过查看 get_score_importances
函数(https://github.com/TeamHG-Memex/eli5/blob/master/eli5/permutation_importance.py#L55),我们可以看到 base_score
是未打乱数据的得分,而 feature_importances
(在那里被称为 scores_decreases
)被定义为未打乱得分减去打乱得分(见 https://github.com/TeamHG-Memex/eli5/blob/master/eli5/permutation_importance.py#L93)
最后,误差(feature_importances_std_
)是上述 feature_importances
的标准差(https://github.com/TeamHG-Memex/eli5/blob/master/eli5/sklearn/permutation_importance.py#L209),而 feature_importances_
是上述 feature_importances
的平均值(未打乱得分减去打乱得分)。