按轴迭代numpy数组的最有效方法? [英] Most efficient way of iterating over a numpy array by axis?
问题描述
我有一个3维的numpy数组. 我想迭代2个暗点,然后在第3个暗点中拉出所有内容. 即:
I have a numpy array with 3 dimensions. I want to iterate over 2 dims and pull everything out in the 3rd dim. I.E:
arr = numpy.random.rand(3,5,5)
for i in range(arr.shape[1]):
for j in range(arr.shape[2]):
print arr[:, i, j]
这是最有效的循环方式吗?我知道numpy为循环提供了更有效的nditer
函数,但似乎无法做到这一点
Is this the most efficient way to loop? I know numpy provides the more efficient nditer
function for looping, but it doesn't seem like it is able to do stuff like this
我将使用的实际数组的大小约为30x256x256
The actual arrays I'll be using will have a size of about 30x256x256
推荐答案
根据最近的问题适应我的回答,您可以使用ndindex
https://stackoverflow.com/a/29467367/901925
Adapting my answer from recent question, you can use ndindex
https://stackoverflow.com/a/29467367/901925
for tup in np.ndindex((arr.shape[1:])):
tup1=(slice(None),tup[0],tup[1])
print arr[tup1]
这使用nditer
生成multi_index
,可以将其与slice
组合以生成所需的索引.
This uses nditer
to generate a multi_index
, which can be combined with a slice
to produce the desired index.
nditer
教程页面还显示了order
和external_loop
属性的混合如何使nditer
返回sub_vector,但这很棘手.
The nditer
tutorial page also shows how a mix of order
and external_loop
properties can make an nditer
return a sub_vector, but that is tricky.
nditer
并不是更有效"或更快.您仍然最终会为每个元素建立索引,或者在第一种情况下为切片创建索引. nditer
作为对问题cython
进行编码的步骤中最有用的.在纯Python中,它与for循环一样慢,甚至更慢.
nditer
isn't more 'efficient' or faster. You still end up indexing each element, or in your case a slice on the 1st dimension. nditer
is most useful as a step toward coding the problem cython
. In pure Python it is just as slow, even slower, than for loops.
nditer
也是很棒的. c[i] = a[i]+b[i]
.
如果必须在最后两个维度上进行迭代,则您执行的操作可能与任何其他方法一样快.其他方法只是隐藏细节.
If you must iterate over the last 2 dimensions, what you are doing is probably as fast as any other method. Other methods just hide details.
您可以探索交换轴或重塑形状,例如arr.reshape(3,-1)
.
You might explore swapping axes, or reshaping, e.g. arr.reshape(3,-1)
.
for x in arr.reshape(3,-1).T:
print(x)
这是我速度的赢家.
这篇关于按轴迭代numpy数组的最有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!