[英]Score of RFECV() in python scikit-learn
Scikit-learn 库支持递归特征消除(RFE)及其交叉验证版本(RFECV)。 RFECV 对我来说非常有用,它可以选择小特征,但我想知道 RFE 的交叉验证是如何完成的。
RFE 是减少最不重要特征的方法。 所以我认为 RFECV 将计算交叉验证分数,逐一删除特征。
但是如果使用交叉验证,我认为每个折叠都会选择其他最不重要的特征,因为数据是不同的。 有人知道如何在 RFECV 中删除功能吗?
交叉验证是在特征数量上完成的。 每次 CV 迭代都会更新每个已删除特征的分数。
然后,它根据分数选择要保留的n_features_to_select
个特征,并在完整数据集上使用 RFE,仅保留n_features_to_select
特征。
从来源:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.