Published in

Analytics Vidhya

# NumPy Manipulating the dimensions and shape of arrays.

## Numpy axis are defined along rows and columns

axis 0: Is the axis along the rows, this axis is also known as “first axis”

axis 1: Is the axis along the columns

Taking sum along axis 0

`array = np.arange(9).reshape(3,3)np.sum(array,axis = 0)Out[4]: array([ 9, 12, 15])`

Taking sum across axis 1

`array = np.arange(9).reshape(3,3)np.sum(array,axis = 1)Out[5]: array([ 3, 12, 21])[ 3, 12, 21]`

Concatenating along axis 0

`array1 = np.random.randint(0,1,size = (3,3))array1Out[7]: array([[0, 0, 0],       [0, 0, 0],       [0, 0, 0]])array2 = np.random.choice([1],size = (3,3))array2Out[9]: array([[1, 1, 1],       [1, 1, 1],       [1, 1, 1]])np.concatenate((array1,array2), axis = 0)Out[10]: array([[0, 0, 0],       [0, 0, 0],       [0, 0, 0],       [1, 1, 1],       [1, 1, 1],       [1, 1, 1]])`

Concatenating along axis 1

`array1 = np.random.randint(0,1,size = (3,3))array1Out[7]: array([[0, 0, 0],       [0, 0, 0],       [0, 0, 0]])array2 = np.random.choice([1],size = (3,3))array2Out[9]: array([[1, 1, 1],       [1, 1, 1],       [1, 1, 1]])np.concatenate((array1,array2), axis = 1)Out[11]: array([[0, 0, 0, 1, 1, 1],       [0, 0, 0, 1, 1, 1],       [0, 0, 0, 1, 1, 1]])`

Transposing a given matrix

`numpy.transpose(a, axes=None)`

axes: tuple or list which contains the permutations of values of axis if there are three axis the axes parameter can contain (0,1,2) or (1,2,0) or (0,2,1) or (1,0,2) or (2,1,0) or (2,0,1). If not specified it defaults to (2,1,0) or reversing the dimension.

Consider the matrix with shape (4,2,3) i.e matrix with dimension (2 X 3) repeats 4 times.

`array = np.ones((4,2,3)) # four times repeat the matrix (2 X 3)arrayOut[9]: array([[[1., 1., 1.],        [1., 1., 1.]],        [[1., 1., 1.],        [1., 1., 1.]],        [[1., 1., 1.],        [1., 1., 1.]],        [[1., 1., 1.],        [1., 1., 1.]]])`

After applying transpose and checking the dimension we observe that the dimensions of the matrix are reversed.

`np.transpose(array)Out[10]: array([[[1., 1., 1., 1.],        [1., 1., 1., 1.]],        [[1., 1., 1., 1.],        [1., 1., 1., 1.]],        [[1., 1., 1., 1.],        [1., 1., 1., 1.]]])np.transpose(array).shapeOut[11]: (3, 2, 4)`

Swapping dimensions (0,1,2) → (1,2,0): In this case matrix of dimension (3 X 4) repeats 2 times

`np.transpose(array,axes = (1,2,0))Out[12]: array([[[1., 1., 1., 1.],        [1., 1., 1., 1.],        [1., 1., 1., 1.]],       [[1., 1., 1., 1.],        [1., 1., 1., 1.],        [1., 1., 1., 1.]]])np.transpose(array,axes = (1,2,0)).shapeOut[13]: (2, 3, 4)`

More Examples:

Flipping array left right or up down

`array = np.diag([1,2,3,4,5])arrayOut[8]: array([[1, 0, 0, 0, 0],       [0, 2, 0, 0, 0],       [0, 0, 3, 0, 0],       [0, 0, 0, 4, 0],       [0, 0, 0, 0, 5]])# columns are preserved and rows are flippednp.fliplr(array)Out[9]: array([[0, 0, 0, 0, 1],       [0, 0, 0, 2, 0],       [0, 0, 3, 0, 0],       [0, 4, 0, 0, 0],       [5, 0, 0, 0, 0]])# rows are preserved but columns are flippednp.flipud(array)Out[11]: array([[0, 0, 0, 0, 5],       [0, 0, 0, 4, 0],       [0, 0, 3, 0, 0],       [0, 2, 0, 0, 0],       [1, 0, 0, 0, 0]])`

--

--

--

## More from Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

## Siddharth Kshirsagar

Data Scientist by Profession, Web Developer by passion