Iterating over an numpy array - SOLVED#

To iterate over the rows of a 2d numpy array we use:

import numpy as np #Run this cell or none of the np commands will work
two_dim_array = np.array([[1.4, 2.3, 5.3],[1.5, 3.2, 6.7]])

for row in two_dim_array:
    print("######################")
    print(f"the row is {row}")
######################
the row is [1.4 2.3 5.3]
######################
the row is [1.5 3.2 6.7]

Exercise 4.1#

Please evaluate the cell below.

two_dim_array_rand =  np.random.uniform(0, 100, size = (5, 10))

In the cell below iterate over each row of the above 2d numpy array, and print the (elementwise) square root of each row.

for row in two_dim_array_rand:
  sdev = np.std(row)
  print("######################")
  print(f"the square root of the row {row} is {sdev}.")
######################
the square root of the row [59.43974636 43.27888641 72.80393323 91.64361126  3.79643226 72.1172325
 29.38347896 10.40803664  9.69029007 58.6107282 ] is 29.095707112136438.
######################
the square root of the row [95.9975604   1.99374252 26.98758401  4.40335451 42.6517666  12.25327059
 48.18207307 96.85199154  3.06965735 69.10825563] is 35.076445966788214.
######################
the square root of the row [15.00108996 50.48763544  9.16283046 13.74540876 53.00244481 85.20615971
 88.74881041 28.87022423 64.93063342 41.38341082] is 27.306014488730096.
######################
the square root of the row [90.54013729 48.64321168 98.29993081 61.65538071 28.62335937 96.71146391
 73.90143705 47.54496512 64.73741095 76.57469113] is 21.790195256962562.
######################
the square root of the row [36.75599794 58.25899754 22.13327427 10.75643635 31.03140119 91.86428563
 96.06028683  0.75761855  3.88076564 78.26093589] is 34.13401989263495.

To iterate all elements of the array we can use the flat function:

for element in two_dim_array.flat:
    print("######################")
    print(f"the element is {element}")
######################
the element is 1.4
######################
the element is 2.3
######################
the element is 5.3
######################
the element is 1.5
######################
the element is 3.2
######################
the element is 6.7

but perhaps it is easier to understand this if we loop over the rows and then loop over the columns within this loop (so using nested looping as we introduced in the third week!)

for row in two_dim_array:
    for element in row:
        print("######################")
        print(f"the element is {element}")
######################
the element is 1.4
######################
the element is 2.3
######################
the element is 5.3
######################
the element is 1.5
######################
the element is 3.2
######################
the element is 6.7

Exercise 4.2#

Please evaluate the cell below:

three_dim_array =  np.random.uniform(0, 10, size = (5, 4, 2))
print(three_dim_array)
[[[4.14073601 9.86331436]
  [5.57175783 2.49409211]
  [7.67068508 6.74670147]
  [2.66190314 8.76324642]]

 [[2.67590684 2.65073201]
  [3.6633357  1.07411289]
  [2.40717276 4.50039888]
  [1.63410822 5.56705604]]

 [[3.74433648 4.67176377]
  [0.9544318  8.78048188]
  [9.42682574 6.67442061]
  [7.50137543 1.72531627]]

 [[1.44072389 8.82424522]
  [7.69166939 6.10515359]
  [8.91531144 4.6290004 ]
  [9.36788043 2.10271803]]

 [[4.30417273 7.92706425]
  [9.35598283 6.80329085]
  [1.43430079 6.74146287]
  [8.86955133 9.79604888]]]

In the cell below iterate over each element of the array, and print the square of each element. (Note, that in this three dimensional array you would need three for loops if using nested loops. Using flat would certainly be easier here, but perhaps not so clear)

for row in three_dim_array:
  for column in row:
    for val in column:
      print(val)
4.1407360107722795
9.863314356387876
5.571757826821165
2.4940921141104297
7.670685077824018
6.746701467608878
2.6619031387946057
8.763246423362892
2.6759068371386086
2.6507320110107213
3.6633356995909363
1.0741128927785215
2.4071727603755755
4.500398877263345
1.6341082172995347
5.567056041454748
3.744336475601253
4.671763771600226
0.9544317964319249
8.780481883243766
9.426825743125454
6.674420613880708
7.5013754336093275
1.7253162650055576
1.4407238860506633
8.824245217168587
7.691669388241595
6.105153593767597
8.915311443265068
4.629000404568644
9.36788043302959
2.1027180318605554
4.304172725761184
7.927064251904429
9.35598283326568
6.803290848665082
1.434300788866254
6.741462871396823
8.869551332921032
9.796048876788694