python - 使用 scikit-learn 进行 ANOVA 测试的交叉验证特征选择

标签 python scikit-learn feature-selection

我正在使用 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 方式使结果更可靠?

最佳答案

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()

示例输出:

Sample output for RFECV

引用链接:

编辑:使用方差分析检验的 CV 特征选择

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

关于python - 使用 scikit-learn 进行 ANOVA 测试的交叉验证特征选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45925011/

相关文章:

python - mock.call_count 的线程安全版本

java - 将 python dict 之类的字符串转换为 java hashmap

python - 来自源代码的 Scipy 安装问题?

python - 如何(安全)将 Python 对象发送到我的 Flask API?

python - 如何获取SOM(Self Organizing Maps)中的重要特征?

python - 主成分分析最重要的原始特征

python - 在 Python 中将 Excel 工作表从一个工作表复制到另一个工作表

python - 为什么 Pandas qcut 给我大小不等的垃圾箱?

machine-learning - 类型对象 'GridSearchCV' 没有属性 'cv_results_' ?

python - 网格搜索 SVM-anova 的超参数并在 Sklearn 中获取所选特征