繁体   English   中英

Scikit学习SelectFromModel-实际获取基础预测变量的特征重要性得分

[英]Scikit-learn SelectFromModel - actually obtain the feature importance scores of underlying predictor

我正在尝试评估手边分类任务的功能重要性。 对我来说重要的是获取代表每个功能重要性的特定数字,而不仅仅是“选择最重要的X功能”。

显而易见的选择是使用基于树的方法,这些方法提供不错的feature_importances_方法来获取每个功能的重要性。 但是我对基于树的分类器的结果不满意。 我了解到SelectFromModel方法能够基于重要性得分消除不重要的功能,并且也成功地针对SVM或线性模型做到了这一点。

我想知道,有什么方法可以从SelectFromModel获得每个功能的特定重要性得分,而不仅仅是获得最重要的功能列表吗?

查看GitHub 源代码 ,我发现了这段代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM