2016-01-10 477 views
1

Scikit学习库支持递归特征消除(RFE)和交叉验证的版本(RFECV)。 RFECV对我来说非常有用,它选择小功能,但我想知道RFE的交叉验证是如何完成的。得分RFECV()的scikit学习

RFE的方式,以减少至少重要的保护功能。因此,我认为RFECV将计算交叉验证得分1.

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

回答

3

的交叉验证在多个特征完成。每个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) 
+0

非常感谢你。但我不明白'交叉验证多个功能'的含义。我想知道的是'如何让消除秩序。就我所了解的代码而言,首先RFE运行整个数据集排序。 (因此,只有1测试用于制造顺序进行)。然后得分是针对每个交叉验证测试,然后使用该顺序删除n个特征,依此类推。我对吗? – z991

+1

@ z991不完全。 RFE在每个CV倍数上运行,并且我们保留所有CV倍数中每个特征评分的平均值。然后,我们使用平均分计算要移除的要素数量,使用整个数据集移除该要素数量。 –

+0

现在我明白了这个流程。非常感谢你! – z991