【问题标题】:Getting features in RFECV scikit-learn在 RFECV scikit-learn 中获取功能
【发布时间】:2018-10-27 11:09:22
【问题描述】:

受此启发:http://scikit-learn.org/stable/auto_examples/feature_selection/plot_rfe_with_cross_validation.html#sphx-glr-auto-examples-feature-selection-plot-rfe-with-cross-validation-py

我想知道是否有任何方法可以获取特定分数的功能:

在这种情况下,我想知道,当#Features = 10 时,选择哪 10 个特征会达到峰值。

有什么想法吗?

编辑:

这是用于获取该图的代码:

from sklearn.feature_selection import RFECV
from sklearn.model_selection import KFold,StratifiedKFold #for K-fold cross validation
from sklearn.ensemble import RandomForestClassifier #Random Forest

# The "accuracy" scoring is proportional to the number of correct classifications
#kfold = StratifiedKFold(n_splits=10, random_state=1) # k=10, split the data into 10 equal parts
model_Linear_SVM=svm.SVC(kernel='linear', probability=True)
rfecv = RFECV(estimator=model_Linear_SVM, step=1, cv=kfold,scoring='accuracy')   #5-fold cross-validation
rfecv = rfecv.fit(X, y)

print('Optimal number of features :', rfecv.n_features_)
print('Best features :', X.columns[rfecv.support_])
print('Original features :', X.columns)
plt.figure()
plt.xlabel("Number of features selected")
plt.ylabel("Cross validation score \n of number of selected features")
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
plt.show()

【问题讨论】:

  • 能否请您添加生成此图的代码?
  • @VladislavGladkikh 添加了
  • 您首先需要通过在您可以找到的任何地方设置参数 random_state 来使该过程可重现。之后,您可以扩展 RFECV fit() 方法以打印每次的特征

标签: python scikit-learn cross-validation rfe


【解决方案1】:

首先,您可以使用

查看它选择的交叉验证分数最大的特征(在您的情况下,这对应于特征的数量 17 或 21,我从图中不确定)
rfecv.support_

rfecv.ranking_ 

然后您可以通过

计算所选特征的重要性(对于cv分数曲线的峰值)
np.absolute(rfecv.estimator_.coef_)

对于简单的估计器或

rfecv.estimator_.feature_importances_ 

如果您的估算器是某个集合,例如随机森林。

然后就可以在循环中将最不重要的特征一个一个去掉,剩下的特征集重新计算rfecv。

【讨论】:

    猜你喜欢
    • 2016-04-14
    • 2015-06-22
    • 2020-05-31
    • 2015-08-13
    • 2017-07-18
    • 2015-12-09
    • 2016-09-26
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多