簡體   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