4

我正在尝试在多标签分类中使用 scikit 计算宏 F1

from sklearn.metrics import f1_score

y_true = [[1,2,3]]
y_pred = [[1,2,3]]

print f1_score(y_true, y_pred, average='macro')

但是它失败并显示错误消息

ValueError: multiclass-multioutput is not supported

如何使用多标签分类计算宏 F1?

4

1 回答 1

8

在当前的 scikit-learn 版本中,您的代码会导致以下警告:

DeprecationWarning: Direct support for sequence of sequences multilabel
    representation will be unavailable from version 0.17. Use
    sklearn.preprocessing.MultiLabelBinarizer to convert to a label
    indicator representation.

按照此建议,您可以使用sklearn.preprocessing.MultiLabelBinarizer将此多标签类转换为f1_score. 例如:

from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.metrics import f1_score

y_true = [[1,2,3]]
y_pred = [[1,2,3]]

m = MultiLabelBinarizer().fit(y_true)

f1_score(m.transform(y_true),
         m.transform(y_pred),
         average='macro')
# 1.0
于 2015-10-25T14:09:48.317 回答