NumPy Iterator

In this tutorial you will learn:

  • Iterating Arrays using nditer
  • DataType Casting
  • Iterating with Step Sizing

Iterating Arrays using nditer()

nditer is an iterator object that was first introduced in NumPy 1.6. For a multi dimensional array it becomes a cumbersome task to write a For loop for each dimension, so nditer provides a flexibility to efficiently and systematically iterate a complete n-dimensiona array.

In this example I will iterate a 2D array using nditer(). Here you can observe that by using nditer() the task of iteration appears more easy, compact and straight forward.

  1. import numpy as np
  2. my_arr = np.array([['s','o','u','r','c','e'],['c','o','d','e','s','t']])
  3. print('2D Array which we will be iterating is:\n ',my_arr)
  4. print('The contents of 2D array are:')
  5. for z in np.nditer(my_arr):
  6. print(z, end=' ')

In this example I will iterate a 3D using nditer(), here you can observe that there is no need for explicit nested for loops and same syntax of nditer() can be used for 2D, 3D and n dimensions arrays.

  1. import numpy as np
  2. my_arr = np.array([[['s','o','u','r','c','e'],['c','o','d','e','s','t'],['t','u','t','o','r','i']]])
  3. print('3D Array which we will be iterating is:\n ',my_arr)
  4. print('The contents of 3D array are:')
  5. for z in np.nditer(my_arr):
  6. print(z, end=' ')

DataType Casting

Sometimes it is necessary to work on an array as a different Data Type than it stored initially, although it is better to let the compiler handle the buffering/ copying instead of casting data type in inner loop. There are two methods through which casting can be done, one is buffering(flags=['buffered']) and other one is making temporary copies(op_flags=['readonly','copy']).

In this example I have casted DataType float to complex64 and used op_flags as a parameter of nditer() function.

  1. import numpy as np
  2. my_arr = np.array([1,2,3],dtype='f')
  3. print('Array which we will be iterating is:\n ',my_arr)
  4. print('DataType of Array which we will be iterating is:\n ',my_arr.dtype)
  5. print('The iterated contents of array are:')
  6. for x in np.nditer(my_arr,op_flags=['readonly','copy'] ,op_dtypes=['complex64']):
  7. print(x)
  8. print('The DataType of final result is :',x.dtype)

Iterating with Step Sizing

nditer() also provides the flexibility to the programmer by iterating with different step size. To carry out desired iteration with different step size we pass the array to nditer() function in a stepped format.

In this example I will iterate a 2D with step size of 1 and 3 by passing it as an argument in a stepped form to nditer

  1. import numpy as np
  2. my_arr = np.array([[9,8,7,6,5,4,3,2,1], [90,80,70,60,50,40,30,20,10]])
  3. print('2D Array which we will be iterating is:\n ',my_arr)
  4. print('The contents of 2D array after applying steps:')
  5. for z in np.nditer(my_arr[:1, ::3]):
  6. print(z, end='')

In this example we will iterate a 3D array in different step size. Here you can clearly see that while passing the array to nditer as argument we have stepped it as per our requirement.

  1. import numpy as np
  2. my_arr = np.array([[['s','o','u','r','c','e'],['c','o','d','e','s','t'],['t','u','t','o','r','i']]])
  3. print('3D Array which we will be iterating is:\n ',my_arr)
  4. print('The contents of 3D array after applying steps:')
  5. for x in np.nditer(my_arr[::1, ::2,::2]):
  6. print(x, end='')

Add new comment