In this article we are going to see how to iterate arrays in NumPy. Let’s get a head start of what array iteration is all about.
Iterating simply means checking element one at a time in a program. They are many multi-dimensional arrays in NumPy.
Iterating a One-dimensional Array:
We are going to go through one element at a time. This is mostly done using the ‘for’ loop
import numpy as np
A = np.arange(12)for cell in A:print (cell, end=' ')
OUTPUT:
In addition to the for loop, we are going to used another function called re.shape. Here we able to iterate array each in a row.
Here is an example where the two functions are involved
import numpy as np
A = np.arange(12).reshape(4,3)for row in A:print(row)
OUTPUT:
In a case where you don’t to what to iterate two rows, there is a function called flatten that reduce a two – dimensional array into a one-dimensional array. Note that this flatten functions reduce a multi-dimensional array into the specific array that you want.
Iterating Using the Nditer Function
It is a basic tool in NumPy module that we can used to solve basic problems. With this function we are able to create order during iteration by using parameters. If you specify a particular order, the nditer functions called it, following the position of the element called.
Iterating scalar each scalar element
With the simple scalar elements, we can use the n for loops.
Examples
import numpy as np
arr = np.array([['a' 'b'], ['c' 'd'], ['e' 'f']])for x in np.nditer(arr):print(x)import numpy as np
OUTPUT:
However, this is not possible with scalars with high dimensions
Just as we can iterate scalars using nditer function, this function can also be used to get order during iteration which is called the nditer iteration order.
Example
To get the ‘C’ order (horizontal order)
import numpy as np
a = np.arange(12)print('Modified array is:')for x in np.nditer(a, flags = ['external_loop'], order = 'C'):print(x)
With this order, the array created is arranged horizontally following the order specified.
OUTPUT:
import numpy as np
a = np.arange(12)print('Modified array in F-style order:')for x in np.nditer(a, order = 'F'):print(x)
With this order, the array created is arranged vertically following the order specified
OUTPUT:
In modifying arrays while iterating, we can use the op_flags function. So, to update the elements in an array we can do the following steps:
import numpy as np
a = np.arange(12)for x in np.nditer(a, op_flags = ['readwrite']):x[...] = 5*xprint('Modified array is:')print(a)
The op-flag tell the compiler to add 5 to the previous number. This is done in a continuous sequence
OUTPUT:
We can use a nditer function to iterate arrays simultaneously. Here is an example to get you understand better.
import numpy as np
a = np.arange(12)a = a.reshape(3,4)b = np.array([5, 6, 7, 8], dtype = int)print('Modified array is:')for x,y in np.nditer([a,b]):print("%d:%d" % (x,y))
Example
NumPy tells compile the run the program at the same time while following the instruction of the nditer function
OUTPUT:
Comments
Post a Comment
Please do not enter any spam link in the comment box.