Updated April 3, 2023
Introduction to NumPy for loop
Numpy for loop is used for iterating through numpy arrays of different dimensions, which is created using the python numpy library and using the for loop, multiple operations can be done going through each element in the array by one. Numpy library also contains an object for iteration, which can be used to iterate over single and multi-dimensional arrays and numpy for loop is very efficient in terms of ease of coding and performance.
Syntax
The basic syntax of the numpy for loop operation is a for with a colon and followed by the python indentation, and we can perform the operation inside this block which allows us to iterate through each element in the given array, and we can print the output inside the loop.
Examples of NumPy for loop
Given below are the examples of Numpy for loop:
Example #1
Let us discuss a basic example for understanding how the numpy for loop works.
Code:
import numpy as np
arr1 = np.array([2, 1, 4])
for x in arr1:
print(x)
Output:
Here in the above example, we can create an array using the numpy library and performed a for loop iteration and printed the values to understand the basic structure of a for a loop.
import numpy as np
arr1 = np.array([[2, 1, 4],[2, 4, 6]])
arr2 = np.array([[8, 16, 44],[22, 40, 16]])
arr3 = np.array([[7, 14, 21],[0, 4, 7]])
for x in arr1,arr2, arr3:
print(x)
Output:
Similar to the previous example, we have created 2-D arrays and iterated and printed each element of the array. So, using this technique, we can perform multiple operations inside the loop, and we can print the results.
Example #2
In this example will discuss how to iterate through a two-dimensional array.
Code:
import numpy as np
arr1 = np.array([[8, 16, 44],[22, 40, 16]])
for x in arr1:
for y in x:
print(y)
Output:
In this example, we have created an array and iterated it twice using python for a loop. This method allows us to perform different operations while iterating multiple times, and this method is very efficient and requires less coding.
Example #3
In this example, we have created a zero-dimensional array and converted it into a two-dimensional array.
Code:
import numpy as np
arr = np.array([[[8, 16, 44], [0, 4, 7]], [[22, 40, 16], [7, 14, 21]]])
for x in arr:
for y in x:
for z in y:
print(z)
Output:
In this example, we have used three for loops iterating one by one to make the array into a scalar. Then, we use this method to extract the scalar of the actual scalar values in the array by iterating to the respective dimensions.
Example #4
In this example, we’ll see a different method that uses a numpy function nditer() which is widely used to loop through an array of different dimensions. From simple to advanced and complex iterations is done using the nditer() function. In general, when we iterate through individual scalar values in an array, we need to use n for loops depending on the array dimension, which might become tedious, so using the nditer() function is very easy to handle such cases.
Code:
import numpy as np
arr = np.array([[[4, 3], [1, 4]], [[7, 6], [3, 2]]])
for x in np.nditer(arr):
print(x)
Output:
Like the previous example, we have created a three-dimensional 3-D array, and unlike python, for loop, we have iterated only once through each of the scalar values of the array. In python for loop, we would have used three for loops to iterate through a 3-D array, but when we used the nditer() function, we have looped only once, and the nditer function allowed us to iterate through this three-dimensional array.
Example #5
In this example, we’ll use the nditer() function and iterate through individual values by changing their data type while iterating this method is very useful in changing the data types of the array values.
Code:
import numpy as np
arr = np.array([[[4, 3], [1, 4]], [[7, 6], [3, 2]]])
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):
print(x)
Output:
In this example, we have used flags and op_dtypes parameter to convert the scalar values in the array to string values. By default, numpy cannot change the data types of the array; we use the parameter called ‘flags = buffered’; this allows the nditer function to perform the datatype conversion through the buffer space we provided the op_dtypes argument, we have passed ‘s’. Similarly, to convert into an integer, we can use ‘i’ in the op_dtypes parameter.
import numpy as np
arr = np.array([[[4, 3], [1, 4]], [[7, 6], [3, 2]]])
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['i']):
print(x)
Output:
Example #6
In this example, we have iterated over the numpy array using the nditer() function by using the step size parameter to skip the values in between.
Code:
import numpy as np
arr = np.array([[8, 16, 44, 14, 77],[22, 30, 41, 44, 16]])
for x in np.nditer(arr[:, ::3]):
print(x)
Output:
We have created a 2-D array and used the nditer function to iterate through individual values in the array; while iterating, we have used the step size parameter and declared the value as 3 so that we will skip two values in between and print the remaining iterated values as output. This method is very useful when we wanted to skip certain elements in the array from iteration.
Example #7
We will discuss one more method where we can control the iteration order using the nditer function in a numpy array.
Code:
import numpy as np
arr = np.arange(10).reshape (2,5)
print(arr)
for i in np.nditer(arr, order = 'C'):
print(i)
Output:
Inside the nditer () function, we have declared the order = ‘c’ parameter to
change the order of the iteration to C. This method allows us to iterate over the elements in the array to our desired order. Also, we can provide order = ‘F’ to iterate in Fortran order, displaying the Fortran order elements.
import numpy as np
arr = np.arange(10).reshape (2,5)
print(arr)
for i in np.nditer(arr, order = 'F'):
print(i)
Output:
Conclusion
In this article, we have discussed numpy for loop in detail, using various examples to understand the for loop operation in the numpy array and its uses. We have also discussed how to use the nditer() function, which is a function inside the numpy package that can be used to iterate the multi-dimensional arrays with ease and techniques involved in changing the data types and order while iterating through the array with examples. I hope this article helps. Thank you.
Recommended Articles
This is a guide to NumPy for loop. Here we discuss the numpy for loop in detail using various examples to understand the for loop operation. You may also have a look at the following articles to learn more –