【问题标题】:Feature Selection with Cross-validation using scikit-learn for ANOVA Test使用 scikit-learn 进行 ANOVA 测试的交叉验证特征选择
【发布时间】:2017-08-30 15:13:32
【问题描述】:

我正在使用 scikit-learn 进行特征选择。这是我的代码

from sklearn.feature_selection import GenericUnivariateSelect
from sklearn.feature_selection import f_classif


scores = GenericUnivariateSelect(f_classif, 'k_best').fit(features_pd, target_pd)

如何使用f_classif是CV的方式,结果更可靠?

【问题讨论】:

    标签: python scikit-learn feature-selection


    【解决方案1】:

    Scikit-learn 具有递归特征消除和交叉验证选择方法,称为RFECV。以下代码供参考,与on this link给出的示例类似。

    import matplotlib.pyplot as plt
    from sklearn.svm import SVC
    from sklearn.cross_validation import StratifiedKFold
    from sklearn.feature_selection import RFECV
    svc = SVC(kernel="linear")
    rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(labels, 50),
          scoring='precision')
    rfecv.fit(features, labels)
    print("Optimal number of features : %d" % rfecv.n_features_)
    print rfecv.support_
    features=features[:,rfecv.support_]
    # Plot number of features VS. cross-validation scores
    plt.figure()
    plt.xlabel("Number of features selected")
    plt.ylabel("Cross validation score (nb of correct classifications)")
    plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
    plt.show()
    

    样本输出:

    参考链接:

    编辑:使用方差分析测试的 CV 特征选择

    要使用 Anova 测试和交叉验证,您需要使用 Pipeline、Select Percentile 和 cross-val score。根据here 给出的示例,您可以结合这些技术使用 CV+Annova 测试进行特征选择。

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 2014-11-05
      • 2018-09-25
      • 2017-04-12
      • 2021-03-26
      • 2021-10-25
      • 2018-02-24
      • 2017-03-30
      相关资源
      最近更新 更多