python - 大于或等于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.
解决方案
一个非常简单的方法:计算大于元素的元素数并除以记录数。
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)
推荐阅读
- python - 如何将 python 程序的标准输出输入 Popen 标准输入?
- sensors - Arduino 激光检测 - 光敏电阻或激光传感器模块?
- java - 构建部分中的 Maven 版本范围
- uuid - 为什么 UUID 被称为 128 位数字,即使它包含字母?
- android - 打开 PDF 文件将打开一个空白 PDF
- php - 如何使用类常量作为绑定参数服务的标签?
- php - 非 HTTPS 站点的 SSL 错误日志记录
- bash - 对文件进行排序,然后将它们传递给 awk 进行搜索
- mysql - 更新对mysql表的数组响应
- python - 试图弄清楚如何使 python 乌龟图形的画布更小,但它不能正常工作