简体   繁体   English

从列表中查找近似模式 python

[英]find approximate mode from a list python

I have a list with values like [62,62,65,67,68,69,3,3,3,1,1,30,30] .我有一个列表,其中包含[62,62,65,67,68,69,3,3,3,1,1,30,30]类的值。

If I create a Counter using this value and take the max occurring value, I get it as 3.如果我使用这个值创建一个Counter并取最大出现值,我得到它为 3。

But, as you can the values 62-69 are kind of close to each other, and I would like to get an average value of these values as the most occurring one.但是,正如您所见,值 62-69 有点接近,我想将这些值的平均值作为出现次数最多的值。 How can we achieve this in python?我们如何在 python 中实现这一点?

You can group your data into ranges, then find the mode For example, arranging your data in ranges of 10, we get您可以将数据分组为范围,然后找到模式 例如,将您的数据排列在 10 个范围内,我们得到

>>> l2 = [e//10 for e in l]
>>> l2
[6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0, 3, 3]

And mode for that is 6模式是 6

>>> Counter(l2).most_common(1)
[(6, 6)]

So your original data should have the mode close to middle of that range, 6*10+5 = 65因此,您的原始数据的模式应该接近该范围的中间,6*10+5 = 65

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM