gpt4 book ai didi

python - sklearn多类roc auc分数

转载 作者:行者123 更新时间:2023-12-02 02:42:43 24 4
gpt4 key购买 nike

如何在sklearn中获取多类分类的roc auc分数?

二进制

# this works
roc_auc_score([0,1,1], [1,1,1])

多类

# this fails
from sklearn.metrics import roc_auc_score

ytest = [0,1,2,3,2,2,1,0,1]
ypreds = [1,2,1,3,2,2,0,1,1]

roc_auc_score(ytest, ypreds,average='macro',multi_class='ovo')

# AxisError: axis 1 is out of bounds for array of dimension 1

我看了官方documentation但无法解决问题。

最佳答案

在多标签情况下,roc_auc_score 期望具有形状(n_samples,n_classes)的二进制标签指示符,这是回到一对多时尚的方式。

要轻松做到这一点,您可以使用 label_binarize ( https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.label_binarize.html#sklearn.preprocessing.label_binarize )。

对于您的代码,它将是:

from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import label_binarize

# You need the labels to binarize
labels = [0, 1, 2, 3]

ytest = [0,1,2,3,2,2,1,0,1]

# Binarize ytest with shape (n_samples, n_classes)
ytest = label_binarize(ytest, classes=labels)

ypreds = [1,2,1,3,2,2,0,1,1]

# Binarize ypreds with shape (n_samples, n_classes)
ypreds = label_binarize(ypreds, classes=labels)


roc_auc_score(ytest, ypreds,average='macro',multi_class='ovo')

通常,这里 ypreds 和yest 变为:

ytest
array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 0, 0],
[0, 1, 0, 0]])

ypreds
array([[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 1, 0],
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]])

关于python - sklearn多类roc auc分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63303682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com