NumPy ways to handle dimensions

Vidhi Chugh
Analytics Vidhya
Published in
3 min readJun 22, 2020

np.reshape, np.newaxis and np.expand_dims illustrated with python code

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 1: Rank 1 array to row/column vector conversion

Here, we have created an array of 4 elements with shape (4,) called as Rank 1 arrays.

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 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 e.g. 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 array below:

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

Thanks for reading !!!

Jupyter notebook with full code is placed here.

References:

--

--

Vidhi Chugh
Analytics Vidhya

Data Transformist and AI Strategist | International Speaker | AI Ethicist and Data-Centric Scientist | Global Woman Achiever https://allaboutscale.com/