# understanding axis in python

• Last Update :
• Techknowledgy :

In numpy `array`s, dimensionality refers to the number of `axes` needed to index it, not the dimensionality of any geometrical space. For example, you can describe the locations of points in 3D space with a 2D array:

```array([
[0, 0, 0],
[1, 2, 3],
[2, 2, 2],
[9, 9, 9]
])```

You can play around with this, and see the number of dimensions and shape of an array like so:

```In[262]: a = np.arange(9)

In[263]: a
Out[263]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])

In[264]: a.ndim # number of dimensions
Out[264]: 1

In[265]: a.shape
Out[265]: (9, )

In[266]: b = np.array([
[0, 0, 0],
[1, 2, 3],
[2, 2, 2],
[9, 9, 9]
])

In[267]: b
Out[267]:
array([
[0, 0, 0],
[1, 2, 3],
[2, 2, 2],
[9, 9, 9]
])

In[268]: b.ndim
Out[268]: 2

In[269]: b.shape
Out[269]: (4, 3)```

Arrays can have many dimensions, but they become hard to visualize above two or three:

```In[276]: c = np.random.rand(2, 2, 3, 4)

In[277]: c
Out[277]:
array([
[
[
[0.33018579, 0.98074944, 0.25744133, 0.62154557],
[0.70959511, 0.01784769, 0.01955593, 0.30062579],
[0.83634557, 0.94636324, 0.88823617, 0.8997527]
],

[
[0.4020885, 0.94229555, 0.309992, 0.7237458],
[0.45036185, 0.51943908, 0.23432001, 0.05226692],
[0.03170345, 0.91317231, 0.11720796, 0.31895275]
]
],

[
[
[0.47801989, 0.02922993, 0.12118226, 0.94488471],
[0.65439109, 0.77199972, 0.67024853, 0.27761443],
[0.31602327, 0.42678546, 0.98878701, 0.46164756]
],

[
[0.31585844, 0.80167337, 0.17401188, 0.61161196],
[0.74908902, 0.45300247, 0.68023488, 0.79672751],
[0.23597218, 0.78416727, 0.56036792, 0.55973686]
]
]
])

In[278]: c.ndim
Out[278]: 4

In[279]: c.shape
Out[279]: (2, 2, 3, 4)```

In Numpy, dimension, axis/axes, shape are related and sometimes similar concepts:

```In[1]: import numpy as np

In[2]: a = np.array([
[1, 2],
[3, 4]
])```

In Numpy dimensions are called axes. The number of axes is rank.

```In[3]: a.ndim # num of dimensions / axes, * Mathematics definition of dimension *
Out[3]: 2```

the nth coordinate to index an `array` in Numpy. And multidimensional arrays can have one index per axis.

```In[4]: a[1, 0] # to index `a`, we specific 1 at the first axis and 0 at the second axis.
Out[4]: 3 # which results in 3(locate at the row 1 and column 0, 0 - based index)```

You can also use axis parameter in group operations, in case of axis=0 Numpy performs the action on elements of each column, and if axis=1, it performs the action on rows.

```test = np.arange(0, 9).reshape(3, 3)

Out[3]:
array([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
])

test.sum(axis = 0)
Out[5]: array([9, 12, 15])

test.sum(axis = 1)
Out[6]: array([3, 12, 21])```

In order to understand the dimensions and axes, it is important to understand tensors and its rank. A vector is a rank-1 tensor a matrix is a rank-2 tensor and so on and so forth. Consider the following:

`x = np.array([0, 3, 4, 5, 8])`

Suggestion : 2

Axes are defined for arrays with more than one dimension. A 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).,A way to represent items in a N-dimensional array in the 1-dimensional computer memory. In row-major order, the rightmost index “varies the fastest”: for example the array:,A way to represent items in a N-dimensional array in the 1-dimensional computer memory. In column-major order, the leftmost index “varies the fastest”: for example the array:,A 2-dimensional ndarray that preserves its two-dimensional nature throughout operations. It has certain special operations, such as * (matrix multiplication) and ** (matrix power), defined:

```>>> x = np.arange(12).reshape((3, 4))

>>>
x
array([
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]
])

>>>
x.sum(axis = 1)
array([6, 22, 38])```
```>>> x = np.array([1, 2, 3], float)

>>>
x
array([1., 2., 3.])

>>>
x.dtype # floating point number, 64 bits of memory per element
dtype('float64')

# More complicated data type: each array element is a combination of
# and integer and a floating point number >>>
np.array([(1, 2.0), (3, 4.0)], dtype = [('x', int), ('y', float)])
array([(1, 2.0), (3, 4.0)],
dtype = [('x', '<i4'), ('y', '<f8')])```
```>>> x = np.array([1, 2, 3]) >>>
x.shape(3, )```
```>>> x = np.array([1, 2]) >>>
y = np.array([
[3],
[4]
])

>>>
x
array([1, 2])

>>>
y
array([
[3],
[4]
])

>>>
x + y
array([
[4, 5],
[5, 6]
])```
```[
[1, 2, 3],
[4, 5, 6]
]```
`[1, 4, 2, 5, 3, 6]`

Suggestion : 3

Last Updated : 30 May, 2021,GATE CS 2021 Syllabus

Output :

```[
[1 2 3]
[11 22 33]
[4 5 6]
[8 9 10]
[20 30 40]
]

Sum array - wise: 204```