scikit在多标签分类中计算F1

时间:2015-10-25 05:59:10

标签: machine-learning nlp scikit-learn precision-recall

我正在尝试用multi-label classification

中的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?

1 个答案:

答案 0 :(得分:4)

在当前的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