二分类之对 sklearn.metrics.recall_score 的理解

sklearn.metrics.recall_score

概念

二分类,分为两类,一类是你关注的类,另一类为不关注的类。
假设,分为1,0,其中1是我们关注的,0为不关注的。
通常认为,我们关注的为正类,即1为正类
recall_score:预测为正类且预测正确的数量/样本中正类的数量
衡量预测的准确性。

举例

会存在一个样本集,即真集,如

y_true = [0, 1, 0, 1]

存在一个预测集

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
sklearn.metricsrecall_score是通过计算真阳性(TP)和假阴性(FN)来计算的。具体地,recall_score被定义为: Recall = TP / (TP + FN) 其中,TP是真阳性的数量,FN是假阴性的数量。 recall_score函数的实现方式如下: ```python def recall_score(y_true, y_pred, labels=None, pos_label=1, average='binary', sample_weight=None, zero_division='warn'): # 参数说明: # y_true:真实标签,可以是二进制标签、多分标签或多标签标签。 # y_pred:预测标签,与y_true具有相同的结构。 # labels:要评估的标签的列表,如果为None,则评估所有标签。 # pos_label:二进制分问题中的正标签。 # average:多/多标签数据的评估方法,可选值为:'micro', 'macro', 'samples', 'weighted'或None。 # sample_weight:样本权重。 # zero_division:当分母为零时的行为,可选值为:"warn", 0或1,默认为"warn"。 # 将二进制标签转换为多标签 y_type, y_true, y_pred = _check_targets(y_true, y_pred) # 获取要评估的标签列表 labels = _get_multiclass_labels(y_true, y_pred, labels) # 计算每个标签的recall值 return _average_binary_score(_binary_recall_score, y_true, y_pred, labels=labels, pos_label=pos_label, average=average, sample_weight=sample_weight, zero_division=zero_division) ``` 其中,_average_binary_score和_binary_recall_score是辅助函数,用于计算二进制分和多问题的recall值。具体实现方式如下: ```python def _average_binary_score(binary_metric, y_true, y_pred, pos_label=1, sample_weight=None, **kwargs): # 计算二进制分问题的recall值 if len(np.unique(y_true)) == 1: return 0.0 return binary_metric(y_true, y_pred, pos_label=pos_label, sample_weight=sample_weight) def _binary_recall_score(y_true, y_pred, pos_label=1, sample_weight=None): tp = np.sum((y_true == pos_label) & (y_pred == pos_label), axis=0) fn = np.sum((y_true == pos_label) & (y_pred != pos_label), axis=0) recall = tp / (tp + fn) if np.any(np.isnan(recall)): warnings.warn('Recall is ill-defined and being set to 0.0 due to no true samples.') recall = np.nan_to_num(recall) return recall ``` 在二分类问题中,_binary_recall_score函数计算真阳性和假阴性的数量,并使用上述公式计算recall值。在多问题中,_average_binary_score函数将多问题转换为多个二分类问题,并计算每个问题的recall值。最终,recall_score函数根据average参数返回所有标签的平均recall值或返回每个标签的recall值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值