Topics: Coronavirus | AI | Data Science | Deep Learning | Machine Learning | Python | R | Statistics

KDnuggets Home » News » 2020 » Jul » Tutorials, Overviews » How to Handle Dimensions in NumPy ( 20:n28 )

How to Handle Dimensions in NumPy

Tags: ,

Learn how to deal with Numpy matrix dimensionality using np.reshape, np.newaxis and np.expand_dims, illustrated with Python code.



By Vidhi Chugh, Data Scientist

Figure

Image by Garik Barseghyan from Pixabay

 

np.newaxis

  • It is used to increase the dimension of the existing array. It uses the slicing operator to recreate the array.
  • The dimension is temporarily added at the position of np.newaxis in the array. ‘None’ can also be used in place of np.newaxis.

np.reshape:

  • It is used to reshape the array to the desired layout.

np.expand_dims:

  • It expands the shape of an array by inserting a new axis at the axis position in the expanded array shape

Let’s see some primary applications where above NumPy dimension handling operations come in handy:

 

Application 1Rank 1 array to row/column vector conversion

 
Here, we have created an array of 4 elements with shape (4,) which is called a Rank 1 array.

Array of 4 elements: [0 1 2 3]
Notice the shape, this is rank 1 array: (4,)
after transpose: (4,)


However, Rank 1 arrays often lead to ambiguous results as they do not behave as row/column vectors consistently. As shown above, if we take transpose of x1, its shape remains the same.

Hence, it’s always recommended to explicitly specify the dimensions of an array. This can be achieved by all 3 techniques explained above:

  • using np.newaxis:

row vector: [[0 1 2 3]]
(1, 4)

column vector:
 [[0]
 [1]
 [2]
 [3]]
(4, 1)


  • using np.reshape

Row vector using reshape: [[0 1 2 3]]
column vector using reshape:
 [[0]
 [1]
 [2]
 [3]]


  • using np.expand_dims

Row vector using expand_dims: [[0 1 2 3]]
column vector using expand_dims:
 [[0]
 [1]
 [2]
 [3]]


 

Application 2Increasing the dimension

 
Lets create another array x2 with shape (2,4,28) and check how we can expand the dimensions of x2 from 3D to 5D

Image for post

Key thing to note from above is np.reshape lets you split the dimension as well.

 

Application 3: Broadcasting

 
As per NumPy documentation:

 

broadcasting describes how numpy treats arrays with different shapes during arithmetic operations.

 

For example when we add the following 2 arrays, it shows 'ValueError' due to shape mismatch:

Image for post

Let’s see how np.newaxis increases the dimension of one of the arrays below:

Image for post

As we primarily need to adjust the dimension of the array for proper broadcasting operation, the np.reshape and np.expand_dims ways of increasing the dimension work equally well (as shown in the previous example).

Thanks for reading !!!

Jupyter notebook with full code is placed here.

 
References:

 
Bio: Vidhi Chugh is inquisitive about the power of data, and is commonly referred to as a Data Scientist.

Original. Reposted with permission.

Related:


Sign Up

By subscribing you accept KDnuggets Privacy Policy