Machine Learning Prerequisites (Numpy)

29 / 32

Numpy - Mathematical and Statistical functions on NumPy Arrays - Sum(across axes)

We can sum across different axes of a NumPy array by specifying the axis parameter of the sum function.

Y = np.arange(24).reshape(2,3,4)

print(Y)

Output will be

array( [ [ [ 0, 1, 2, 3],
           [ 4, 5, 6, 7],
           [ 8, 9, 10, 11] ],

         [ [ 12, 13, 14 15],
           [ 16, 17, 18, 19],
           [ 20, 21, 22, 23] ] ] )

axis=0

axis = 0 means first dimension.

The first dimension in array Y (i.e. the outermost bracket '[') has two matrices i.e.

[ [ 0, 1, 2, 3],
  [ 4, 5, 6, 7],
  [ 8, 9, 10, 11] ]

and

[ [ 12, 13, 14 15],
  [ 16, 17, 18, 19],
  [ 20, 21, 22, 23] ]

Hence, sum across the first dimension (axis=0) means the sum of these two matrices (element-wise) which will give us a single matrix in the final matrix, as shown below.

Y.sum(axis=0)

The output will be

array( [ [ 12, 14, 16, 18 ],
         [ 20, 22, 24, 26 ],
         [ 28, 30, 32, 34 ] ] )

This is calculated as

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

axis=1

axis = 1 means the second dimension.

The second dimension in array Y (i.e. the second bracket '[' from the outer side) is the rows. The rows are

row 0:

    [ 0, 1, 2, 3]
    [ 4, 5, 6, 7]
    [ 8, 9, 10, 11]

row 1:

    [ 12, 13, 14, 15]
    [ 16, 17, 18, 19]
    [ 20, 21, 22, 23]

Hence, sum across the second dimension (axis=1) means the sum of the (along the) rows of the two matrices (element-wise) which will give us a single matrix in the final matrix, as shown below.

Y.sum(axis=1)

The output will be

array( [ [12, 15, 18, 21],
         [48, 51, 54, 57] ] )

This is calculated as

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

axis=2

axis = 2 means the third dimension.

The third dimension in array Y (i.e. the third bracket '[' from the outer side) is the columns.

print(Y)


array( [ [ [ 0, 1, 2, 3],
           [ 4, 5, 6, 7],
           [ 8, 9, 10, 11] ],

         [ [ 12, 13, 14 15],
           [ 16, 17, 18, 19],
           [ 20, 21, 22, 23] ] ] )

The columns are

[ 0, 
  4,
  8 ]

[ 1, 
  5,
  9 ]

[ 2, 
  6,
  10 ]

[ 3, 
  7,
  11 ]

[ 12, 
  16,
  20 ]

[ 13, 
  17,
  21 ]

[ 14, 
  18,
  22 ]

[ 15, 
  19,
  23 ]

Hence, sum across third dimension (axis=2) means the sum of the (along with the) columns of the two matrices (element-wise) which will give us a single matrix in the final matrix, as shown below.

Y.sum(axis=2)

The output will be

array( [ [ 6, 22, 38],
         [54, 70, 86] ] )

This is calculated as

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

Please follow the following steps:

(1) Please import the required libraries

import numpy as np

(2) Please create a NumPy array called Z, with a total of 18 elements, and shape as (2, 3, 3)

<<your code comes here>> = np.arange(<<your code comes here>>).reshape(<<your code comes here>>)

(3) Print the above array Z using the print() function to see its values.

print(Z)

(4) Please find the sum across axis=2 for the array Z, and print the result using the print() function

  print( Z.sum(<<your code comes here>>))
See Answer

No hints are availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...