首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >大于或等于python中的binning

大于或等于python中的binning
EN

Stack Overflow用户
提问于 2020-08-30 12:13:24
回答 2查看 821关注 0票数 2

我有一个巨大的列表>100万条条目。

我的垃圾箱大小是0,1,2,3.1000

所以,对于0 bin大小所有>1米的条目传递等等.

我需要一个快速的解决方案,我试着编写它,但它是相当慢的。

任何帮助都是非常感谢的。谢谢。

代码语言:javascript
复制
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.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-30 13:07:03

一种非常简单的方法:计算大于元素的元素的no,再除以记录的no。

代码语言:javascript
复制
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)
票数 2
EN

Stack Overflow用户

发布于 2020-08-30 12:39:21

如果我正确理解你的问题,你可以使用numpy.histogram。如果您在您自己的input_listbins中替换了下面的代码块

代码语言:javascript
复制
import numpy as np

# Filling in dummy data
input_list = [np.random.randint(low=0, high=100) for i in range(100)]

# Setup bins as [1, 2, 3, ... 100]
bins = [i for i in range(1, 101)]

# Run numpy.histogram
hist, bin_edges = np.histogram(input_list, bins=bins)

# Find cumulative sum
cumsum = np.array([sum(hist[:i]) for i in range(len(hist))])

# Find ratios
ratios = (len(data) - cumsum) / len(data)

ratios变量包含您要查找的内容,即大于或等于特定bin的值的比率。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63657305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档