python - 如何在 sklearn 中使用 SVC 运行 RFECV

标签 python machine-learning scikit-learn svm

我正在尝试使用 GridSearchCV 执行递归特征消除与交叉验证 (RFECV),如下所示,使用 SVC 作为分类器。

我的代码如下。

X = df[my_features]
y = df['gold_standard']

x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=0)

k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=0)

clf = SVC(class_weight="balanced")
rfecv = RFECV(estimator=clf, step=1, cv=k_fold, scoring='roc_auc')

param_grid = {'estimator__C': [0.001, 0.01, 0.1, 0.25, 0.5, 0.75, 1.0, 10.0, 100.0, 1000.0],
              'estimator__gamma': [0.001, 0.01, 0.1, 1.0, 2.0, 3.0, 10.0, 100.0, 1000.0],
              'estimator__kernel':('rbf', 'sigmoid', 'poly')
       }

CV_rfc = GridSearchCV(estimator=rfecv, param_grid=param_grid, cv= k_fold, scoring = 'roc_auc', verbose=10)

CV_rfc.fit(x_train, y_train)

但是,我收到一条错误消息:RuntimeError: The classifier does not expose "coef_"or "feature_importances_"attributes

有没有办法解决这个错误?如果不是,我可以与 SVC 一起使用的其他特征选择技术是什么?

如果需要,我很乐意提供更多详细信息。

最佳答案

要查看更多功能选择实现,您可以查看:

https://scikit-learn.org/stable/modules/classes.html#module-sklearn.feature_selection

例如,在下一个链接中,他们使用具有 k-best 特征选择和 svc 的 PCA。

https://scikit-learn.org/stable/auto_examples/compose/plot_feature_union.html#sphx-glr-auto-examples-compose-plot-feature-union-py

一个使用示例是,为了更简单起见,修改了前面的链接:

iris = load_iris()

X, y = iris.data, iris.target

# Maybe some original features where good, too?
selection = SelectKBest()

# Build SVC
svm = SVC(kernel="linear")

# Do grid search over k, n_components and C:

pipeline = Pipeline([("features", selection), ("svm", svm)])

param_grid = dict(features__k=[1, 2],
                  svm__C=[0.1, 1, 10])

grid_search = GridSearchCV(pipeline, param_grid=param_grid, cv=5, verbose=10)
grid_search.fit(X, y)
print(grid_search.best_estimator_)

关于python - 如何在 sklearn 中使用 SVC 运行 RFECV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649352/

相关文章:

python - 为什么我不能得到与 GridSearchCV 相同的结果?

python - 具有哈希向量化器精度的 Scikit SGD 分类器停留在 58%

python - 输入预测/估计给定变量趋势所需的数据

python - TensorFlow 对象检测 API : classification weights initialization when changing number of classes at training using pre-trained models

python - 如何从序列中的特定点开始迭代(Python)

Python在列表中找到n个连续的数字

python - sklearn 逻辑回归 - 使用外部测试数据的自定义输入进行预测

python - 如何让 SVM 很好地处理 scikit-learn 中的缺失数据?

python - 将 Recaptcha 表单添加到我的 Django 登录页面

python - 练习 41 : Learning To Speak Object Oriented