【问题标题】:Binning 2D data into overlapping circles in x,y将 2D 数据分箱成 x,y 中的重叠圆圈
【发布时间】:2019-01-22 19:12:35
【问题描述】:

我目前正在处理一个相当大的 3D 点 (x,y,z) 数据集,并且想要一种有效的方法来确定哪些点位于 xy 平面中的一组圆内,半径为 r 和中心 ( x1,y1),其中 x1 和 y1 是网格坐标(每个长度为 120)。圆圈会重叠,有些点会属于多个圆圈。

所以输出将是 14400 个圆圈 (120*120) 的身份,以及 (x,y,z) 列表中的哪些点在每个圆圈中。

import numpy as np

def inside_circle(x, y, x0, y0, r):
    return (x - x0)*(x - x0) + (y - y0)*(y - y0) < r*r

x = np.random.random_sample((10000,))
y = np.random.random_sample((10000,))

x0 = np.linspace(min(x),max(x),120)
y0 = np.linspace(min(y),max(y),120)

idx = np.zeros((14400,10000))
r = 2
count = 0

for i in range(0,120):
    for j in range(0,120):
        idx[count,:] = inside_circle(x,y,x0[i],y0[j],r)
        count = count + 1

其中 inside_circle 是一个函数,它为半径为 r 的圆中的每个测试点 x,y,z 提供一个布尔值 True 或 False 数组,中心为 x0[i] 和 x0[j]

我的主要问题是是否有比嵌套 for 循环更有效的方法?或者甚至更有效的方法在这里做任何事情——因为我对 python 很陌生。

感谢您的回复!

亚历克。

【问题讨论】:

  • 请阅读How to create a Minimal, Complete, and Verifiable example。人们无法运行您当前的代码
  • 3d 中的一个点怎么可能在一个圆内?你的意思是在z轴方向投影圆给出的圆柱体内部?
  • 是的,我的意思是在圆柱体内(所以只是 x,y 平面上的圆圈)

标签: python numpy for-loop histogram binning


【解决方案1】:

这个使用数组广播,比嵌套的 for 循环稍快(在我的机器上是 0.5 秒对 0.8 秒)。在我看来,可读性有所下降。

import numpy as np

x = np.random.random_sample((1, 10000))
y = np.random.random_sample((1, 10000))

x0 = np.reshape(np.linspace(np.min(x),np.max(x),120), (120, 1))
y0 = np.reshape(np.linspace(np.min(y),np.max(y),120), (120, 1))

r = 2

all_xdiffssquared = np.subtract(x, x0)**2
all_ydiffssquared = np.subtract(y, y0)**2
# 3d here means that the array has 3 dimensions. Not the geometry described
all_xdiffssquared_3d = np.reshape(all_xdiffssquared, (120, 10000, 1))
all_ydiffssquared_3d = np.reshape(np.transpose(all_ydiffssquared), (1, 10000, 120))
all_distances_3d = all_xdiffssquared_3d + all_ydiffssquared_3d - r**2
idx = np.signbit(np.reshape(np.moveaxis(all_distances_3d, 1, -1), (14400, 10000)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2016-07-23
    • 2016-07-16
    相关资源
    最近更新 更多