f1_score#
- sklearn.metrics.f1_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn')[源代码]#
计算F1评分,也称为平衡F评分或F测量。
F1分数可以解释为精确度和召回率的调和平均值,其中F1分数在1时达到最佳值,在0时达到最差值。查准率和查全率对F1得分的相对贡献是相等的。F1分数的计算公式为:
\[\text{F1} = \frac{2 * \text{TP}}{2 * \text{TP} + \text{FP} + \text{FN}}\]Where \(\text{TP}\) is the number of true positives, \(\text{FN}\) is the number of false negatives, and \(\text{FP}\) is the number of false positives. F1 is by default calculated as 0.0 when there are no true positives, false negatives, or false positives.
年之后的支持 binary 通过治疗来实现目标 multiclass 和 multilabel 数据作为二进制问题的集合,每个标签一个问题。为 binary 案例、设置
average='binary'
将返回F1比分pos_label
.如果average
不'binary'
,pos_label
被忽略,并计算两个类别的F1分数,然后求平均值或两者返回(当average=None
).同样对于 multiclass 和 multilabel 目标,F1得分为所有人labels
根据average
参数.使用labels
指定要计算F1分数的标签集。阅读更多的 User Guide .
- 参数:
- y_true1D类数组或标签指示符数组/稀疏矩阵
地面真相(正确)目标值。
- y_pred1D类数组或标签指示符数组/稀疏矩阵
分类器返回的估计目标。
- labels类数组,默认=无
何时包含的标签集
average != 'binary'
,以及它们的顺序,如果average is None
.可以排除数据中存在的标签,例如在多类分类中排除“负类”。可以包括数据中未出现的标签,并将“分配”0个样本。对于多标签目标,标签是列索引。默认情况下,所有标签y_true
和y_pred
按排序顺序使用。在 0.17 版本发生变更: 参数
labels
改进了多类问题。- pos_labelint、float、bool或char,默认=1
如果需要报告的班级
average='binary'
并且数据是二进制的,否则该参数将被忽略。对于多类别或多标签目标,设置labels=[pos_label]
和average != 'binary'
仅报告一个标签的指标。- average“微观”、“宏观”、“样本”、“加权”、“二进制”}或无, 默认='二元'
多类/多标签目标需要此参数。如果
None
,返回每个类别的指标。否则,这将确定对数据执行的平均类型:'binary'
:仅报告由指定的类的结果
pos_label
.这仅适用于以下情况 (y_{true,pred}
)是二进制的。'micro'
:通过计算总真阳性、假阴性和假阳性来计算全球指标。
'macro'
:计算每个标签的指标,并找到其未加权平均值。 这没有考虑标签不平衡。
'weighted'
:计算每个标签的指标,并找到按支持度加权的平均值(每个标签的真实实例数)。这会改变“宏”以解决标签失衡问题;它可能会导致F评分不在精确度和召回率之间。
'samples'
:计算每个实例的指标,并找到其平均值(仅对多标签分类有意义,因为这与多标签分类不同
accuracy_score
).
- sample_weight形状类似数组(n_samples,),默认=无
样本重量。
- zero_division{“warn”,0.0,1.0,NP.nan},默认=“warn”
设置当零除时(即当所有预测和标签均为负时)返回的值。
注释:-如果设置为“警告”,则其行为类似于0,但也会引发警告。- 如果设置为
np.nan
,这些值将被排除在平均值之外。Added in version 1.3:
np.nan
添加了选项。
- 返回:
- f1_scorefloat或float数组,形状= [n_unique_labels]
二进制分类中正类别的F1分数或多类别任务每个类别F1分数的加权平均值。
参见
fbeta_score
计算F-beta评分。
precision_recall_fscore_support
计算精确度、召回率、F分数和支持度。
jaccard_score
计算Jaccard相似性系数得分。
multilabel_confusion_matrix
为每个类别或样本计算混淆矩阵。
注意到
当
true positive + false positive + false negative == 0
(i.e.一个类完全缺席两者y_true
或y_pred
),f分数未定义。在这种情况下,默认f-score将设置为0.0,并且UndefinedMetricWarning
将被提出。可以通过设置zero_division
参数.引用
[1]Wikipedia entry for the F1-score <https://en.wikipedia.org/wiki/F1_score>
_.示例
>>> import numpy as np >>> from sklearn.metrics import f1_score >>> y_true = [0, 1, 2, 0, 1, 2] >>> y_pred = [0, 2, 1, 0, 0, 1] >>> f1_score(y_true, y_pred, average='macro') 0.26... >>> f1_score(y_true, y_pred, average='micro') 0.33... >>> f1_score(y_true, y_pred, average='weighted') 0.26... >>> f1_score(y_true, y_pred, average=None) array([0.8, 0. , 0. ])
>>> # binary classification >>> y_true_empty = [0, 0, 0, 0, 0, 0] >>> y_pred_empty = [0, 0, 0, 0, 0, 0] >>> f1_score(y_true_empty, y_pred_empty) 0.0... >>> f1_score(y_true_empty, y_pred_empty, zero_division=1.0) 1.0... >>> f1_score(y_true_empty, y_pred_empty, zero_division=np.nan) nan...
>>> # multilabel classification >>> y_true = [[0, 0, 0], [1, 1, 1], [0, 1, 1]] >>> y_pred = [[0, 0, 0], [1, 1, 1], [1, 1, 0]] >>> f1_score(y_true, y_pred, average=None) array([0.66666667, 1. , 0.66666667])