Open In App
Related Articles

Insert a new axis within a NumPy array

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

This post deals with the ways to increase the dimension of an array in NumPy. NumPy provides us with two different built-in functions to increase the dimension of an array i.e.,

1D array will become 2D array
2D array will become 3D array
3D array will become 4D array
4D array will become 5D array

Add a New Axis to a Numpy Array

There are various ways to add a new axis to a NumPy array, here we are discussing some generally used methods for adding a new axis to a numpy array those are following.

Using numpy.newaxis()

The first method is to use numpy.newaxis object. This object is equivalent to use None as a parameter while declaring the array. The trick is to use the numpy.newaxis object as a parameter at the index location in which you want to add the new axis.

In this example This code uses NumPy to create a 2D array ‘arr’ with shape (5, 5) containing sequential values. It then transforms ‘arr’ into a 5D array ‘arr_5D’ by adding dimensions using indexing with ‘np.newaxis’. The resulting shape of ‘arr_5D’ is (1, 5, 5, 1, 1).

Python3

import numpy as np
 
arr = np.arange(5*5).reshape(5, 5)
print(arr.shape)
 
# promoting 2D array to a 5D array
# arr[None, ..., None, None]
arr_5D = arr[np.newaxis, ..., np.newaxis, np.newaxis]
 
print(arr_5D.shape)

                    

Output: 

(5, 5)
(1, 5, 5, 1, 1)

Using numpy.expand_dims()

The second method is to use numpy.expand_dims() function that has an intuitive axis kwarg. This function takes two parameters. The first is the array of which you want to increase the dimension of and the second is index/indexes of array on which you want to create a new axis.

In this example The code uses NumPy to create a 3×4 matrix of zeros named ‘x’. It then expands the dimensions of ‘x’ along axis 1, resulting in a new shape, and prints the shape. In this case, the output is (3, 1, 4), indicating an additional dimension added along axis 1.

Python3

import numpy as np
 
x = np.zeros((3, 4))
y = np.expand_dims(x, axis=1).shape
print(y)

                    

Output: 

(3, 1, 4)

Simultaneously Insert Many New Axes in the Array

In this method uses NumPy’s `expand_dims` method to insert multiple new axes into a given array simultaneously. The array `arr` is initially created as a 5×5 array, and the `newaxes` tuple with values (0, 3, -1) is used to specify the positions of the new axes. The resulting 5D array, `arr_5D`, is printed along with its shape.

In this example code uses the NumPy library to create a 5×5 array and prints its shape. Then, it defines a tuple `newaxes` with values (0, 3, -1) and uses `np.expand_dims` to add three new dimensions to the original array based on the specified axes. Finally, it prints the shape of the resulting 5D array.

Python3

import numpy as np
 
arr = np.arange(5*5).reshape(5,5)
print(arr.shape)
 
newaxes = (0, 3, -1)
arr_5D = np.expand_dims(arr, axis=newaxes)
print(arr_5D.shape)

                    

Output: 

(5, 5)
(1, 5, 5, 1, 1)

Using numpy.reshape() for a Single Axis

The numpy.reshape() method is employed to insert a new axis into a NumPy array along a single dimension. The original array is reshaped to a specified shape, effectively adding a new axis. For instance, if the initial array is one-dimensional, calling arr.reshape((n, 1)) introduces a new axis along the second dimension.

In this example code uses the NumPy library to create a one-dimensional array ‘arr’ with values from 0 to 5. The ‘reshape’ function is then applied to ‘arr’ to create a two-dimensional array ‘arr_reshape’ with dimensions (2, 3).

Python3

import numpy as np
arr = np.arange(6)
arr_reshape = arr.reshape((2, 3))

                    

Output:

[[0 1 2]
[3 4 5]]


Last Updated : 10 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads