Numpy expand_dims() Function



The Numpy expand_dims() Function is used to add a new axis or dimension to an array. This function takes an input array and an axis parameter specifying where to insert the new dimension.

This function results in a new array with the specified axis added, which can be useful for adjusting array shapes to meet the requirements of certain operations or functions.

For instance, expanding dimensions is often used to convert a 1D array into a 2D array by making it compatible for broadcasting or matrix operations.

Syntax

The syntax for the Numpy expand_dims() function is as follows −

numpy.expand_dims(a, axis)

Parameters

Below are the parameters of Numpy expand_dims() Function −

  • a :Input array.
  • axis(int): This is the position in the expanded axes where the new axis is placed. If we provide a negative integer, it counts from the last to the first axis.

Return Value

This function retuns a view of array with the number of dimensions increased by one.

Example 1

Following is the basic example of Numpy expand_dims() function which adds a new axis at the beginning of the 1D array by converting it into a 2D array with shape (1, 3) −

import numpy as np

# Original 1D array
arr = np.array([1, 2, 3])

# Expand dimensions
expanded_arr = np.expand_dims(arr, axis=0)

print("Original array:")
print(arr)
print("Shape:", arr.shape)

print("\nExpanded array:")
print(expanded_arr)
print("Shape:")
print(expanded_arr.shape)

Output

Original array:
[1 2 3]
Shape: (3,)

Expanded array:
[[1 2 3]]
Shape: 
(1, 3)

Example 2

Here this example adds a new axis at the end of the 3D array by changing its shape from (2, 2, 2) to (2, 2, 2, 1) −

import numpy as np

# Original 3D array
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Expand dimensions
expanded_arr = np.expand_dims(arr, axis=-1)

print("Original array:")
print(arr)
print("Shape:", arr.shape)

print("\nExpanded array:")
print(expanded_arr)
print("Shape:")
print(expanded_arr.shape)

After execution of above code, we get the following result

Original array:
[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]
Shape: 
(2, 2, 2)

Expanded array:
[[[[1]
   [2]]

  [[3]
   [4]]]


 [[[5]
   [6]]

  [[7]
   [8]]]]
Shape: (2, 2, 2, 1)

Example 3

The below example shows how expand_dims() function is used to add new dimensions to arrays by adjusting their shapes and dimensions as needed −

import numpy as np 

# Create a 2D array
x = np.array([[1, 2], [3, 4]])

print('Array x:')
print(x)
print('\n')

# Add a new axis at position 0
y = np.expand_dims(x, axis=0)

print('Array y with a new axis added at position 0:')
print(y)
print('\n')

# Print the shapes of x and y
print('The shape of x and y arrays:')
print(x.shape, y.shape)
print('\n')

# Add a new axis at position 1
y = np.expand_dims(x, axis=1)

print('Array y after inserting axis at position 1:')
print(y)
print('\n')

# Print the number of dimensions (ndim) for x and y
print('x.ndim and y.ndim:')
print(x.ndim, y.ndim)
print('\n')

# Print the shapes of x and y
print('x.shape and y.shape:')
print(x.shape, y.shape)

After execution of above code, we get the following result

Array x:
[[1 2]
 [3 4]]


Array y with a new axis added at position 0:
[[[1 2]
  [3 4]]]


The shape of x and y arrays:
(2, 2) (1, 2, 2)


Array y after inserting axis at position 1:
[[[1 2]]

 [[3 4]]]


x.ndim and y.ndim:
2 3


x.shape and y.shape:
(2, 2) (2, 1, 2)
numpy_array_manipulation.htm
Advertisements