如何解决“RFECV对象没有support_属性”的属性错误?

我试图传递sklearn中的RFECV对象,并交叉验证分数以返回模型在所选特征和特征排名下的性能表现。

然而,我遇到了“RFECV对象没有support_属性”的错误,很可能是由于我没有将数据拟合到模型中。我需要一些帮助来确定在哪里拟合数据,以及如何确保测试数据集没有数据泄漏。

原始数据集是一个时间序列数据,因此我使用了时间序列分割方法进行分割。

from sklearn.datasets import make_classificationfrom sklearn.feature_selection import RFE, RFECVfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.model_selection import TimeSeriesSplit, cross_val_scorefrom sklearn import metricsfrom sklearn.metrics import balanced_accuracy_score, make_scorerX, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)# create pipelinerfecv_model = RFECV(estimator=DecisionTreeClassifier())model = DecisionTreeClassifier()pipeline = Pipeline(steps=[('s',rfecv_model),('m',model)])#make balanced scorerscorer = make_scorer(balanced_accuracy_score)# evaluate modelcv = TimeSeriesSplit(n_splits=3)n_scores = cross_val_score(pipeline, X, y, scoring=scorer, cv=cv)# report performanceprint('Balanced_Accuracy: %.3f (%.3f)' % (mean(n_scores), std(n_scores)))for i in range(X.shape[1]):    print('Column: %d, Selected %s, Rank: %.3f' % (i, rfecv_model.support_[i], rfecv_model.ranking_[i]))

这段代码来源于RFE教程这里


回答:

当您需要交叉验证的拟合模型时,我建议使用cross_validate

from sklearn import set_configset_config(print_changed_only=True)from sklearn.datasets import make_classificationfrom sklearn.feature_selection import RFE, RFECVfrom sklearn.tree import DecisionTreeClassifierfrom sklearn.model_selection import TimeSeriesSplit, cross_validatefrom sklearn import metricsfrom sklearn.metrics import balanced_accuracy_score, make_scorerfrom sklearn.pipeline import PipelineX, y = make_classification(    n_samples=1000, n_features=10, n_informative=5, n_redundant=5, random_state=1)# create pipelinerfecv_model = RFECV(estimator=DecisionTreeClassifier())model = DecisionTreeClassifier()pipeline = Pipeline(steps=[('s', rfecv_model), ('m', model)])# make balanced scorerscorer = make_scorer(balanced_accuracy_score)# evaluate modelcv = TimeSeriesSplit(n_splits=3)result = cross_validate(pipeline, X, y, scoring=scorer,                          cv=cv, return_estimator=True)

结果

{'fit_time': array([0.07009673, 0.09101987, 0.11680794]), 'score_time': array([0.00072193, 0.00065613, 0.00060487]), 'estimator': (Pipeline(steps=[('s', RFECV(estimator=DecisionTreeClassifier())),                  ('m', DecisionTreeClassifier())]),  Pipeline(steps=[('s', RFECV(estimator=DecisionTreeClassifier())),                  ('m', DecisionTreeClassifier())]),  Pipeline(steps=[('s', RFECV(estimator=DecisionTreeClassifier())),                  ('m', DecisionTreeClassifier())])), 'test_score': array([0.812     , 0.83170092, 0.8510502 ])}

现在让我们逐个查看cv的每次迭代中的特征选择器。

for iter, pipe in enumerate(result['estimator']):    print(f'迭代次数: {iter}')    for i in range(X.shape[1]):        print('列: %d, 选择: %s, 排名: %d' %            (i, pipe['s'].support_[i], pipe['s'].ranking_[i]))# 输出迭代次数: 0列: 0, 选择: False, 排名: 4列: 1, 选择: True, 排名: 1列: 2, 选择: True, 排名: 1列: 3, 选择: True, 排名: 1列: 4, 选择: False, 排名: 3列: 5, 选择: False, 排名: 5列: 6, 选择: True, 排名: 1列: 7, 选择: True, 排名: 1列: 8, 选择: True, 排名: 1列: 9, 选择: False, 排名: 2迭代次数: 1列: 0, 选择: False, 排名: 2列: 1, 选择: False, 排名: 4列: 2, 选择: True, 排名: 1列: 3, 选择: True, 排名: 1列: 4, 选择: True, 排名: 1列: 5, 选择: False, 排名: 6列: 6, 选择: True, 排名: 1列: 7, 选择: False, 排名: 5列: 8, 选择: True, 排名: 1列: 9, 选择: False, 排名: 3迭代次数: 2列: 0, 选择: True, 排名: 1列: 1, 选择: False, 排名: 4列: 2, 选择: True, 排名: 1列: 3, 选择: True, 排名: 1列: 4, 选择: True, 排名: 1列: 5, 选择: False, 排名: 3列: 6, 选择: False, 排名: 2列: 7, 选择: False, 排名: 5列: 8, 选择: True, 排名: 1列: 9, 选择: True, 排名: 1

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注