Numpy is a Python library that is mostly used in array computing as well as mathematical operations like linear algebra, Fourier transform, and random number capabilities. The NumPy array is basic to most scientific computing tools in Python. In this article, we will discuss methodologies to iterate over NumPy arrays.
Iterating over a 1-D NumPy Array
We can iterate over a 1-D NumPy array using the for loop. Consider the following example:
import numpy as np arr = np.array([1, 2, 3]) for x in arr: print(x)
1 2 3
Iterating over 2-D NumPy Arrays
We can iterate over a 2D NumPy array using two nested for loops as follows:
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) for row in arr: for x in row: print(x)
1 2 3 4 5 6
Iterating over 3-D NumPy Arrays
We can iterate over a 3D NumPy array using three nested for loops as follows:
import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) for d3 in arr: for d2 in d3: for x in d2: print(x)
1 2 3 4 5 6 7 8
Iterating using nditer()
Suppose a 2D array is created and as we all know there exists two indices to refer the array element. In iterating over the elements of an array, there exist a lot of methods, but the one that is widely used is the nditer() method. In the following example, we create an array if size (3, 4) and then we iterate over its elements using nditer().
import numpy as np arr = np.arange(12).reshape(3,4) for x in np.nditer(arr): print(x)
0 1 2 3 4 5 6 7 8 9 10 11
Iterating using ndenumerate()
The ndenumerate() method will return an iterator yielding a 2-tuple of the index of the element and the element itself.
import numpy as np arr = np.arange(12).reshape(3,4) for index, x in np.ndenumerate(arr): print(index, x)
(0, 0) 0 (0, 1) 1 (0, 2) 2 (0, 3) 3 (1, 0) 4 (1, 1) 5 (1, 2) 6 (1, 3) 7 (2, 0) 8 (2, 1) 9 (2, 2) 10 (2, 3) 11
index will have an array in the form of indexes of the values while iterating the data element-wise. Here, as we have 3 rows and 4 columns so it will return us 12 elements but the important thing is that it will also give us the index of that element. For the first case, the first element is 0 and its location/index is (0,0).
Iterating with Different Step Sizes
Apart from iterating over the array in the natural style(which is sequential order), we can also traverse over the elements of the element by skipping a few steps. We can do so in both the loops using range(), however, the other way is to use the slicing operation for the same. In this example, let us consider a single-dimensional array.
import numpy as np arr = np.arange(12) for x in np.nditer(arr[::2]): print(x)
0 2 4 6 8 10
In the above code, slicing has been done is to iterate the array in the increment of two steps as given through the use of
::2. This slicing iteration is not to iterate through the normal procedure, rather in this operation, the iteration is to be performed array-wise rather than element-wise. The array can be sliced for both rows as well as columns.
Modifying Array Values While Iterating
We can modify elements of the array while iterating over it. This can be done using the writeback parameter. In the following example, every array element is multiplied by 2.
import numpy as np arr = np.arange(8).reshape(2,4) for x in np.nditer(arr, op_flags=['readwrite']): x[...] = x * 2 print(arr)
[[ 0 2 4 6] [ 8 10 12 14]]
In the above code, we have used the op_flags parameter, the readwrite flag specifies that the array can be used both for reading and writing.