用于多类分类的 sklearn 指标
sklearn metrics for multiclass classification
我已经使用 sklearn 进行了 GaussianNB 分类。我尝试使用以下代码计算指标:
print accuracy_score(y_test, y_pred)
print precision_score(y_test, y_pred)
准确度分数工作正常,但精度分数计算显示错误为:
ValueError: Target is multiclass but average='binary'. Please choose another average setting.
由于目标是多类的,我可以得到准确率、召回率等指标分数吗?
函数调用precision_score(y_test, y_pred)
等同于precision_score(y_test, y_pred, pos_label=1, average='binary')
。
文档 (http://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html) 告诉我们:
'binary':
Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
所以问题是你的标签不是二进制的,但可能是单热编码的。幸运的是,还有其他选项适用于您的数据:
precision_score(y_test, y_pred, average=None)
将 return 每个 class 的精度分数,而
precision_score(y_test, y_pred, average='micro')
将return总比
tp/(tp + fp)
如果您选择 binary
之外的另一个 average
选项,pos_label
参数将被忽略。
我已经使用 sklearn 进行了 GaussianNB 分类。我尝试使用以下代码计算指标:
print accuracy_score(y_test, y_pred)
print precision_score(y_test, y_pred)
准确度分数工作正常,但精度分数计算显示错误为:
ValueError: Target is multiclass but average='binary'. Please choose another average setting.
由于目标是多类的,我可以得到准确率、召回率等指标分数吗?
函数调用precision_score(y_test, y_pred)
等同于precision_score(y_test, y_pred, pos_label=1, average='binary')
。
文档 (http://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html) 告诉我们:
'binary':
Only report results for the class specified by pos_label. This is applicable only if targets (y_{true,pred}) are binary.
所以问题是你的标签不是二进制的,但可能是单热编码的。幸运的是,还有其他选项适用于您的数据:
precision_score(y_test, y_pred, average=None)
将 return 每个 class 的精度分数,而
precision_score(y_test, y_pred, average='micro')
将return总比
tp/(tp + fp)
如果您选择 binary
之外的另一个 average
选项,pos_label
参数将被忽略。