replacing iteration in numpy

  • Last Update :
  • Techknowledgy :

There is a numpy function for this, numpy.full or numpy.full_like:

>>> x = np.array(([1, 2, 2], [1, 4, 3])) >>>
   np.full(x.shape, 10)

array([
   [10, 10, 10],
   [10, 10, 10]
])
# OR, >>>
np.full_like(x, 10)

array([
   [10, 10, 10],
   [10, 10, 10]
])

If you want to iterate you can either use itertools.product:

>>> from itertools
import product

   >>>
   def Iter_Replace(x):
   indices = product( * map(range, x.shape))
for index in indices:
   x[tuple(index)] = 10
return x >>>
   x = np.array([
      [1, 2, 2],
      [1, 4, 3]
   ]) >>>
   Iter_Replace(x)

array([
   [10, 10, 10],
   [10, 10, 10]
])

Or, use np.nditer

>>> x = np.array([
      [1, 2, 2],
      [1, 4, 3]
   ])

   >>>
   for index in np.ndindex(x.shape):
   x[index] = 10

   >>>
   x

array([
   [10, 10, 10],
   [10, 10, 10]
])

You have two errors. There are missing parenthesis in the first line of main:

x = np.array(([1, 2, 2], [1, 4, 3]))

Suggestion : 2

During iteration, you may want to use the index of the current element in a computation. For example, you may want to visit the elements of an array in memory order, but use a C-order, Fortran-order, or multidimensional index to look up values in a different array.,The most basic task that can be done with the nditer is to visit every element of an array. Each element is provided one by one using the standard Python iterator interface.,For a simple example, consider taking the sum of all elements in an array.,used the nditer as a context manager using the with statement, and the temporary data will be written back when the context is exited.

>>> a = np.arange(6).reshape(2, 3) >>>
   for x in np.nditer(a):
   ...print(x, end = ' ')
   ...
   0 1 2 3 4 5
>>> a = np.arange(6).reshape(2, 3) >>>
   for x in np.nditer(a.T):
   ...print(x, end = ' ')
   ...
   0 1 2 3 4 5
>>>
for x in np.nditer(a.T.copy(order = 'C')):
   ...print(x, end = ' ')
   ...
   0 3 1 4 2 5
>>> a = np.arange(6).reshape(2, 3) >>>
   for x in np.nditer(a, order = 'F'):
   ...print(x, end = ' ')
   ...
   0 3 1 4 2 5 >>>
   for x in np.nditer(a.T, order = 'C'):
   ...print(x, end = ' ')
   ...
   0 3 1 4 2 5
>>> a = np.arange(6).reshape(2, 3) >>>
   a
array([
      [0, 1, 2],
      [3, 4, 5]
   ]) >>>
   with np.nditer(a, op_flags = ['readwrite']) as it:
   ...
   for x in it:
   ...x[...] = 2 * x
   ...
   >>>
   a
array([
   [0, 2, 4],
   [6, 8, 10]
])
>>> a = np.arange(6).reshape(2, 3) >>>
   for x in np.nditer(a, flags = ['external_loop']):
   ...print(x, end = ' ')
   ...[0 1 2 3 4 5]

Suggestion : 3

Last Updated : 13 May, 2022

Output:

[
   [0, 1, 2, 3, 4],
   [5, 6, 7, 8, 9],
   [10, 11, 12, 13, 14],
   [15, 16, 17, 18, 19],
   [20, 21, 22, 23, 24]
])

[0 5 10 15 20]
[1 6 11 16 21]
[2 7 12 17 22]
[3 8 13 18 23]
[4 9 14 19 24]

Suggestion : 4

for and while loop is used to iterate one-dimensional and two-dimensional array.,Numpy package provides an iterator object called numpy.nditer. nditer is a multi-dimensional iterator that enables you to iterate each element of Numpy array.,Note: The default order of iteration followed by nditer() is as per the memory layout of an array. This thing is noticable when you transpose an array and then iterate it.,In this tutorial, you will learn two different ways of iterating Numpy array-

Iterating a One-dimensional Array

#Python program to iterate 1 - D Numpy array using
for loop
import numpy as np

x = np.array([21, 15, 99, 42, 78])
for cell in x:
   print(cell, end = ' ')

Output of the above program

21 15 99 42 78
21 15 99 42 78
#Python program to iterate 1 - D Numpy array using
while loop
import numpy as np

x = np.array([21, 15, 99, 42, 78])
i = 0
while i < x.size:
   print(x[i], end = ' ')
i = i + 1

In case, you want to iterate each cell then go through the below examples-

#Python program to iterate each cell of 2 - D array using
for loop
import numpy as np

x = np.array([
   [21, 15, 99, 42, 78],
   [11, 54, 34, 76, 89]
])
for row in x:
   for cell in row:
   print(cell, end = ' ')
print()
21 15 99 42 78
11 54 34 76 89
#Python program to iterate each cell of 2 - D array using
while loop
import numpy as np

x = np.array([
   [21, 15, 99, 42, 78],
   [11, 54, 34, 76, 89]
])
i = 0
j = 0
rows, cols = x.shape
while i < rows:
   j = 0
while j < cols:
   print(x[i][j], end = ' ')
j = j + 1
print()
i = i + 1