fbeta_score#
- sklearn.metrics.fbeta_score(y_true, y_pred, *, beta, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn')[源代码]#
计算F-beta评分。
F-Beta得分是精确度和召回率的加权调和平均值,在1时达到最佳值,在0时达到最差值。
The
beta
parameter represents the ratio of recall importance to precision importance.beta > 1
gives more weight to recall, whilebeta < 1
favors precision. For example,beta = 2
makes recall twice as important as precision, whilebeta = 0.5
does the opposite. Asymptotically,beta -> +inf
considers only recall, andbeta -> 0
only precision.F-Beta评分的公式为:
\[F_\beta = \frac{(1 + \beta^2) \text{tp}} {(1 + \beta^2) \text{tp} + \text{fp} + \beta^2 \text{fn}}\]哪里 \(\text{tp}\) 是真阳性的数量, \(\text{fp}\) 是误报的数量, \(\text{fn}\) 是假阴性的数量。
Support beyond term:
binary
targets is achieved by treating multiclass and multilabel data as a collection of binary problems, one for each label. For the binary case, settingaverage='binary'
will return F-beta score forpos_label
. Ifaverage
is not'binary'
,pos_label
is ignored and F-beta score for both classes are computed, then averaged or both returned (whenaverage=None
). Similarly, for multiclass and multilabel targets, F-beta score for alllabels
are either returned or averaged depending on theaverage
parameter. Uselabels
specify the set of labels to calculate F-beta score for.阅读更多的 User Guide .
- 参数:
- y_true1D类数组或标签指示符数组/稀疏矩阵
地面真相(正确)目标值。
- y_pred1D类数组或标签指示符数组/稀疏矩阵
分类器返回的估计目标。
- beta浮子
确定回忆在综合分数中的权重。
- 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
添加了选项。
- 返回:
- fbeta_scorefloat(如果平均值不是无)或float数组,shape = [n_unique_labels]
二元分类中正类的F-beta得分或多类任务中每个类的F-beta得分的加权平均值。
参见
precision_recall_fscore_support
计算精确度、召回率、F分数和支持度。
multilabel_confusion_matrix
为每个类别或样本计算混淆矩阵。
注意到
当
true positive + false positive + false negative == 0
,f分数返回0.0并提高UndefinedMetricWarning
.此行为可以通过设置修改zero_division
.引用
[1]R.贝萨-耶茨和B。里贝罗-内托(2011)。现代信息检索。艾迪生·韦斯利,页。327-328.
[2]Wikipedia entry for the F1-score <https://en.wikipedia.org/wiki/F1_score>
_.示例
>>> import numpy as np >>> from sklearn.metrics import fbeta_score >>> y_true = [0, 1, 2, 0, 1, 2] >>> y_pred = [0, 2, 1, 0, 0, 1] >>> fbeta_score(y_true, y_pred, average='macro', beta=0.5) 0.23... >>> fbeta_score(y_true, y_pred, average='micro', beta=0.5) 0.33... >>> fbeta_score(y_true, y_pred, average='weighted', beta=0.5) 0.23... >>> fbeta_score(y_true, y_pred, average=None, beta=0.5) array([0.71..., 0. , 0. ]) >>> y_pred_empty = [0, 0, 0, 0, 0, 0] >>> fbeta_score(y_true, y_pred_empty, ... average="macro", zero_division=np.nan, beta=0.5) 0.12...