How To Iterate Over Numpy Array With Examples

1. How To Iterate Over Numpy Array.

  1. NumPy provides the nditer() function to get the iterator object that can be used in conjunction with the for loop to iterate over array elements.
  2. The following example uses the range() function to create a 2*3 array and nditer to generate an iterator object.
    import numpy as np
    
    def numpy_array_iterator_example():
        # create a numpy integer number array, start from 0, end with 30, step is 5.
        array = np.arange(0,30,5)
        print('array: ', array)
    
        # reshape the array to a 2 dimensional array that has 2 rows and 3 columns.
        array = array.reshape(2,3)
        print('array.reshape(2,3): ', array)
    
        # Use the nditer() method to get the array iterator.
        it = np.nditer(array)
        
        # use the for loop to get each element in the array.
        for x in it:
           
            print(x)     
    
    if __name__ == '__main__':
        
        numpy_array_iterator_example()
  3. Below is the above example execution output.
    array:  [ 0  5 10 15 20 25]
    
    array.reshape(2,3):  
    [[ 0  5 10]
     [15 20 25]]
    
    0
    5
    10
    15
    20
    25
    

2. Numpy Array Traversal Order.

  1. In memory, the NumPy array provides two ways to store data: C-order (row priority order) and Fortrant-order (column priority order).
  2. The nditer iterator selects an order consistent with the memory layout of the array. The reason for doing so is to improve the efficiency of data access.
  3. By default, when we traverse the elements in the array, we do not need to consider the storage order of the array, this can be verified by traversing the transposed array of the above array in section 1, below is the example source code.
    import numpy as np
    
    def numpy_array_iterator_order_example():
        # create a numpy integer number array, start from 0, end with 30, step is 5.
        array = np.arange(0,30,5)
        print('array: ', array)
    
        # reshape the array to a 2 dimensional array that has 2 rows and 3 columns.
        array = array.reshape(2,3)
        print('array.reshape(2,3): ', array)
        
        # transfer the array to a 2D array with 3 rows and 2 columns.
        array_transposed = array.T
        print('array.T: ', array_transposed)
    
        # Use the nditer() method to get the array_transposed iterator.
        it = np.nditer(array_transposed)
        
        # use the for loop to get each element in the array.
        # from the result 
        for x in it:
           
            print(x)            
    
    if __name__ == '__main__':
        
        numpy_array_iterator_order_example()
  4. Below is the above example execution output.
    array:  [ 0  5 10 15 20 25]
    
    array.reshape(2,3):  
    [[ 0  5 10]
     [15 20 25]]
    
    array.T:  [[ 0 15]
     [ 5 20]
     [10 25]]
    
    0
    5
    10
    15
    20
    25
  5. From the output of the example, we can see that the traversal order of array and array.T is the same, that is to say, their storage order in memory is the same.
  6. In the following example, a copy of the transposed array is accessed in C order ( array.T.copy(order=‘C’) ) which uses the row order to store the array element.
    import numpy as np
    
    def numpy_array_iterator_copy_with_c_order_example():
        # create a numpy integer number array, start from 0, end with 30, step is 5.
        array = np.arange(0,30,5)
        print('array: ', array)
    
        # reshape the array to a 2 dimensional array that has 2 rows and 3 columns.
        array = array.reshape(2,3)
        print('array.reshape(2,3): ', array)
        
        # transfer the array to a 2D array with 3 rows and 2 columns.
        array_transposed = array.T.copy(order='C')
        print('array.T.copy(order=\'C\'): ', array_transposed)
    
        # Use the nditer() method to get the array_transposed iterator.
        it = np.nditer(array_transposed)
        
        # use the for loop to get each element in the array.
        # from the result 
        for x in it:
           
            print(x)                      
    
    if __name__ == '__main__':
        
        numpy_array_iterator_copy_with_c_order_example()
  7. From the example execution result below, we can see that array.T.copy(order=‘C’) has a different result from array traversal in examples 1 and 2. The reason is that they are stored differently in memory.
    array:  [ 0  5 10 15 20 25]
    
    array.reshape(2,3):  [[ 0  5 10]
     [15 20 25]]
    
    array.T.copy(order='C'):  [[ 0 15]
     [ 5 20]
     [10 25]]
    
    0
    15
    5
    20
    10
    25
    

