Python 代码评估脚本:用于计算问答模型的准确率和 F1 分数
def main():
if len(sys.argv) != 3:
print('Usage: python eval.py goldData predAnswers')
sys.exit(-1)
goldData = json.loads(open(sys.argv[1]).read())
predAnswers = json.loads(open(sys.argv[2]).read())
PredAnswersById = {}
for item in predAnswers:
PredAnswersById[item['QuestionId']] = item['Answers']
total = 0.0
f1sum = 0.0
recSum = 0.0
precSum = 0.0
numCorrect = 0
for entry in goldData['Questions']:
skip = True
for pidx in range(0, len(entry['Parses'])):
np = entry['Parses'][pidx]
if np['AnnotatorComment']['QuestionQuality'] == 'Good' and np['AnnotatorComment']['ParseQuality'] == 'Complete':
skip = False
if (len(entry['Parses']) == 0 or skip):
continue
total += 1
id = entry['QuestionId']
if id not in PredAnswersById:
print('The problem ' + id + ' is not in the prediction set')
print('Continue to evaluate the other entries')
continue
if len(entry['Parses']) == 0:
print('Empty parses in the gold set. Breaking!!')
break
predAnswers = PredAnswersById[id]
bestf1 = -9999
bestf1Rec = -9999
bestf1Prec = -9999
for pidx in range(0, len(entry['Parses'])):
pidxAnswers = entry['Parses'][pidx]['Answers']
prec, rec, f1 = CalculatePRF1(pidxAnswers, predAnswers)
if f1 > bestf1:
bestf1 = f1
bestf1Rec = rec
bestf1Prec = prec
f1sum += bestf1
recSum += bestf1Rec
precSum += bestf1Prec
if bestf1 == 1.0:
numCorrect += 1
print('Number of questions:', int(total))
print('Average precision over questions: %.3f' % (precSum / total))
print('Average recall over questions: %.3f' % (recSum / total))
print('Average f1 over questions (accuracy): %.3f' % (f1sum / total))
print('F1 of average recall and average precision: %.3f' % (2 * (recSum / total) * (precSum / total) / (recSum / total + precSum / total)))
print('True accuracy (ratio of questions answered exactly correctly): %.3f' % (numCorrect / total))
该 Python 代码脚本用于评估问答模型的性能,计算平均精度、召回率和 F1 分数,并提供准确回答问题的比例。
使用方法:
- 将 goldData.json 和 predAnswers.json 文件分别替换为包含真实答案和预测答案的 JSON 文件。
- 在命令行中运行脚本,例如:
python eval.py goldData.json predAnswers.json
脚本功能:
- 读取包含真实答案的 goldData.json 文件和包含预测答案的 predAnswers.json 文件。
- 逐个评估问题,计算每个问题的精度、召回率和 F1 分数。
- 计算所有问题的平均精度、召回率和 F1 分数。
- 计算准确回答问题的比例。
输出:
脚本将打印以下信息:
- 问题总数
- 平均精度
- 平均召回率
- 平均 F1 分数
- 平均召回率和平均精度的 F1 分数
- 准确回答问题的比例
注意:
- 该脚本需要使用
CalculatePRF1函数来计算精度、召回率和 F1 分数,该函数需要根据具体情况进行定义。 - 该脚本假设 goldData.json 和 predAnswers.json 文件包含与问题 ID 相关的答案数据。
其他:
该脚本可用于评估各种问答模型的性能,例如基于规则的问答模型、基于机器学习的问答模型等。 通过评估模型的性能,可以帮助你了解模型的优缺点,并改进模型的性能。
原文地址: https://www.cveoy.top/t/topic/n4na 著作权归作者所有。请勿转载和采集!