Python | Numpy.expand_dims () method



With the Numpy.expand_dims() method we can get the extended dimensions of an array using Numpy.expand_dims () .

Syntax: Numpy.expand_dims()

Return: Return the expanded array.

Example # 1:
In this example we can see that using Numpy.expand_dims () we can get an extended an array using this method.

# NumPy import

import numpy as np

 
# using Numpy.expand_dims () method

gfg = np.array ([ 1 , 2 ])

print (gfg.shape)

 

gfg = np.expand_dims (gfg, axis = 0 )

print (gfg.shape)

Output:

(2,)
(1, 2)

Example # 2:

# NumPy import

import numpy as np

 
# using the Numpy.expand_dims () method

gfg = np.array ([[ 1 , 2 ], [ 7 , 8 ]])

print (gfg.shape)

 

gfg = np.expand_dims (gfg, axis = 0 )

print (gfg.shape)

Output:

( 2, 2)
(1, 2, 2)