如何计算或近似列表的中位数而不存储在列表 [英] How to calculate or approximate the median of a list without storing the list

查看:169
本文介绍了如何计算或近似列表的中位数而不存储在列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算一组值的中位数,但我不想存储所有的值,可以吹内存需求。是否有计算或近似中值不存储和整理所有的个人价值的一种方式?

I'm trying to calculate the median of a set of values, but I don't want to store all the values as that could blow memory requirements. Is there a way of calculating or approximating the median without storing and sorting all the individual values?

在理想情况下,我想我写codeA有点像下面的

Ideally I would like to write my code a bit like the following

var medianCalculator = new MedianCalculator();
foreach (var value in SourceData)
{
  medianCalculator.Add(value);
}
Console.WriteLine("The median is: {0}", medianCalculator.Median);

我需要的是实际的MedianCalculator code!

All I need is the actual MedianCalculator code!

更新:有些人问,如果我试图计算中位数知道属性的值。答案是肯定的。一个值是0.5增量约-25​​至-0.5。另一种是,可以在0.5的增量从-120至-60。我想这意味着我可以使用某种形式的直方图的每个值。

Update: Some people have asked if the values I'm trying to calculate the median for have known properties. The answer is yes. One value is in 0.5 increments from about -25 to -0.5. The other is also in 0.5 increments from -120 to -60. I guess this means I can use some form of histogram for each value.

感谢

尼克

推荐答案

如果该值是离散的,不同的值的数目并不算太高,你可以只累积每个值出现在直方图的次数,然后发现从直方图计数的中位数(刚加入了对从直方图的顶部和底部,直到你达到中等)。或者,如果他们是连续的值,你可以分配到箱子里去 - 这不会告诉你确切的中位数,但它会给你一个范围,如果您需要了解更多的precisely你可以遍历列表中再次,检查只在中央仓的元素。

If the values are discrete and the number of distinct values isn't too high, you could just accumulate the number of times each value occurs in a histogram, then find the median from the histogram counts (just add up counts from the top and bottom of the histogram until you reach the middle). Or if they're continuous values, you could distribute them into bins - that wouldn't tell you the exact median but it would give you a range, and if you need to know more precisely you could iterate over the list again, examining only the elements in the central bin.

这篇关于如何计算或近似列表的中位数而不存储在列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