python - 如何加速 numpy.all 和 numpy.nonzero()?

标签 python numpy

我需要检查一个点是否位于边界长方体内。长方体的数量非常大(~4M)。我想出的代码是:

import numpy as np

# set the numbers of points and cuboids
n_points = 64
n_cuboid = 4000000

# generate the test data
points = np.random.rand(1, 3, n_points)*512
cuboid_min = np.random.rand(n_cuboid, 3, 1)*512
cuboid_max = cuboid_min + np.random.rand(n_cuboid, 3, 1)*8

# main body: check if the points are inside the cuboids
inside_cuboid = np.all((points > cuboid_min) & (points < cuboid_max), axis=1)
indices = np.nonzero(inside_cuboid)
运行需要8秒np.all和 3 秒运行 np.nonzero在我的电脑上。有什么想法可以加快代码速度吗?

最佳答案

我们可以减少 all-reduction 的内存拥塞与 slicing沿3的最小轴长获取 inside_cuboid ——

out = (points[0,0,:] > cuboid_min[:,0]) & (points[0,0,:] < cuboid_max[:,0]) & \
      (points[0,1,:] > cuboid_min[:,1]) & (points[0,1,:] < cuboid_max[:,1]) & \
      (points[0,2,:] > cuboid_min[:,2]) & (points[0,2,:] < cuboid_max[:,2])
时间——
In [43]: %timeit np.all((points > cuboid_min) & (points < cuboid_max), axis=1)
2.49 s ± 20 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [51]: %%timeit
    ...: out = (points[0,0,:] > cuboid_min[:,0]) & (points[0,0,:] < cuboid_max[:,0]) & \
    ...:       (points[0,1,:] > cuboid_min[:,1]) & (points[0,1,:] < cuboid_max[:,1]) & \
    ...:       (points[0,2,:] > cuboid_min[:,2]) & (points[0,2,:] < cuboid_max[:,2])
1.95 s ± 10.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

关于python - 如何加速 numpy.all 和 numpy.nonzero()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63967363/

相关文章:

python - 将核苷酸位置与 fasta 文件中的序列进行匹配

python - 谷歌应用引擎: store uploaded file in Google Cloud Storage

python - numpy.linalg.eig 如何决定返回特征值的顺序?

python - 解压缩坐标和值的字典

numpy - 为什么特征向量与对应特征值的乘积不等于原矩阵与特征向量的乘积?

python mpl : how to plot a point on a graph of 3D vectors, 和箭头头

Python - 为 3d 线图着色

python - 将结果从 Python 返回到 Vba

python - 如何在整个 numpy 矩阵中获取最大(顶部)N 个值

python - 基于稀疏信息填充数组