我正在尝试用multi-label classification
中的scikit计算宏-F1from 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?
答案 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