投票 (找众数) from collections import Counter 导致召回率过低

问题描述:

直接取列表中所有元素的众数时,自己写的代码得到的结果指标很差,准确率很低,改写成库函数,准确率得到提升

from collections import Counter

...
# 去掉个位数
_line =[(item // 10) *10 for item in line]

numcount = dict(Counter(_line))      
numcount存储了出现的编号以及对应频率
maxValue = 0
maxKey = 0
# 得到出现最多的数
for key ,value in numcount.items():  
    if value > maxValue :
        maxValue = value
        maxKey = key

原因分析:

没分析出来・ࡇ・可能是自己写错了哪里,以后尽可能使用官方自带函数


解决方案:

from collections import Counter
...

# 去掉个位数
_line =[(item // 10) *10 for item in line1]

# 使用库函数
maxKey = Counter(_line).most_common(1)[0][0] 

Counter用法样例:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_47651805/article/details/113621028