问题描述
我尝试计算 f1_score
,但是当我使用 sklearn f1_score
方法时,我会收到一些警告.
I try to calculate the f1_score
but I get some warnings for some cases when I use the sklearn f1_score
method.
我有一个用于预测的多标签 5 类问题.
I have a multilabel 5 classes problem for a prediction.
import numpy as np
from sklearn.metrics import f1_score
y_true = np.zeros((1,5))
y_true[0,0] = 1 # => label = [[1, 0, 0, 0, 0]]
y_pred = np.zeros((1,5))
y_pred[:] = 1 # => prediction = [[1, 1, 1, 1, 1]]
result_1 = f1_score(y_true=y_true, y_pred=y_pred, labels=None, average="weighted")
print(result_1) # prints 1.0
result_2 = f1_score(y_true=y_ture, y_pred=y_pred, labels=None, average="weighted")
print(result_2) # prints: (1.0, 1.0, 1.0, None) for precision/recall/fbeta_score/support
当我使用 average="samples"
而不是 "weighted"
时,我得到 (0.1, 1.0, 0.1818..., None)."weighted"
选项是否对多标签问题没有用,或者我如何正确使用 f1_score
方法?
When I use average="samples"
instead of "weighted"
I get (0.1, 1.0, 0.1818..., None). Is the "weighted"
option not useful for a multilabel problem or how do I use the f1_score
method correctly?
我在使用 average="weighted"
时也收到警告:
I also get a warning when using average="weighted"
:
UndefinedMetricWarning:召回率和 F 分数定义不明确,在没有真实样本的标签中被设置为 0.0."
推荐答案
如果你把数据稍微加起来就可以了:
It works if you slightly add up data:
y_true = np.array([[1,0,0,0], [1,1,0,0], [1,1,1,1]])
y_pred = np.array([[1,0,0,0], [1,1,1,0], [1,1,1,1]])
recall_score(y_true=y_true, y_pred=y_pred, average='weighted')
>>> 1.0
precision_score(y_true=y_true, y_pred=y_pred, average='weighted')
>>> 0.9285714285714286
f1_score(y_true=y_true, y_pred=y_pred, average='weighted')
>>> 0.95238095238095244
数据表明我们没有遗漏任何真阳性,也没有预测任何假阴性(recall_score
等于 1).然而,我们在第二次观察中预测了一个误报,导致 precision_score
等于 ~0.93.
The data suggests we have not missed any true positives and have not predicted any false negatives (recall_score
equals 1). However, we have predicted one false positive in the second observation that lead to precision_score
equal ~0.93.
由于precision_score
和recall_score
在weighted
参数下都不为零,因此存在f1_score
.由于示例中缺少信息,我认为您的案例无效.
As both precision_score
and recall_score
are not zero with weighted
parameter, f1_score
, thus, exists. I believe your case is invalid due to lack of information in the example.
这篇关于如何计算多标签分类的 F1-Score?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!