gpt4 book ai didi

python - 找到位于球形区域内的所有点

转载 作者:行者123 更新时间:2023-11-30 22:48:05 27 4
gpt4 key购买 nike

例如,找到下图,它解释了一个简单的 2D 情况的问题。每个点的标签 (N) 和坐标 (x,y) 已知。我需要找到位于红圈内的所有点标签

我的实际问题是在 3D 中,并且点分布不均匀

enter image description here

此处附有包含 7.25 M 点坐标的示例输入文件 point file .

我尝试了以下代码

import numpy as np

C = [50,50,50]

R = 20

centroid = np.loadtxt('centroid') #chk the file attached

def dist(x,y): return sum([(xi-yi)**2 for xi, yi in zip(x,y)])

elabels=[i+1 for i in range(len(centroid)) if dist(C,centroid[i])<=R**2]

单次搜索大约需要 10 分钟。有什么建议可以让它更快吗?

谢谢,普里蒂维

最佳答案

使用 numpy 时,避免在数组上使用列表推导式。

您可以使用这样的矢量化表达式来完成计算

centre = np.array((50., 50., 50.))
points = np.loadtxt('data')

distances2= np.sum((points-centre)**2, axis=1)

points 是一个 N x 2 数组,points-centre 也是一个 N x 2 数组,(points-centre)**2 计算差值每个元素的平方,最终 np.sum(..., axis=1) 对差值的元素求和沿轴编号的平方差1、即跨列。

要过滤位置数组,您可以使用 bool 索引

close = points[distances2<max_dist**2]

关于python - 找到位于球形区域内的所有点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40306661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com