【问题标题】:Score of RFECV() in python scikit-learnpython scikit-learn 中 RFECV() 的得分
【发布时间】:2016-04-14 16:06:08
【问题描述】:

Scikit-learn 库支持递归特征消除 (RFE) 及其交叉验证版本 (RFECV)。 RFECV 对我来说非常有用,它选择了小特征,但我想知道 RFE 的交叉验证是如何完成的。

RFE 是减少最不重要的特征特征的方法。所以我认为 RFECV 会计算交叉验证分数,将特征 1 逐一删除。

但是如果使用交叉验证,我认为每个折叠都会选择其他最不重要的特征,因为数据不同。 有人知道如何在 RFECV 中删除功能吗?

【问题讨论】:

    标签: python scikit-learn cross-validation


    【解决方案1】:

    对特征数量进行交叉验证。每次 CV 迭代都会更新每个移除特征数量的分数。

    然后,它会根据分数选择要保留的特征数量 n_features_to_select,并在完整数据集上使用 RFE,仅保留 n_features_to_select 特征。

    来自source:

    for n, (train, test) in enumerate(cv):
        X_train, y_train = _safe_split(self.estimator, X, y, train)
        X_test, y_test = _safe_split(self.estimator, X, y, test, train)
    
        rfe = RFE(estimator=self.estimator,
                  n_features_to_select=n_features_to_select,
                  step=self.step, estimator_params=self.estimator_params,
                  verbose=self.verbose - 1)
    
        rfe._fit(X_train, y_train, lambda estimator, features:
                 _score(estimator, X_test[:, features], y_test, scorer))
        scores.append(np.array(rfe.scores_[::-1]).reshape(1, -1))
    scores = np.sum(np.concatenate(scores, 0), 0)
    # The index in 'scores' when 'n_features' features are selected
    n_feature_index = np.ceil((n_features - n_features_to_select) /
                              float(self.step))
    n_features_to_select = max(n_features_to_select,
                               n_features - ((n_feature_index -
                                             np.argmax(scores)) *
                                             self.step))
    # Re-execute an elimination with best_k over the whole set
    rfe = RFE(estimator=self.estimator,
              n_features_to_select=n_features_to_select,
              step=self.step, estimator_params=self.estimator_params)
    rfe.fit(X, y)
    

    【讨论】:

    • 非常感谢。但我不明白“对特征数量进行交叉验证”的含义。我想知道的是‘如何安排淘汰的顺序。据我了解代码,首先 RFE 在整个数据集排序上运行。 (所以只做 1 次测试来进行排序)然后为每个交叉验证测试分数,然后使用该顺序删除 n 个特征,依此类推。我说的对吗?
    • @z991 不完全是。 RFE 在每个 CV 折叠上运行,我们保留所有 CV 折叠中每个特征得分的平均值。然后,我们使用平均分数来计算要删除的特征数量,使用整个数据集删除该数量的特征。
    • 现在我理解了流程。非常感谢!
    猜你喜欢
    • 2018-10-27
    • 2020-05-31
    • 2015-06-22
    • 2017-07-18
    • 2016-08-31
    • 2016-09-26
    • 2016-08-26
    • 2018-04-02
    • 2020-10-25
    相关资源
    最近更新 更多