axis = 3 for a 3D array in numpy?

Viewed 1159

I am confused as to why I dont get an error for axis = 3 in the following

import numpy as np

a = np.arange(27)
b = np.arange(27)

a = a.reshape((3,3,3))
b = b.reshape((3,3,3))

c = np.mean([a,b],axis=3)

I was using the above code as a way to understand how axis work in numpy.

From this article which shows the 2D case:enter image description here I figured out that for multi dimensional axis, axis 0 is always along the rows (y dimension), axis 1(x dimension) is along the cols. So in my case where I have a 3d array, I wouldve thought I would have 1 more axis, axis = 2 along the 'z direction'. Yet numpy allows me to input axis = 3 as well? What direction is this indexing across? How is numpy using this axis to calculate a mean?

1 Answers

You create a 4D array in the call

c = np.mean([a,b],axis=3) # [a,b] adds 4th dimension
np.array([a,b]).shape

Output

 (2, 3, 3, 3)
Related