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.
By Vidhi Chugh, Data Scientist
- 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.
Bio: Vidhi Chugh is inquisitive about the power of data, and is commonly referred to as a Data Scientist.
Original. Reposted with permission.
- Sparse Matrix Representation in Python
- Speed up your Numpy and Pandas with NumExpr Package
- Faster machine learning on larger graphs with NumPy and Pandas
Top Stories Past 30 Days