模型评估指标

本文介绍了模型评估中的关键指标,包括准确度、精度、召回率及F1分数等,并详细解释了这些指标的计算方法和应用场景。同时,针对问答系统的评估,文章还探讨了绝对匹配(Exact Match)和F1分数的具体计算方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

符号含义
y y y真实值【一个向量】
y ^ \hat y y^预测值【一个向量】

分类评估(Metrics for Classification)

准确度(Accuracy)

正确的预测/样本个数

Eg:

y y y = (0,0,1,0,1)

y ^ \hat y y^ = (1,0,1,0,1)
A c c u a r c y = s u m ( y = = y ^ ) / l e n ( y ) = s u m ( ( 0 , 0 , 1 , 0 , 1 ) = = ( 1 , 0 , 1 , 0 , 1 ) ) / 5 = 4 / 5 \begin{aligned} Accuarcy &= sum(y == \hat y)/len(y) \\ &= sum((0,0,1,0,1) == (1,0,1,0,1))/5\\ &= 4/5 \end{aligned} Accuarcy=sum(y==y^)/len(y)=sum((0,0,1,0,1)==(1,0,1,0,1))/5=4/5


精度(Precision)

当有时候数据正负类分布不平衡,此时得到的准确度就有一点偏离模型真正能够达到的准确度,因此需要其他指标来权衡利弊。

每个类别里预测等于实际的个数/在预测中该类别出现的次数

Eg:

y y y = (0,0,1,0,1)

y ^ \hat y y^ = (1,0,1,0,1)

预测正确率
P r e c i s i o n = s u m ( y = = 1   a n d   y ^ = = 1 ) / s u m ( y ^ = = 1 ) = s u m ( ( 0 , 0 , 1 , 0 , 1 ) = = 1   a n d   ( 1 , 0 , 1 , 0 , 1 ) = = 1 ) / s u m ( ( 1 , 0 , 1 , 0 , 1 ) = = 1 ) = 2 / 3 \begin{aligned} Precision &= sum(y==1\ and\ \hat y==1)/sum(\hat y==1) \\ &= sum((0,0,1,0,1)==1\ and\ (1,0,1,0,1)==1)/ sum((1,0,1,0,1)==1)\\ &= 2/3 \end{aligned} Precision=sum(y==1 and y^==1)/sum(y^==1)=sum((0,0,1,0,1)==1 and (1,0,1,0,1)==1)/sum((1,0,1,0,1)==1)=2/3


召回率(Recall)

每个类别里预测等于实际的个数/在实际中该类别出现的次数

Eg:

y y y = (0,0,1,0,1)

y ^ \hat y y^ = (1,0,1,0,1)

预测正确率
R e c a l l = s u m ( y = = 1   a n d   y ^ = = 1 ) / s u m ( y = = 1 ) = s u m ( ( 0 , 0 , 1 , 0 , 1 ) = = 1   a n d   ( 1 , 0 , 1 , 0 , 1 ) = = 1 ) / s u m ( ( 0 , 0 , 1 , 0 , 1 ) = = 1 ) = 2 / 2 \begin{aligned} Recall &= sum(y==1\ and\ \hat y==1)/sum( y==1) \\ &= sum((0,0,1,0,1)==1\ and\ (1,0,1,0,1)==1)/ sum((0,0,1,0,1)==1)\\ &= 2/2 \end{aligned} Recall=sum(y==1 and y^==1)/sum(y==1)=sum((0,0,1,0,1)==1 and (1,0,1,0,1)==1)/sum((0,0,1,0,1)==1)=2/2


F1

平衡精度和召回率

F 1 = 2 ∗ P ∗ R / ( P + R ) F1 = 2*P*R/(P+R) F1=2PR/(P+R)

Eg:

在以上的例子中计算F1
F 1 = 2 ∗ P ∗ R / ( P + R ) = ( 2 ∗ 2 / 3 ∗ 2 / 2 ) / ( 2 / 3 + 2 / 2 ) = 4 / 5 \begin{aligned} F1 &= 2*P*R/(P+R) \\ &= (2*2/3*2/2)/(2/3+2/2)\\ &= 4/5 \end{aligned} F1=2PR/(P+R)=(22/32/2)/(2/3+2/2)=4/5


问答评估(Metrics for Question Answering)

绝对匹配(Exact Match)

衡量预测答案是否与标准答案完全一致,Exact Match是问答系统的一种常见的评价标准,它用来评价预测中匹配到正确答案(ground truth answers)的百分比。

EM 是用于 SQuAD 的主要指标之一。

实现公式

名称符号
预测答案字符串S_pred
标准答案字符串S_ref

