我试图在多标签分类中使用scikit计算宏观F1值
from sklearn.metrics import f1_scorey_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值?
回答:
在当前的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 MultiLabelBinarizerfrom sklearn.metrics import f1_scorey_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