3. How To Specify Traversal Order When Iterate Numpy Array.

  1. You can specify the order in which the array is traversed through the order parameter of the nditer object. Below is the example.
    import numpy as np
    
    def numpy_array_iterator_specify_order_example():
        # create a numpy integer number array, start from 0, end with 30, step is 5.
        array = np.arange(0,30,5)
        print('array: ', array)
    
        # reshape the array to a 2 dimensional array that has 2 rows and 3 columns.
        array = array.reshape(2,3)
        print('array.reshape(2,3): ', array)
    
        # Use the nditer() method to get the array iterator by row.
        it_1 = np.nditer(array, order='C')
        
        print('get array elements by row order.\n')
        
        # use the for loop to get each element in the array.
        for x in it_1:
           
            print(x)    
            
        # Use the nditer() method to get the array iterator by column.
        it_2 = np.nditer(array, order='F')
        
        print('\n')
        
        print('get array elements by column order.\n')
        
        # use the for loop to get each element in the array.
        for x in it_2:
           
            print(x)                                                
    
    if __name__ == '__main__':
        
        numpy_array_iterator_specify_order_example()
  2. Below is the above example execution result.
    array:  [ 0  5 10 15 20 25]
    array.reshape(2,3):  [[ 0  5 10]
     [15 20 25]]
    get array elements by row order.
    
    0
    5
    10
    15
    20
    25
    
    
    get array elements by column order.
    
    0
    15
    5
    20
    10
    25

4. Modify Array Element Values.

  1. The nditer object provides an optional parameter op_flags, which indicates whether the elements can be modified or not when traversing the array.
  2. The op_flags parameter provides three mode values as below.
  3. read-only: In the read-only mode the elements in the array cannot be modified during traversal.
  4. read-write: In the read-write mode, the element value can be modified during traversal.
  5. write-only: In the write-only mode, the element value can be modified during traversal.
  6. Below is the example source code.
    import numpy as np
    
    def numpy_array_iterator_modify_element_value_example():
        # create a numpy integer number array, start from 0, end with 30, step is 5.
        array = np.arange(0,30,5)
        print('array: ', array)
    
        # reshape the array to a 2 dimensional array that has 2 rows and 3 columns.
        array = array.reshape(2,3)
        print('array.reshape(2,3): ', array)
        
        # Use the nditer() method to get the array_transposed iterator.
        it = np.nditer(array, op_flags=['readwrite'])
        
        # use the for loop to get each element in the array.
        # from the result 
        for x in it:
           
            x[...] = x*x 
            
        print('The array after modified: \n', array)                                          
    
    if __name__ == '__main__':
        
        numpy_array_iterator_modify_element_value_example()
  7. When you run the above example, you will get the below output.
    array:  [ 0  5 10 15 20 25]
    
    array.reshape(2,3):  [[ 0  5 10]
     [15 20 25]]
    
    The array after modified: 
     [[  0  25 100]
     [225 400 625]]
    

5. Iterate Over Multiple Numpy Arrays.

  1. If both the 2 arrays can be broadcast ( that is, the smaller array x can be broadcast into the larger array y ), the nditer object can iterate over them at the same time.
  2. In the following example, the first NumPy array array1 has dimensions 4*3 and the second NumPy array array2 has dimensions 1*3.
  3. The second array must have the dimension 1*3, otherwise, it will throw the below errors.
        for x,y in np.nditer([array1, array2]):
    ValueError: operands could not be broadcast together with shapes
  4. Below is the example source code.
    import numpy as np
    
    def numpy_array_iterator_multiple_array_example():
        
        # create the first numpy 2d array with 4 rows and 3 columns..
        array1 = np.arange(0, 12, 1).reshape(4, 3)
        print('array1:\n ', array1)
        print('array1.ndim:\n ', array1.ndim)
    
        # create the second numpy 2d array, it must be 1 rows and 3 columns, otherwise, when you run the example, you will meet errors.
        array2 = np.arange(0, 3, 1).reshape(1, 3)
        print('array2:\n ', array2)
        print('array2.ndim:\n ', array2.ndim)
        
        
        for x,y in np.nditer([array1, array2]):
            
            print ("%d : %d" % (x,y),end=" ,\n ")
                                                   
    
    if __name__ == '__main__':
        
        numpy_array_iterator_multiple_array_example()
  5. When you run the above example, you will get the below output.
    array1:
      [[ 0  1  2]
     [ 3  4  5]
     [ 6  7  8]
     [ 9 10 11]]
    array1.ndim:
      2
    array2:
      [[0 1 2]]
    array2.ndim:
      2
    0 : 0 ,
     1 : 1 ,
     2 : 2 ,
     3 : 0 ,
     4 : 1 ,
     5 : 2 ,
     6 : 0 ,
     7 : 1 ,
     8 : 2 ,
     9 : 0 ,
     10 : 1 ,
     11 : 2 ,

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.