Analytics Vidhya
Published in

Analytics Vidhya

NumPy Manipulating the dimensions and shape of arrays.

Understanding axis in numpy, numpy.transpose, numpy.fliplr, numpy.flipud, numpy.rot90

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))array1
Out[7]:
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
array2 = np.random.choice([1],size = (3,3))array2
Out[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))array1
Out[7]:
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
array2 = np.random.choice([1],size = (3,3))array2
Out[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)array
Out[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).shape
Out[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)).shape
Out[13]: (2, 3, 4)

More Examples:

Flipping array left right or up down

array = np.diag([1,2,3,4,5])array
Out[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 flipped
np.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 flipped
np.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]])

--

--

--

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

Recommended from Medium

Streamline your mainframe development with Zowe Explorer & Zowe CLI (a series): Part 3 — REXX

Add Tailwindcss to Rails

The Three Ways to DevOps paradise

Are you planning your product to death?

Lightweight

AWS CodeCommit — A New Home For Your Repository

Rust FFI vs Golang FFI (cgo)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Siddharth Kshirsagar

Siddharth Kshirsagar

Data Scientist by Profession, Web Developer by passion

More from Medium

How to Improve Univariate Feature Imputation with Just One Line of Code

Ensemble Averaging — Improve machine learning performance by voting

Predicting State of Health and Lifecycle of Li-ion Batteries (pt.2)

Eigendecomposition of a Covariance Matrix with NumPy