首页 > 解决方案 > 大于或等于python中的分箱

问题描述

我有一个庞大的列表 > 1m 条目。

和我的垃圾箱大小 [0,1,2,3.....1000]

因此,对于 0 bin 大小,所有 >1m 条目都通过,依此类推...

我需要一个快速的解决方案,我尝试对其进行编码,但速度很慢。

任何帮助表示赞赏。谢谢。

Input-
input_list = [0,0,0,1,2,3,55,34,......] (almost 1m in Len)
bins = [0,1,2,....., 1000]

Output-
{0:1.00, 1:0.99, 2:998........1000:0.02}
where key is bin,
      value is ratio of values greater than or equal to particular bin to total entries in list.


标签: pythonpython-3.xpandasnumpy

解决方案


一个非常简单的方法:计算大于元素的元素数并除以记录数。

import numpy as np

data = np.random.randint(2000, size=10**6)
bins = np.arange(1000) 
dic = {}
for bi in bins:
    dic[bi] = np.count_nonzero(data>=bi)/len(data)

推荐阅读