GridSearchCV 不支持multi-class吗?
Does GridSearchCV not support multi-class?
我尝试根据此处的答案将 GridSearchCV 用于多 class 案例:
但是我得到了值错误,multiclass format is not supported.
如何将此方法用于多个 class 案例?
以下代码来自上面的答案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.
"label indicator format" 意味着每个标签值都表示为二进制列(而不是单个列中的唯一目标值)。您不想为您的预测器执行此操作,因为它可能会导致非互斥预测(即预测案例 p1 的标签 2 和 4,或预测案例 p2 的标签)。
选择或自定义实现针对多类问题定义明确的评分函数,例如 F1 score. Personally I find informedness 比 F1 分数更有说服力,并且比 roc_auc_score 更容易泛化到多类问题.
支持多class
可以设置scoring = f1.macro
的para,例如:
gsearch1 = GridSearchCV(estimator = est1, param_grid=params_test1, scoring='f1_macro', cv=5, n_jobs=-1)
或得分='roc_auc_ovr'
如果 classifier 默认为 y_true
和 y_pred
/[=15= 具有正确的 API,则自然支持多 class ].
否则,必须使用 make_scorer
等评分函数进行一些自定义。
对于 common metrics 就像 AUROC 对于 multi-classes,sklearn 提供了 'roc_auc_ovr'
,它实际上指的是
roc_auc_ovr_scorer = make_scorer(roc_auc_score, needs_proba=True,
multi_class='ovr')
如 source file.
要使用 classifier 处理多 class 问题,例如 LogisticRegression
,需要 ovr
并且 y_true
是格式的分类值。以上设置直接生效
二进制 classifications 的一些其他指标也可以通过包装相应的函数来扩展。例如,average_precision_score
可以包装为
from sklearn.preprocessing import OneHotEncoder
def multi_auprc(y_true_cat, y_score):
y_true = OneHotEncoder().fit_transform(y_true_cat.reshape(-1, 1)).toarray()
return average_precision_score(y_true, y_score)
然后可以将 GridsearchCV 的指标定义为
{
'auprc': make_scorer(multi_auprc, needs_proba=True, greater_is_better=True)
}
我尝试根据此处的答案将 GridSearchCV 用于多 class 案例:
但是我得到了值错误,multiclass format is not supported.
如何将此方法用于多个 class 案例?
以下代码来自上面的答案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.
"label indicator format" 意味着每个标签值都表示为二进制列(而不是单个列中的唯一目标值)。您不想为您的预测器执行此操作,因为它可能会导致非互斥预测(即预测案例 p1 的标签 2 和 4,或预测案例 p2 的标签)。
选择或自定义实现针对多类问题定义明确的评分函数,例如 F1 score. Personally I find informedness 比 F1 分数更有说服力,并且比 roc_auc_score 更容易泛化到多类问题.
支持多class
可以设置scoring = f1.macro
的para,例如:
gsearch1 = GridSearchCV(estimator = est1, param_grid=params_test1, scoring='f1_macro', cv=5, n_jobs=-1)
或得分='roc_auc_ovr'
如果 classifier 默认为 y_true
和 y_pred
/[=15= 具有正确的 API,则自然支持多 class ].
否则,必须使用 make_scorer
等评分函数进行一些自定义。
对于 common metrics 就像 AUROC 对于 multi-classes,sklearn 提供了 'roc_auc_ovr'
,它实际上指的是
roc_auc_ovr_scorer = make_scorer(roc_auc_score, needs_proba=True,
multi_class='ovr')
如 source file.
要使用 classifier 处理多 class 问题,例如 LogisticRegression
,需要 ovr
并且 y_true
是格式的分类值。以上设置直接生效
二进制 classifications 的一些其他指标也可以通过包装相应的函数来扩展。例如,average_precision_score
可以包装为
from sklearn.preprocessing import OneHotEncoder
def multi_auprc(y_true_cat, y_score):
y_true = OneHotEncoder().fit_transform(y_true_cat.reshape(-1, 1)).toarray()
return average_precision_score(y_true, y_score)
然后可以将 GridsearchCV 的指标定义为
{
'auprc': make_scorer(multi_auprc, needs_proba=True, greater_is_better=True)
}