Iterative arrays
NumPy introduces the nditer object to provide a way to access array elements.
I. Single Array Iteration
1. use nditer to access each element of the array
>>>a = (12).reshape(3, 4) >>>for x in (a): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11 # The above examples do not use standard C or Fortran order, the order chosen is consistent with the array memory layout. # This is done to improve access efficiency, and the default is row-major order (or C-order). # This reflects the default of just accessing each element without regard to its particular order. # We can see this by iterating over the transpose of the above array. # and compare it to the copy method, which accesses the array transpose in C order, as in the following example: >>>for x in (): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11 >>>for x in ((order='C')): print(x, end=' ') 0 4 8 1 5 9 2 6 10 3 7 11
2. Control the order of iteration of the elements of the array
The order in which the elements are accessed is controlled using the parameter order, which has the following optional values:
- 'C': C order, i.e., the line order is prioritized;
- 'F': Fortran order, i.e. column order first;
- 'K': reference to the order of the array elements in memory;
- 'A': indicates 'F' order;
>>>a = (12).reshape(3, 4) >>>for x in (a, order='C'): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11 >>>a = (12).reshape(3, 4) >>>for x in (a, order='F'): print(x, end=' ') 0 4 8 1 5 9 2 6 10 3 7 11 >>>a = (12).reshape(3, 4) >>>for x in (a, order='K'): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11 >>>a = (12).reshape(3, 4) >>>for x in (a, order='A'): print(x, end=' ') 0 1 2 3 4 5 6 7 8 9 10 11
3. Modification of array values
When iterating over arrays with nditer objects, the default is read-only. Therefore, if you need to modify the array, you can use the parameter op_flags = 'readwrite' or 'writeonly' to flag it for read-write or read-only mode.
At this point, nditer will generate a writable buffer array during iteration where modifications can be made. In order to be able to write the modified data back to the original location after modification, it is necessary to throw an end-of-iteration signal at the end of the iteration in two ways:
- Use the with context manager;
- At the end of the iteration, the close method of the iterator is called;
>>>a = (12).reshape(3, 4) >>>print(a) >>>with (a, op_flags=['readwrite']) as it: for x in it: x += 10 >>>print(a) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[10 11 12 13] [14 15 16 17] [18 19 20 21]]
4. Use of external loops, tracking indexes or multiple indexes
The above operations are performed element-by-element during iteration, which is simple but inefficient. You can use the flags argument to make nditer iterate over larger blocks. It is also possible to force the C and F order to get different block sizes.
# Maintain native memory order by default, iterators provide a single one-dimensional array # 'external_loop' gives a one-dimensional array with multiple values, not a zero-dimensional array >>>a = (12).reshape(3, 4) >>>print(a) >>>for x in (a, flags=['external_loop']): print(x, end=' ') [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [ 0 1 2 3 4 5 6 7 8 9 10 11], # Set the 'F' order >>>a = (12).reshape(3, 4) >>>print(a) >>>for x in (a, flags=['external_loop'], order='F'): print(x, end=' ') [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [0 4 8], [1 5 9], [ 2 6 10], [ 3 7 11], # 'c_index' can be used to track 'C' order indexes through the >>>a = (12).reshape(3, 4) >>>print(a) >>>it = (a, flags=['c_index']) >>>for x in it: print("{}: ({})".format(x, )) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 0: (0) 1: (1) 2: (2) 3: (3) 4: (4) 5: (5) 6: (6) 7: (7) 8: (8) 9: (9) 10: (10) 11: (11) # 'f_index' can be used to track 'F' order indexes through the >>>a = (12).reshape(3, 4) >>>print(a) >>>it = (a, flags=['c_index']) >>>for x in it: print("{}: ({})".format(x, )) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 0: (0) 1: (3) 2: (6) 3: (9) 4: (1) 5: (4) 6: (7) 7: (10) 8: (2) 9: (5) 10: (8) 11: (11) # 'multi_index' keeps track of array indexes with it.multi_index >>>a = (12).reshape(3, 4) >>>print(a) >>>it = (a, flags=['multi_index']) >>>for x in it: print("{}: {}".format(x, it.multi_index)) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 0: (0, 0) 1: (0, 1) 2: (0, 2) 3: (0, 3) 4: (1, 0) 5: (1, 1) 6: (1, 2) 7: (1, 3) 8: (2, 0) 9: (2, 1) 10: (2, 2) 11: (2, 3)
external_loop together with multi_index、c_index、c_indexCannot be used at the same time,Otherwise an error will be raised ValueError: Iterator flag EXTERNAL_LOOP cannot be used if an index or multi-index is being tracked
5. Iterating with specific data types
When it is necessary to iterate over an array with other data types, there are two ways to do it:
- Temporary Copies: When iterating, a copy of the array is created with the new data type and then the iteration is done in the copy. However, this method consumes a lot of memory space.
- Buffered Mode: Uses buffering to support flexible inputs with minimal memory overhead.
# Temporary copies >>>a = (12).reshape(3, 4) >>>print() >>>it = (a, op_flags=['readonly', 'copy'],op_dtypes=[np.float64]) >>>for x in it: print("{}".format(x), end=', ') int32 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, # Buffered mode >>>a = (12).reshape(3, 4) >>>print() >>>it = (a, flags=['buffered'],op_dtypes=[np.float64]) >>>for x in it: print("{}".format(x), end=', ') int32 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0,
take note of
By default, the conversion enforces the 'safe' mechanism and throws an exception if it does not conform to NumPy's conversion rules: TypeError: Iterator operand 0 dtype could not be cast from dtype('float64') to dtype('float32') according to the rule 'safe'
II. Broadcast Array Iteration
If arrays of different shapes are broadcastable, then dtype can iterate over multiple arrays.
>>> a = (3) >>> b = (6).reshape(2,3) >>> for x, y in ([a,b]): print("%d:%d" % (x,y), end=' ') 0:0 1:1 2:2 0:3 1:4 2:5
to this article on the implementation of NumPy iterative arrays to this article, more related to NumPy iterative arrays content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!