python scikit-learn 中 RFECV() 的分数

z99*_*991 1 python scikit-learn cross-validation

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

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

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

toa*_*kes 5

交叉验证是在功能数量上完成的。每次 CV 迭代都会更新每个移除特征数量的分数。

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

来源

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)
Run Code Online (Sandbox Code Playgroud)