why is there a 0 put into this?

mean3 = stacked_threes.mean(0)

Hi,

Its the axis of the mean calculation, letâ€™s take for example 2-D matrix

Axis 0 will calculate the mean on the columns

Axis 1 will calculate the mean on the rows

@Kerner after messing with the .mean() code some more I had one follow-up question. Your example assumes a 2D matrixâ€¦ what if it is a 3D matrix like we did for the Mlist example in lecture 4.

stacked_threes = torch.stack(three_tensors).float()/255

stacked_threes.shape

Result

torch.Size([6131, 28, 28])

where 6131 represents thet number of Mlist images as arrays and 28*28 are the pixels of the images.

In this example how would 0 in mean(0) start to calculate the mean of the array?

Hi,

I think this image explains it the best:

You can imagine axis 0 is the height of stacking 6,131 of 28*28 2D matrix.

mean(0) will â€ścollapseâ€ť this 3D matrix along 0 axis resulting in a single 2D 28*28 matrix which each pixel is the mean of all the 6,131 in its location (i.e. location [0][0] is mean of 6,131 location [0][0])

The image was taken from this blog:

https://www.awesomegrasp.com/what-is-python-numpy-array-dimension-or-axis/

There is also the code for this matrix, my recommendation is to follow Jeremyâ€™s advice and just play with this example (try mean 0, mean 1, mean -1 etcâ€¦) and check the results matching your manual calculations.

You can even try with 4D but you cannot draw that so nicely and probably will get a headache

1 Like