E M = { 1 , if  S p r e d = S r e f 0 , if  S p r e d ≠ S r e f EM = \begin{cases} 1, & \text{if }S_{pred} = S_{ref} \\ 0, & \text{if } S_{pred}\neq S_{ref} \end{cases} EM={1,0,if Spred=Srefif Spred=Sref

也存在缺陷,如有些答案没有写全冠词the、an(预测答案是apple,标准答案是an apple),数字1和中文一,其实答案应该是对的

F1

衡量预测答案和标准答案的相似度,属于[0,1]

名称符号
预测答案字符串S_pred
标准答案字符串S_ref
预测答案单词集合W_pred
标准答案单词集合W_ref
集合W中单词个数|W|

实现公式

两者集合取交集
W m a t c h = W p r e d ∩ W r e f W_{match} = W_{pred} \cap W_{ref} Wmatch=WpredWref
P = ∣ W m a t c h ∣ W p r e d P = \frac{|W_{match}|}{W_{pred}} P=WpredWmatch

R = ∣ W m a t c h ∣ W r e f R = \frac{|W_{match}|}{W_{ref}} R=WrefWmatch
F 1 = 2 ∗ P ∗ R / ( P + R ) F1 = 2*P*R/(P+R) F1=2PR/(P+R)

代码

导入相关库

from __future__ import print_function
from collections import Counter
import string
import re
import argparse
import json
import sys

在进行匹配之间先进行一些处理

  1. 将单词小写
  2. 去除标点符号
  3. 去除冠词
  4. 根据空格进行分词
def normalize_answer(s):

    def remove_articles(text):
        return re.sub(r'\b(a|an|the)\b', ' ', text)

    def white_space_fix(text):
        return ' '.join(text.split())

    def remove_punc(text):
        exclude = set(string.punctuation)
        return ''.join(ch for ch in text if ch not in exclude)

    def lower(text):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(s))))

计算F1


def f1_score(prediction, ground_truth):
    # 首先把prediction和ground_truth标准化(即用上面的函数进行处理)
    prediction_tokens = normalize_answer(prediction).split()
    ground_truth_tokens = normalize_answer(ground_truth).split()
    # 统计他们共有的字符
    common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
    # 计算共有的字符的总量
    num_same = sum(common.values())
    if num_same == 0:
        return 0
    # 计算precision
    precision = 1.0 * num_same / len(prediction_tokens)
    # 计算recall
    recall = 1.0 * num_same / len(ground_truth_tokens)
    f1 = (2 * precision * recall) / (precision + recall)
    return f1

计算EM

def exact_match_score(prediction, ground_truth):
    return (normalize_answer(prediction) == normalize_answer(ground_truth))
### 解析 Import Error 的常见原因 当遇到 `ImportError: cannot import name 'Generic'` 错误时,通常意味着尝试从模块中导入的对象不存在或无法访问。此问题可能由多种因素引起: - 版本不兼容:不同库之间的版本冲突可能导致此类错误。 - 安装缺失:目标库未正确安装或路径配置有误。 - 导入语句不当:可能存在循环依赖或其他语法层面的问题。 ### 针对 Generic 类型的具体解决方案 对于特定于 `Generic` 的情况,考虑到 Python 中 `Generic` 是 typing 模块的一部分,在处理该类别的 ImportError 时可采取如下措施[^1]: #### 方法一:确认typing模块可用性 确保环境中已安装标准库中的 typing 模块,并且其版本支持所使用的特性。可以通过以下命令验证: ```bash python -c "from typing import Generic; print(Generic)" ``` 如果上述命令执行失败,则可能是由于 Python 或者相关扩展包的版本过低造成的。此时应考虑升级至更高版本的解释器以及对应的开发工具链。 #### 方法二:调整导入方式 有时直接通过顶层命名空间来获取所需组件会更稳定可靠。修改代码以采用这种做法可能会解决问题: ```python from collections.abc import Iterable # 如果是迭代器相关接口 from typing import TypeVar, Protocol # 对于协议和泛型定义 T = TypeVar('T') class MyContainer(Protocol[T]): ... ``` 注意这里并没有显式提到 `Generic` ,而是利用了更为基础的数据结构抽象基类或是其他替代方案实现相同功能[^2]。 #### 方法三:排查环境变量设置 检查系统的 PYTHONPATH 和虚拟环境配置是否正常工作。任何异常都可能导致某些第三方软件包找不到必要的资源文件而引发类似的错误提示。建议清理并重建项目专属的工作区以便排除干扰项的影响。 #### 示例修正后的代码片段 假设原始代码试图这样引入 `Generic` : ```python from some_module import Generic # 可能导致 ImportError ``` 改为遵循官方文档推荐的方式后变为: ```python from typing import Generic # 正确的做法 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

365JHWZGo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值