使用scikitlearn进行方差分析的特征选择和交叉验证

2024-09-28 01:24:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用scikit学习如何进行功能选择。这是我的密码

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方式,这样结果更可靠?在


Tags: fromimport功能密码sklearnscikitfeaturefit
1条回答
网友
1楼 · 发布于 2024-09-28 01:24:26

scikitlearn有一种递归的特征消除和交叉验证的选择方法,称为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

参考链接:

编辑:使用变异数分析(ANOVA)进行特征选择

为了使用方差分析和交叉验证,您需要使用PipelineSelect Percentile和{a9}。基于给定的示例here,您可以结合这些技术来使用CV+Annova测试进行特征选择。在

相关问题 更多 >

    热门问题