跟踪NumPy中的多索引和修改值

bad*_*lms 4 python iteration numpy multi-index numpy-ndarray

我正在迭代一个2D数组,以使用索引值进行计算,然后将计算出的值分配给所述索引。

在NumPy文档中,提供了一个使用迭代器修改值的示例

for x in np.nditer(a, op_flags=['readwrite']):
    x[...] = 2 * x
Run Code Online (Sandbox Code Playgroud)

但是,使用以下方法跟踪索引时,这似乎不起作用:

it = np.nditer(a, flags=['multi_index'])
while not it.finished:
    it[...] = . . .
    it.iternext()
Run Code Online (Sandbox Code Playgroud)

但是,我可以使用这些it.multi_index值,但这似乎不必要。是否有更简单的方法可以通过不同的方法或不同的语法来实现?

it = np.nditer(a, flags=['multi_index'])
while not it.finished:
    matrix[it.multi_index[0]][it.multi_index[1]] = . . .
    it.iternext()
Run Code Online (Sandbox Code Playgroud)

编辑

这是一个multi_index尝试使用迭代器索引和失败来修改值的迭代示例。

matrix = np.zeros((5,5))
it = np.nditer(matrix, flags=['multi_index'])
while not it.finished:
  it[...] = 1
  it.iternext()
Run Code Online (Sandbox Code Playgroud)

产生的错误是

TypeError                                 Traceback (most recent call last)
<ipython-input-79-3f4cabcbfde6> in <module>()
     25 it = np.nditer(matrix, flags=['multi_index'])
     26 while not it.finished:
---> 27   it[...] = 1
     28   it.iternext()

TypeError: invalid index type for iterator indexing
Run Code Online (Sandbox Code Playgroud)

hpa*_*ulj 5

在您的第一个迭代示例中:

In [1]: arr = np.arange(12).reshape(3,4)
In [2]: for x in np.nditer(arr, op_flags=['readwrite']):
   ...:     print(x, type(x))
   ...:     x[...] = 2 * x
   ...:     
0 <class 'numpy.ndarray'>
1 <class 'numpy.ndarray'>
2 <class 'numpy.ndarray'>
3 <class 'numpy.ndarray'>
4 <class 'numpy.ndarray'>
....
11 <class 'numpy.ndarray'>
In [3]: x
Out[3]: array(22)
In [4]: arr
Out[4]: 
array([[ 0,  2,  4,  6],
       [ 8, 10, 12, 14],
       [16, 18, 20, 22]])
Run Code Online (Sandbox Code Playgroud)

打开multi_index:

In [9]: it = np.nditer(arr, flags=['multi_index'],op_flags=['readwrite'])
In [10]: while not it.finished:
    ...:     print(it[0], it.multi_index)
    ...:     it.iternext()
    ...:     
0 (0, 0)
2 (0, 1)
4 (0, 2)
...
20 (2, 2)
22 (2, 3)
Run Code Online (Sandbox Code Playgroud)

通过的元素进行相同的迭代arr,但还会生成2d索引元组。itnditer具有各种方法和属性的对象。在这种情况下,它具有一个multi_index属性。并且当前的迭代变量在中it[0]

我可以通过[...]就地或通过索引来修改元素arr

In [11]: it = np.nditer(arr, flags=['multi_index'],op_flags=['readwrite'])
In [13]: while not it.finished:
    ...:     it[0][...] *= 2
    ...:     arr[it.multi_index] += 100
    ...:     it.iternext()
    ...:     

In [14]: arr       # values are doubled and add by 100
Out[14]: 
array([[100, 104, 108, 112],
       [116, 120, 124, 128],
       [132, 136, 140, 144]])
Run Code Online (Sandbox Code Playgroud)

没有multi_index我,我仍然可以创建一个nditer对象,并使用while not finished语法进行迭代。除了访问外,x[...]我还必须使用it[0][...]


np.ndindex是生成的更便捷的方法multi_index。看一下它的代码。它是使用的少数numpy函数之一np.nditer

In [26]: for idx in np.ndindex(arr.shape):
    ...:     print(idx)
    ...:     arr[idx] -= 100
    ...:     
(0, 0)
(0, 1)
...
(2, 3)
In [27]: arr
Out[27]: 
array([[ 0,  4,  8, 12],
       [16, 20, 24, 28],
       [32, 36, 40, 44]])
Run Code Online (Sandbox Code Playgroud)

尽管玩起来很有趣nditer,但这是不实际的,至少在纯Python代码中不是这样。作为在cythonc代码中使用它的垫脚石,它最有用。请参阅迭代页面的最终示例。