准确率、召回率、精度、错误率

本文介绍了评估机器学习模型性能的关键指标,包括准确率、召回率、精度等,并详细解释了这些指标的具体含义及其计算方法。
摘要由CSDN通过智能技术生成

概念

准确率precision(查准率): 是针对预测为正的样本来说的,指的是,预测为正的样本中预测正确了的百分比。

召回率recall(查全率):是针对真实为正的所有样本来说的,指的是,所有为正的样本中,被正确识别出来了的样本的比例。

精度accuracy:指的是,预测正确了的样本占总样本的比例。

错误率:1-精度

TP:真正例 (预测为正,真实为正)
TN: 真反例 (预测为反,真实为反)
FP:假正例 (预测为正,真实为反)
FN:假反例 (预测为反,真实为正)

这里写图片描述

准确率 precision=TPTP+FP

召回率 recall=TPTP+FN

可以发现分子都是TP,即真实为正预测也为正的样本

精度= TP+TNTP+TN+FP+FN

### 精度 (Precision) 精度衡量的是预测为正类的结果中有多少是真正的正类。较高的 Precision 表明模型对于 "预测为 Positive" 的判断更加可信[^1]。 公式如下: \[ \text{Precision} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Positives}} \] 其中 True Positives 是真正例的数量,而 False Positives 则是指错误地标记成正类的例子数量。 ```python def calculate_precision(true_positives, false_positives): if true_positives + false_positives == 0: return 0 return true_positives / (true_positives + false_positives) ``` ### 召回率 (Recall) 召回率指的是所有实际为正类的数据中被正确识别的比例。这反映了模型发现所有正样本的能力。 公式表示为: \[ \text{Recall} = \frac{\text{True Positives}}{\text{True Positives} + \text{False Negatives}} \] 这里 False Negatives 表示实际上属于正类却被误判为负类的情况数。 ```python def calculate_recall(true_positives, false_negatives): if true_positives + false_negatives == 0: return 0 return true_positives / (true_positives + false_negatives) ``` ### 准确率 (Accuracy) 准确率是最直观的理解方式之一,它代表了分类器做出的所有预测中有多少比例是正确的。然而,在不平衡数据集上可能不是最佳评估指标。 计算方法很简单: \[ \text{Accuracy} = \frac{\text{Correct Predictions}}{\text{Total Predictions}} \] 即 \[ \text{Accuracy} = \frac{\text{True Positives} + \text{True Negatives}}{\text{All Samples}} \] ```python def calculate_accuracy(true_positives, true_negatives, all_samples): if all_samples == 0: return 0 return (true_positives + true_negatives) / all_samples ``` ### 平均精确率 (Mean Average Precision, MAP) MAP 主要用于信息检索领域以及多标签分类问题中的性能评价。其核心思想是在不同阈值下分别求得每个查询对应的 AP 值(Average Precision),再取这些 AP 的算术平均作为最终得分。 具体实现较为复杂,通常依赖于特定应用场景下的定义。一般情况下会涉及到先按置信度排序,然后逐步降低阈值来统计每次变化后的 Precision 和 Recall 来构建 PR 曲线并据此得到 AP 值。 ```python import numpy as np def average_precision(relevance_scores): """Calculate the average precision.""" total_relevant = sum(relevance_scores) if total_relevant == 0: return 0 precisions_at_k = [] num_correct_predictions = 0 for i, relevant in enumerate(relevance_scores, start=1): if relevant: num_correct_predictions += 1 precisions_at_k.append(num_correct_predictions / i) return np.mean(precisions_at_k) def mean_average_precision(query_results_list): """Compute Mean Average Precision over multiple queries""" aps = [average_precision(result['relevances']) for result in query_results_list] return np.mean(aps) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值