Alcott
Alcott

Reputation: 18585

how is axis indexed in numpy's array?

From Numpy's tutorial, axis can be indexed with integers, like 0 is for column, 1 is for row, but I don't grasp why they are indexed this way? And How do I figure out each axis' index when coping with multidimensional array?

Upvotes: 125

Views: 144459

Answers (5)

Bookervsky
Bookervsky

Reputation: 11

Let's assume a NumPy multi-dimensional array X:

X = array( [[[1,2], [3,4]], [[1,2], [3,4]]] )

As you can see, X has 3 outermost square brackets (starting [[[ and ending ]]]) and therefore X has 3 dimensions.

Easier and equivalent ways to visualize X are:

[[[1,2], [3,4]],
 [[1,2], [3,4]]]

or

[[[1,2],
  [3,4]],

 [[1,2],
  [3,4]]]

or

[
 [
  [1,2],  
  [3,4]
 ],  
 [
  [1,2],  
  [3,4]
 ]
]

axis=0 refers to the outermost [ , axis=1 refers to the middle [ , axis=2 refers to the innermost [.
For example, when you do x.sum(axis=0),you are actually doing two steps:

1.Add up elements inside the brackets that correspond to axis=0, the brackets that correspond to axis=0 are the outermost brackets, so it's [[[1,2], [3,4]]]+[[[1,2], [3,4]]], add them up and you get[[[2,4], [6,8]]].

I.e.,

[
 [
  [1,2],  
  [3,4]
 ]  
 +  
 [
  [1,2],  
  [3,4]
 ]
]

==

[
 [
  [1+1, 2+2],  
  [3+3, 4+4]
 ]
]

==

[
 [
  [2,4],  
  [6,8]
 ]
]

2.Wipe out the brackets that correspond to axis=0, which are the outermost [ and ], finally giving you [[2,4], [6,8]].
I.e.,

[
 [2,4],  
 [6,8]
]

It's the same idea when you do x.sum(axis=2), you

1.Add up elements inside the bracket that corresponds to axis=2, it's [[[1+2], [3+4], [1+2], [3+4]]]

2.Wipe out the brackets that correspond to axis=2,which is the innermost [, finally you get [[3,7], [3,7]].

Upvotes: 1

Debashis Sahoo
Debashis Sahoo

Reputation: 5912

If at all anyone need this visual description of a shape=(3,5) array:

Numpy array axis 0 and 1

Upvotes: 114

Lyn
Lyn

Reputation: 561

You can grasp axis in this way:

>>> a = np.array([[[1,2,3],[2,2,3]],[[2,4,5],[1,3,6]],[[1,2,4],[2,3,4]],[[1,2,4],[1,2,6]]])
array([[[1, 2, 3],
    [2, 2, 3]],

   [[2, 4, 5],
    [1, 3, 6]],

   [[1, 2, 4],
    [2, 3, 4]],

   [[1, 2, 4],
    [1, 2, 6]]])
>>> a.shape
(4,2,3)

I created an array of a shape with different values(4,2,3) so that you can tell the structure clearly. Different axis means different 'layer'.

That is, axis = 0 index the first dimension of shape (4,2,3). It refers to the arrays in the first []. There are 4 elements in it, so its shape is 4:

  array[[1, 2, 3],
        [2, 2, 3]],

  array[[2, 4, 5],
        [1, 3, 6]],

  array[[1, 2, 4],
        [2, 3, 4]],

  array[[1, 2, 4],
        [1, 2, 6]]

axis = 1 index the second dimension in shape(4,2,3). There are 2 elements in each array of the layer: axis = 0,e.c. In the array of

 array[[1, 2, 3],
       [2, 2, 3]]

. The two elements are:

array[1, 2, 3]

array[2, 2, 3]

And the third shape value means there are 3 elements in each array element of layer: axis = 2. e.c. There are 3 elements in array[1, 2, 3]. That is explicit.

And also, you can tell the axis/dimensions from the number of [] at the beginning or in the end. In this case, the number is 3([[[), so you can choose axis from axis = 0, axis = 1 and axis = 2.

Upvotes: 28

nneonneo
nneonneo

Reputation: 179422

By definition, the axis number of the dimension is the index of that dimension within the array's shape. It is also the position used to access that dimension during indexing.

For example, if a 2D array a has shape (5,6), then you can access a[0,0] up to a[4,5]. Axis 0 is thus the first dimension (the "rows"), and axis 1 is the second dimension (the "columns"). In higher dimensions, where "row" and "column" stop really making sense, try to think of the axes in terms of the shapes and indices involved.

If you do .sum(axis=n), for example, then dimension n is collapsed and deleted, with each value in the new matrix equal to the sum of the corresponding collapsed values. For example, if b has shape (5,6,7,8), and you do c = b.sum(axis=2), then axis 2 (dimension with size 7) is collapsed, and the result has shape (5,6,8). Furthermore, c[x,y,z] is equal to the sum of all elements b[x,y,:,z].

Upvotes: 196

ndas
ndas

Reputation: 217

In general, axis = 0, means all cells with first dimension varying with each value of 2nd dimension and 3rd dimension and so on

For example , 2-dimensional array has two corresponding axes: the first running vertically downwards across rows (axis 0), and the second running horizontally across columns (axis 1)

For 3D, it becomes complex, so, use multiple for loops

>>> x = np.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],
    [24, 25, 26]]])

>>> x.shape #(3, 3, 3)

#axis = 0 
>>> for j in range(0, x.shape[1]):
      for k in range(0, x.shape[2]):
        print( "element = ", (j,k), " ", [ x[i,j,k] for i in range(0, x.shape[0]) ])
...
element =  (0, 0)   [0, 9, 18]             #sum is 27
element =  (0, 1)   [1, 10, 19]            #sum is 30
element =  (0, 2)   [2, 11, 20]
element =  (1, 0)   [3, 12, 21]
element =  (1, 1)   [4, 13, 22]
element =  (1, 2)   [5, 14, 23]
element =  (2, 0)   [6, 15, 24]
element =  (2, 1)   [7, 16, 25]
element =  (2, 2)   [8, 17, 26]

>>> x.sum(axis=0)            
array([[27, 30, 33],
       [36, 39, 42],
       [45, 48, 51]])

#axis = 1    
for i in range(0, x.shape[0]):
    for k in range(0, x.shape[2]):
        print( "element = ", (i,k), " ", [ x[i,j,k] for j in range(0, x.shape[1]) ])

element =  (0, 0)   [0, 3, 6]      #sum is 9 
element =  (0, 1)   [1, 4, 7]
element =  (0, 2)   [2, 5, 8]
element =  (1, 0)   [9, 12, 15]
element =  (1, 1)   [10, 13, 16]
element =  (1, 2)   [11, 14, 17]
element =  (2, 0)   [18, 21, 24]
element =  (2, 1)   [19, 22, 25]
element =  (2, 2)   [20, 23, 26]

# for sum, axis is the first keyword, so we may omit it,

>>> x.sum(0), x.sum(1), x.sum(2)
(array([[27, 30, 33],
        [36, 39, 42],
        [45, 48, 51]]),
 array([[ 9, 12, 15],
        [36, 39, 42],
        [63, 66, 69]]),
 array([[ 3, 12, 21],
        [30, 39, 48],
        [57, 66, 75]]))

Upvotes: 12

Related Questions