选择分割数据集的范围

发布于 2024-09-18 03:29:39 字数 171 浏览 3 评论 0原文

我有几百万个 0 到 64K 之间的整数。我想将它们分成 N 个桶,其中每个桶包含来自连续范围的大约相同数量的项目。例如,如果我只有一个包含每个可能值的数据点和 64 个存储桶,那么理想情况下我最终会得到 0-1024 的存储桶、1025-2048 的存储桶等。

计算存储桶的算法是什么项目数量分布最均匀的范围?

I have a few million integers between 0 and 64K. I'd like to split them up into N buckets, where each bucket contains about the same number of items from a contiguous range. So for example, if I only had a single datapoint with each possible value, and 64 buckets, ideally I'd end up with a bucket for 0-1024, one for 1025-2048, etc.

What is an algorithm for calculating the bucket ranges that most evenly distributes the number of items?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心清如水 2024-09-25 03:29:41

如果您专注于均匀分布,最简单的方法可能是对列表进行排序,然后将第一个 (list_length / N) 项放入第一个存储桶中,然后将下一个 ( list_length / N) 项放入下一个存储桶中,等等。由于您有一个相当大的列表需要排序,这可能不是最有效的解决方案。

If you are focusing on even distribution, the easiest way to go would probably be to sort the list and then place the first (list_length / N) items into the first bucket, then the next (list_length / N) items into the next bucket, etc. Since you have a rather large list to sort, this probably isn't the most efficient solution.

向日葵 2024-09-25 03:29:41

一种可能性是,在浏览排序列表时对数字进行排序并填充包含所需数量元素的桶。

您可以使用 执行类似的操作,但可能更快:您填充堆上您的元素,然后您可以非常快速地提取最小的 list_length/N 元素。

然而,如果速度不是太重要的话,对 100 万个数字进行排序既简单又快速(在使用 Numpy 的 Python 中只需几分之一秒)。

Sorting your numbers and filling buckets that contain the desired number of elements as you go through the sorted list is one possibility.

You can do something similar but probably faster by using a heap: you fill the heap with your elements, and you can then extract the smallest list_length/N elements very fast.

If speed is not too much of a concern, however, sorting 1 million numbers is both simple and fast (a fraction of a second in Python with Numpy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文