遍历numpy数组
有没有一种更简洁的写法,可以替代这个:
for x in xrange(array.shape[0]):
for y in xrange(array.shape[1]):
do_stuff(x, y)
我想出了这个:
for x, y in itertools.product(map(xrange, array.shape)):
do_stuff(x, y)
这样可以少一个缩进,但看起来还是挺丑的。
我希望能有一种像这样伪代码的写法:
for x, y in array.indices:
do_stuff(x, y)
有没有类似的写法呢?
4 个回答
20
请查看 nditer
import numpy as np
Y = np.array([3,4,5,6])
for y in np.nditer(Y, op_flags=['readwrite']):
y += 3
Y == np.array([6, 7, 8, 9])
直接写
y = 3
是不行的,应该用y *= 0
和y += 3
来代替。
49
如果你只需要索引的话,可以试试 numpy.ndindex
这个工具:
>>> a = numpy.arange(9).reshape(3, 3)
>>> [(x, y) for x, y in numpy.ndindex(a.shape)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
217
我觉得你在找的是 ndenumerate。
>>> a =numpy.array([[1,2],[3,4],[5,6]])
>>> for (x,y), value in numpy.ndenumerate(a):
... print x,y
...
0 0
0 1
1 0
1 1
2 0
2 1
关于性能方面,它比列表推导式稍微慢一点。
X = np.zeros((100, 100, 100))
%timeit list([((i,j,k), X[i,j,k]) for i in range(X.shape[0]) for j in range(X.shape[1]) for k in range(X.shape[2])])
1 loop, best of 3: 376 ms per loop
%timeit list(np.ndenumerate(X))
1 loop, best of 3: 570 ms per loop
如果你担心性能问题,可以通过查看 ndenumerate
的实现来进一步优化。它主要做两件事:转换为数组和循环。如果你确定自己有一个数组,可以直接调用平坦迭代器的 .coords
属性。
a = X.flat
%timeit list([(a.coords, x) for x in a.flat])
1 loop, best of 3: 305 ms per loop