GridSearchCV 不支持多类吗? [英] Does GridSearchCV not support multi-class?

查看:82
本文介绍了GridSearchCV 不支持多类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试根据此处的答案将 GridSearchCV 用于多类案例:

I tried to use GridSearchCV for multi-class case based on the answer from here:

加速预测

但是我得到了值错误,不支持多类格式.

如何在多类案例中使用这种方法?

How can I use this method for multi-class case?

以下代码来自上面链接中的答案.

Following code is from the answer in above link.

import numpy as np
from sklearn.datasets import make_classification
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import accuracy_score, recall_score, f1_score, roc_auc_score, make_scorer

X, y = make_classification(n_samples=3000, n_features=5, weights=[0.1, 0.9, 0.3])

pipe = make_pipeline(StandardScaler(), SVC(kernel='rbf', class_weight='auto'))

param_space = dict(svc__C=np.logspace(-5,0,5), svc__gamma=np.logspace(-2, 2, 10))

accuracy_score, recall_score, roc_auc_score
my_scorer = make_scorer(roc_auc_score, greater_is_better=True)

gscv = GridSearchCV(pipe, param_space, scoring=my_scorer)
gscv.fit(X, y)

print gscv.best_params_

推荐答案

来自 roc_auc_score:

注意:此实现仅限于标签指示符格式的二元分类任务或多标签分类任务.

Note: this implementation is restricted to the binary classification task or multilabel classification task in label indicator format.

标签指示符格式"是指每个标签值都表示为一个二进制列(而不是单个列中的唯一目标值).您不想为您的预测器这样做,因为它可能会导致非互斥的预测(即,预测案例 p1 的标签 2 和 4,或预测案例 p2 的无标签).

By "label indicator format", they mean each label value is represented as a binary column (rather than as a unique target value in a single column). You don't want to do that for your predictor, as it could result in non-mutually-exclusive predictions (i.e., predicting both label 2 and 4 for case p1, or predicting no labels for case p2).

选择或自定义实现为多类问题定义明确的评分函数,例如 F1 分数.我个人觉得知情更多比 F1 分数更有说服力,并且比 roc_auc_score 更容易推广到多类问题.

Pick or custom-implement a scoring function that is well-defined for the multiclass problem, such as F1 score. Personally I find informedness more convincing than F1 score, and easier to generalize to the multiclass problem than roc_auc_score.

这篇关于GridSearchCV 不支持多类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