carmen23
Adaptive Systems Engineer
LEVEL 1
400 XP
In this tutorial you will learn:
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.
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.
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.
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
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.
Book traversal links for NumPy Iterator
Download
- 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.
- import
numpy as
np
- my_arr =
np.array
(
[
[
's'
,
'o'
,
'u'
,
'r'
,
'c'
,
'e'
]
,
[
'c'
,
'o'
,
'd'
,
'e'
,
's'
,
't'
]
]
)
- print
(
'2D Array which we will be iterating is:\n
'
,
my_arr)
- print
(
'The contents of 2D array are:'
)
- for
z in
np.nditer
(
my_arr)
:
- 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.
- import
numpy as
np
- my_arr =
np.array
(
[
[
[
's'
,
'o'
,
'u'
,
'r'
,
'c'
,
'e'
]
,
[
'c'
,
'o'
,
'd'
,
'e'
,
's'
,
't'
]
,
[
't'
,
'u'
,
't'
,
'o'
,
'r'
,
'i'
]
]
]
)
- print
(
'3D Array which we will be iterating is:\n
'
,
my_arr)
- print
(
'The contents of 3D array are:'
)
- for
z in
np.nditer
(
my_arr)
:
- 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.
- import
numpy as
np
- my_arr =
np.array
(
[
1
,
2
,
3
]
,
dtype=
'f'
)
- print
(
'Array which we will be iterating is:\n
'
,
my_arr)
- print
(
'DataType of Array which we will be iterating is:\n
'
,
my_arr.dtype
)
- print
(
'The iterated contents of array are:'
)
- for
x in
np.nditer
(
my_arr,
op_flags=
[
'readonly'
,
'copy'
]
,
op_dtypes=
[
'complex64'
]
)
:
- print
(
x)
- 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
- import
numpy as
np
- my_arr =
np.array
(
[
[
9
,
8
,
7
,
6
,
5
,
4
,
3
,
2
,
1
]
,
[
90
,
80
,
70
,
60
,
50
,
40
,
30
,
20
,
10
]
]
)
- print
(
'2D Array which we will be iterating is:\n
'
,
my_arr)
- print
(
'The contents of 2D array after applying steps:'
)
- for
z in
np.nditer
(
my_arr[
:1
,
::3
]
)
:
- 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.
- import
numpy as
np
- my_arr =
np.array
(
[
[
[
's'
,
'o'
,
'u'
,
'r'
,
'c'
,
'e'
]
,
[
'c'
,
'o'
,
'd'
,
'e'
,
's'
,
't'
]
,
[
't'
,
'u'
,
't'
,
'o'
,
'r'
,
'i'
]
]
]
)
- print
(
'3D Array which we will be iterating is:\n
'
,
my_arr)
- print
(
'The contents of 3D array after applying steps:'
)
- for
x in
np.nditer
(
my_arr[
::1
,
::2
,
::2
]
)
:
- print
(
x,
end=
''
)
Book traversal links for NumPy Iterator
- ‹ NumPy Array Creation
- Up
- NumPy Array Concatenation ›
Download
You must upgrade your account or reply in the thread to view hidden text.