这个小程序是3月份公司的一个小任务,关于它的解释,网上有一大堆文章。这里主要说的是针对我们公司当时的需求写的一个最简单的小程序。 先把公式贴出来:
一、需求:
当公司将数据处理完毕后,会传两个list给我,分别是“总的实体数“和”总的实体识别数”。
我要做的就是将两个list的数据作比较。然后给出结果。供后面的工作参考。
代码:
# -*- coding: utf-8 -*-
__author__ = 'se7en'
__date__ = '17/3/20 下午7:08'
line = {}
j = []
def crf(Totallist, Correctlist):
tote = len(Totallist) # 总实体数
total = len(Correctlist) # 总实体识别数
for i in Correctlist:
if Correctlist.count(i) > 1: # 统计字符串里某个字符出现的次数。
line[i] = Correctlist.count(i)
"""
将两个list的数据进行对比
去掉重复数据,取得长度
"""
try:
num = [g for g in Totallist if g in Correctlist]
qu = len(num)
dRmoval = qu
except:
return None
"""
套用公式
"""
try:
R = float(dRmoval) / float(total) * 100
P = float(dRmoval) / float(tote) * 100
F = 2 * P * R / (P + R)
except:
return None
"""输出结果"""
print totallist
print correctlist
print "\n"
for k,v in line.items():
print u"其中{k}重复了{v}次".format(k=k,v=v)
print "\n"
print u"准确率: {:.2f}" .format(R) + "%"
print u"召回率: {:.2f}" .format(P) + "%"
print u"F-测度值: {:.2f}" .format(F) + "%"
print "\n"
print u"总实体数:{tote}" .format(tote=tote)
print u"总实体识别数:{total}" .format(total=total)
print u"正确识别数:{correct}" .format(correct=dRmoval)
"""
这里在真实的项目中是没有的,两个list的数据都应该是从别的地方传过来的。
"""
totallist = ['123', 'abc', '678', '789', '666', '777', '999', 'jjh', 'jjh', 'jjh', 'jjh']
correctlist = ['123', '000', '002', '003', '000', '000', '022','000','000','002','003']
if __name__ == '__main__':
crf(totallist,correctlist)
运行结果:
Tags: None