迭代numpy数组

157

有没有更简洁的替代方法:

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)

有类似的东西存在吗?


我正在使用Python 2.7,并使用您的解决方案与itertools。我在评论中读到使用itertools会更快。然而,(可能是因为我在2.7中),我还必须在for循环中解压缩map。 for x, y in itertools.product(*map(xrange, array.shape)): - ALM
有一个名为“Iterating Over Arrays”的页面在NumPy参考文档中:https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html - Casey
请参考以下链接:https://stackoverflow.com/questions/29493183/most-efficient-way-of-iterating-over-a-numpy-array-by-axis#29495997 - Eulenfuchswiesel
4个回答

216

我认为您正在寻找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

1
请注意,这个代码可以运行,但是非常慢。您最好手动迭代。 - Marty

48
如果您只需要索引,可以尝试使用 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)]

18

请参阅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 *= 0y += 3代替。


2
或使用 y[...] = 3 - Donald Hobson

0

我发现这里没有使用numpy.nditer()的好描述。 因此,我将提供一个。 根据NumPy v1.21 dev0 manual,引入于NumPy 1.6的迭代器对象nditer提供了许多灵活的方式以系统方式访问一个或多个数组的所有元素。

我必须计算mean_squared_error,并且我已经从sklearn的波士顿数据集中获得了y_predicted和y_actual。

def cal_mse(y_actual, y_predicted):
    """ this function will return mean squared error
       args:
           y_actual (ndarray): np array containing target variable
           y_predicted (ndarray): np array containing predictions from DecisionTreeRegressor
       returns:
           mse (integer)
    """
    sq_error = 0
    for i in np.nditer(np.arange(y_pred.shape[0])):
        sq_error += (y_actual[i] - y_predicted[i])**2
    mse = 1/y_actual.shape[0] * sq_error
    
    return mse

希望这可以帮到你 :). 如需进一步解释,请访问

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接