How to Handle Dimensions in NumPy
Learn how to deal with Numpy matrix dimensionality using np.reshape, np.newaxis and np.expand_dims, illustrated with Python code.
- 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.
- It is used to reshape the array to the desired layout.
- 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 1: Rank 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: [   ] (4, 1)
- using np.reshape
Row vector using reshape: [[0 1 2 3]] column vector using reshape: [   ]
- using np.expand_dims
Row vector using expand_dims: [[0 1 2 3]] column vector using expand_dims: [   ]
Application 2: Increasing 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
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:
Let’s see how np.newaxis increases the dimension of one of the arrays below:
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.
Vidhi Chugh is an award-winning AI/ML innovation leader and an AI Ethicist. She works at the intersection of data science, product, and research to deliver business value and insights. She is an advocate for data-centric science and a leading expert in data governance with a vision to build trustworthy AI solutions.
Original. Reposted with permission.