简体   繁体   English

以有效的方式从 3d numpy 中选择多个值

[英]Selecting multiple values from 3d numpy in efficient way

I have a very large 3d numpy from which I want to extract many values (x, y, z).我有一个非常大的 3d numpy,我想从中提取许多值(x、y、z)。

For the sake of simplicity let's say this is the numpy:为了简单起见,我们假设这是 numpy:

import numpy as np
a = np.arange(64).reshape(4,4,4)

From which I want to extract the values of the following collection of points:我想从中提取以下点集合的值:

points = [[3,0,0],[0,1,0],[3,0,1],[2,3,1]]

In this example, the expected result should be:在这个例子中,预期的结果应该是:

[48,4,49,45]

Because performance metter, I want to avoid iterate like the following code:因为性能计,我想避免像下面的代码那样迭代:

points  = [[3,0,0],[0,1,0],[3,0,1],[2,3,1]]
for i in points:
    print(a[i[0],i[1],i[2]])

Try this.尝试这个。 Uses numpy fancy/advanced indexing .使用 numpy 花式/高级索引

>>> import numpy as np
>>> a = np.arange(64).reshape(4,4,4)

>>> points = [[3,0,0],[0,1,0],[3,0,1],[2,3,1]]
>>> points = np.array(points)

>>> i = points[:, 0]
>>> j = points[:, 1]
>>> k = points[:, 2]
>>> a[i, j, k]
array([48,  4, 49, 45])

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

相关问题 从 3D numpy 数组中选择多个补丁 - Selecting multiple patches from a 3D numpy array 从 3D 矩阵堆栈在 numpy/scipy 中构造 3D 块对角矩阵堆栈的有效方法 - Efficient way of constructing a 3D stack of block diagonal matrix in numpy/scipy from a 3D stack of matrices Numpy 多次掩码数组,并用另一个 3D 数组中的值填充 3D 数组中的 nans - Numpy mask array multiple times and fill nans in 3D array with values from another 3D array 使用 NumPy 将 3D 函数映射到网格的有效方法 - Efficient way to map 3D function to a meshgrid with NumPy 在 3d numpy 数组的一系列二维子数组上执行 function 的更有效方法是什么? - What's a more efficient way to execute a function over a series of 2d subarrays from a 3d numpy array? 从一维 arrays 创建 3D 阵列的有效方法 - Efficient way of creating a 3D array from 1D arrays 从具有 3d 张量的 4d 张量中选择值 - Selecting values from a 4d tensor with a 3d tensor 还有另一种方法可以按“列”值对 3D numpy 数组进行排序吗? - is there another way for sorting 3D numpy array by 'column' values? NumPy:将两个 4D 矩阵中的每对 3D 矩阵相乘的有效方法? - NumPy: Efficient way to multiply each pair of 3D matrices in two 4D matrix? 将函数应用于3D numpy数组的每个2D切片的有效方法 - Efficient way to apply function to each 2D slice of 3D numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM