maxframe.learn.metrics.recall_score#
- maxframe.learn.metrics.recall_score(y_true, y_pred, *, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn', execute=False, session=None, run_kwargs=None)[源代码]#
计算召回率
召回率是
tp / (tp + fn)的比率,其中tp是真正例的数量,fn是假负例的数量。召回率直观上反映了分类器找到所有正样本的能力。最佳值是 1,最差值是 0。
更多内容请参见 用户指南。
- 参数:
y_true (1d array-like, or label indicator array / sparse matrix) -- 真实(正确)的目标值。
y_pred (1d array-like, or label indicator array / sparse matrix) -- 由分类器返回的预测目标值。
labels (list, optional) -- 当
average != 'binary'时要包含的标签集合,以及当average is None时这些标签的顺序。数据中存在的标签可以被排除,例如在计算忽略多数负类的多类平均值时;而数据中不存在的标签在宏平均中会导致 0 分量。对于多标签目标,标签是列索引。默认情况下,y_true和y_pred中的所有标签都会按排序顺序使用。pos_label (str or int, 1 by default) -- 如果
average='binary'且数据是二分类的,则报告该类的结果。如果数据是多类或多标签的,则此参数会被忽略;设置labels=[pos_label]且average != 'binary'时,将只报告该标签的得分。average (string, [None, 'binary' (default), 'micro', 'macro', 'samples', 'weighted']) -- 对于多类/多标签目标,此参数是必需的。如果为
None,则返回每个类别的得分。否则,它决定了数据上执行的平均类型:'binary':仅报告由pos_label指定的类的结果。这仅适用于二分类目标(y_{true,pred})。'micro':通过计算总的真正例、假负例和假正例来全局计算指标。'macro':为每个标签计算指标,并求它们的未加权平均值。此方法不考虑标签不平衡。'weighted':为每个标签计算指标,并根据支持度(每个标签的真实实例数)加权平均。这会调整 'macro' 以考虑标签不平衡;它可能导致 F-score 不在精确率和召回率之间。'samples':为每个实例计算指标,并求它们的平均值(仅在多标签分类中与accuracy_score()不同时有意义)。sample_weight (array-like of shape (n_samples,), default=None) -- 样本权重。
zero_division ("warn", 0 or 1, default="warn") -- 设置在零除法时返回的值。如果设置为 "warn",则行为如同 0,但也会引发警告。
- 返回:
recall -- 二分类中正类的召回率或多类任务中各类召回率的加权平均值。
- 返回类型:
float (if average is not None) or array of float, shape = [n_unique_labels]
参见
precision_recall_fscore_support,balanced_accuracy_score,multilabel_confusion_matrix示例
>>> from maxframe.learn.metrics import recall_score >>> y_true = [0, 1, 2, 0, 1, 2] >>> y_pred = [0, 2, 1, 0, 0, 1] >>> recall_score(y_true, y_pred, average='macro') 0.33... >>> recall_score(y_true, y_pred, average='micro') 0.33... >>> recall_score(y_true, y_pred, average='weighted') 0.33... >>> recall_score(y_true, y_pred, average=None) array([1., 0., 0.]) >>> y_true = [0, 0, 0, 0, 0, 0] >>> recall_score(y_true, y_pred, average=None) array([0.5, 0. , 0. ]) >>> recall_score(y_true, y_pred, average=None, zero_division=1) array([0.5, 1. , 1. ])
备注
当
true positive + false negative == 0时,召回率返回 0 并引发UndefinedMetricWarning。此行为可以通过zero_division参数进行修改。