简体   繁体   English

Numpy 直方图二维向量

[英]Numpy historgramm 2d vector

在此处输入图像描述

I have a list of xy like the picture above我有一个 xy 列表,如上图

in code it works like this:在代码中它是这样工作的:

np.array([[1.3,2.1],[1.5,2.2],[3.1,4.8]])

now I would like to set a grid of which I can set the start, the number of columns and rows as well as the row and columns size, and then count the number of points in each cell.现在我想设置一个网格,我可以设置它的开始、列数和行数以及行和列大小,然后计算每个单元格中的点数。

in this example [0,0] has 1 point in it, [1,0] has 1, [2,0] has 3, [0,1] has 4 and so on.在这个例子中,[0,0] 有 1 个点,[1,0] 有 1,[2,0] 有 3,[0,1] 有 4,依此类推。

I know it would probably be trivial to do by hand, even without numpy, but I need to create it as fast as possible, since I will have to process a ton of data this way.我知道手工操作可能很简单,即使没有 numpy,但我需要尽快创建它,因为我必须以这种方式处理大量数据。

whats a good way to do this?什么是这样做的好方法? Basicly create a 2D Histogramm of points?基本上创建点的二维直方图? And more importantly, how can I do it as fast as possible?更重要的是,我怎样才能尽可能快地做到这一点?

I think numpy.histogram2d is the best option.我认为numpy.histogram2d是最好的选择。

a = np.array([[1.3,2.1],[1.5,2.2],[3.1,4.8]])
H, _, _ = np.histogram2d(a[:, 0], a[:, 1], bins=(range(6), range(6)))
print(H)
# [[0. 0. 0. 0. 0.]
#  [0. 0. 2. 0. 0.]
#  [0. 0. 0. 0. 0.]
#  [0. 0. 0. 0. 1.]
#  [0. 0. 0. 0. 0.]]

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

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