简体   繁体   English

计算重复范围内的出现次数

[英]Count number of occurrences in repeated range

I want to count the number of occurrences/events within a range given a Numpy array of numbers. 我想计算给定的Numpy数字数组在一定范围内的发生次数/事件数。

For example, let's consider the array called arr and the result called arr via the function event_count : 例如,让我们考虑称为数组arr ,并呼吁其结果arr通过功能event_count

import numpy as np
arr = np.array([0, 0.2, 0.3, 1, 1.5, 2])
bins = [0, 1, 2]
res = event_count(arr, bins=bins)
print(res)
>>> [3, 2, 1]

This is somewhat similar to what a historgram performs with it's bin argument, but I want to do it without creating a histogram plot. 这有点类似于直方图对其bin参数执行的操作,但我想这样做而不创建直方图。 This is also similar to what bincount does, but I want a range instead of specific instances. 这也类似于bincount所做的事情,但是我想要一个范围而不是特定的实例。 This is also similar to this Finding Occurrences in a Range question, but I want a repeated range. 这也类似于“ 在范围问题中查找发生次数” ,但是我想要重复的范围。

You can use a histogram without using it to plot . 您可以不使用直方图来绘制直方图 Here's an example using the previous code: 这是使用先前代码的示例:

import numpy as np

arr = np.array([0, 0.2, 0.3, 1, 1.5, 2])
bins = [0, 1, 2, 3]
res = np.histogram(arr, bins=bins)
print(res[0])
>>> [3, 2, 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM