【问题标题】:Scikit-learn SelectFromModel - actually obtain the feature importance scores of underlying predictorScikit-learn SelectFromModel - 实际获取底层预测器的特征重要性分数
【发布时间】:2018-01-01 23:17:12
【问题描述】:

我试图估计手头分类任务的特征重要性。对我来说重要的是获得代表每个特征重要性的具体数字,而不仅仅是“选择最重要的 X 个特征”。

明显的选择是使用基于树的方法,它提供了很好的 feature_importances_ 方法来获得每个特征的重要性。但我对基于树的分类器的结果并不满意。我了解到,SelectFromModel 方法能够根据重要性得分消除不重要的特征,并且对于 SVM 或线性模型也成功地做到了。

我想知道,有没有什么方法可以从 SelectFromModel 中获取每个特征的特定重要性分数,而不仅仅是获取最重要特征的列表?

【问题讨论】:

    标签: python machine-learning scikit-learn feature-selection


    【解决方案1】:

    翻阅GitHubsource code,找到了这段代码:

    def _get_feature_importances(estimator):
        """Retrieve or aggregate feature importances from estimator"""
        importances = getattr(estimator, "feature_importances_", None)
    
        if importances is None and hasattr(estimator, "coef_"):
            if estimator.coef_.ndim == 1:
                importances = np.abs(estimator.coef_)
    
            else:
                importances = np.sum(np.abs(estimator.coef_), axis=0)
    
        elif importances is None:
            raise ValueError(
                "The underlying estimator %s has no `coef_` or "
                "`feature_importances_` attribute. Either pass a fitted estimator"
                " to SelectFromModel or call fit before calling transform."
                % estimator.__class__.__name__)
    
        return importances
    

    因此,如果您使用线性模型,代码只是使用模型系数作为“重要性分数”。

    您可以通过从传递给SelectFromModel 的估算器中提取coef_ 属性来做到这一点。

    例子:

    sfm = SelectFromModel(LassoCV(), 0.25)
    sfm.fit(X, y)
    print(sfm.estimator_.coef_)  # print "importance" scores
    

    【讨论】:

    • 谢谢,有道理。因此,对于线性模型,我可以将系数归一化以获得“相对”重要性。
    • 没问题!如果您计划尝试多个模型,然后从这些多个模型的归一化系数中建立相对重要性,我可以看到归一化的理由,但是如果您只使用一个模型,我看不出它是否重要你规范化与否。但是,我会将绝对值应用于系数,因为如果它们的大小相同,负系数与正系数一样重要,但我想你已经知道了 :)
    猜你喜欢
    • 2015-01-12
    • 2015-03-19
    • 2017-11-04
    • 2021-02-02
    • 2016-03-18
    • 2019-02-14
    • 2018-09-07
    • 2018-09-23
    • 2016-04-03
    相关资源
    最近更新 更多