scikit-learn 在多标签分类中计算 F1
scikit-learn calculate F1 in multilabel classification
我正在尝试在 multi-label classification
中使用 scikit 计算 macro-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
如何计算多标签分类的macro-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
将此多标签 class 转换为 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
我正在尝试在 multi-label classification
中使用 scikit 计算 macro-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
如何计算多标签分类的macro-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
将此多标签 class 转换为 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