NumPy Array Iteration
- Previous Page NumPy Array Reshaping
- Next Page NumPy Array Join
Array iteration
Iteration means to traverse elements sequentially.
When dealing with multi-dimensional arrays in numpy, we can use Python's basic for loop to accomplish this operation.
If we iterate over a 1-D array, it will sequentially traverse each element.
Example
Iterate over the elements of the following 1-D array:
import numpy as np arr = np.array([1, 2, 3]) for x in arr: print(x)
Iterate over the 2-D array
In the 2-D array, it will traverse all rows.
Example
Iterate over the elements of the following 2-D array:
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) for x in arr: print(x)
If we iterate over an n-D array, it will traverse the n-1 dimension sequentially.
To return actual values, scalars, we must iterate over the array in each dimension.
Example
Iterate over each scalar element of the 2-D array:
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) for x in arr: for y in x: print(y)
Iterate over the 3-D array
In the 3-D array, it will traverse all 2-D arrays.
Example
Iterate over the elements of the following 3-D array:
import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: print(x)
To return actual values, scalars, we must iterate over the array in each dimension.
Example
Iterate to scalar:
import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) for x in arr: for y in x: for z in y: print(z)
Use nditer() to iterate over the array
function nditer()
is a helper function that can be used from very basic iteration to very advanced iteration. It solves some of the basic problems we face in iteration, and let's introduce it through examples.
Iterate over each scalar element
In the basic for
In the loop, to iterate over each scalar in the array, we need to use n for
Loops can be difficult to write for arrays with high dimensions.
Example
Traverse the following 3-D array:
import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) for x in np.nditer(arr): print(x)
to iterate over arrays of different data types
We can use op_dtypes
Parameters, and pass the expected data type to change the element data type during iteration.
NumPy does not change the data type of elements in place (elements are located in the array), so it needs some additional space to perform this operation, which is called buffer, in order to nditer()
Enable it in the middle, we pass the parameters flags=['buffered']
.
Example
Traverse the array in string form:
import numpy as np arr = np.array([1, 2, 3]) for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']) print(x)
Iterate with different step lengths
We can use filtering and then iterate.
Example
Skip 1 element every time we traverse a scalar element of the 2D array:
import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) for x in np.nditer(arr[:, ::2]): print(x)
Use ndenumerate() for enumeration iteration
Enumeration refers to the serial number of each mentioned item.
Sometimes, when iterating, we need the corresponding index of the elements, for these cases, we can use ndenumerate()
Method.
Example
Enumerate the elements of the following 1D array:
import numpy as np arr = np.array([1, 2, 3]) for idx, x in np.ndenumerate(arr): print(idx, x)
Example
Enumerate the elements of the following 2D array:
import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) for idx, x in np.ndenumerate(arr): print(idx, x)
- Previous Page NumPy Array Reshaping
- Next Page NumPy Array Join