10

I have not understood the difference between axis in a multidimensional array in NumPy. Can you explain to me? In particular, I would like to know where are axis0, axis1 and axis2 in a NumPy tridimensional array. And why?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Alex
  • 119
  • 1
  • 1
  • 5
  • [I'm just posting this here to link these two questions](http://stackoverflow.com/questions/22981845/3-dimensional-array-in-numpy). They're different, but about the same issue, and reading both would help future readers. – iled Feb 22 '17 at 01:30

2 Answers2

18

The easiest way is with an example:

In [8]: x = np.array([[1, 2, 3], [4,5,6],[7,8,9]], np.int32)

In [9]: x
Out[9]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]], dtype=int32)

In [10]: x.sum(axis=0)  # sum the columns [1,4,7] = 12, [2,5,8] = 15 [3,6,9] = 18  
Out[10]: array([12, 15, 18])

In [11]: x.sum(axis=1)    # sum the rows [1,2,3] = 6, [4,5,6] = 15 [7,8,9] = 24
Out[11]: array([ 6, 15, 24])

axis 0 are the columns and axis 1 are the rows.

In a three dimensional array:

In [26]: x = np.array((((1,2), (3,4) ), ((5,6),(7,8))))
In [27]: x
Out[27]: 
   array([[[1, 2],
           [3, 4]],
          [[5, 6],
           [7, 8]]])
In [28]: x.shape # dimensions of the array
Out[28]: (2, 2, 2)

In [29]: x.sum(axis=0)
Out[29]: 
array([[ 6,  8],   #  [1,5] = 6 [2,6] = 8 [3,7] = 10 [4, 8] = 12
      [10, 12]])
In [31]: x.sum(axis=1)
Out[31]: 
    array([[ 4,  6],   # [1,3] = 4 [2,4] = 6 [5, 7] = 12 [6, 8] = 14
           [12, 14]])
In [33]: x.sum(axis=2) # [1, 2] = 3 [3, 4] = 7 [5, 6] = 11 [7, 8] = 15
Out[33]: 
array([[ 3,  7],
       [11, 15]])

In [77]: x.ndim # number of dimensions of the array
Out[77]: 3

Link for a good tutorial on using multidimensional data arrays

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • 2
    So I can say that the axis0 is the first element of the tuple of the command "shape", the axis1 the second element and the axis2 the third element. Is it right? – Alex Jun 18 '14 at 10:36
  • yes exactly. It is a 2*2*2 array. You can access particular elements using `x[0][0][1]` etc.. – Padraic Cunningham Jun 18 '14 at 10:43
  • "_axis 0_ are the _columns_ and _axis 1_ are the _rows._" Or put it another way from which I managed to understand how numpy axes work: "_axis 0_ is the outermost axis indexing the largest subarrays while _axis n-1_ is the innermost axis indexing individual elements. – Bálint Sass Feb 12 '21 at 10:51
  • Or put it yet another way: `x.sum(axis=0)` is `x[sum][*][*]`, `x.sum(axis=1)` is `x[*][sum][*]`, `x.sum(axis=2)` is `x[*][*][sum]`. – Bálint Sass Feb 12 '21 at 10:54
3

The axes can be named by traversing through the n-dimensional array, right from the outside of the array to the inside till we reach the actual scalar elements. The outermost dimension will always be axis 0 and the the innermost dimension(scalar elements) will be axis n-1. The below link will be more useful in imagining and realising NumPy axes - How does NumPy's transpose() method permute the axes of an array?

Cheat Code #1: When you use the NumPy sum function with the axis parameter, the axis that you specify is the axis that gets collapsed.

Cheat Code #2: When we use the axis parameter with the np.concatenate() function, the axis parameter defines the axis along which we stack the arrays.

YDF
  • 365
  • 5
  • 9