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. So we’ll be learning how is the axis indexed

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 to 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 the shape `(5,6,8)`

. Furthermore, `c[x,y,z]`

is equal to the sum of all elements `b[x,y,:,z]`

.

If at all anyone needs this visual description of a `shape=(3,5)`

array:

You can grasp the 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)

## Method 2

I created an array of a shape with different values`(4,2,3)`

so that you can tell the structure clearly. A different axis means a 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`

axis indexed 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`

.

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, the 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)
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, 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]

```
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]]))

##### Important Notice for college students

If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

For more Programming related blogs Visit Us Geekycodes. Follow us on Instagram.