NumPy Tutorial NumPy Statistics NumPy References

NumPy - broadcast_to() function



The NumPy broadcast_to() function broadcasts an array to a new shape.

Syntax

numpy.broadcast_to(array, shape)

Parameters

array Required. Specify the array (array_like) to broadcast.
shape Required. Specify the shape of the desired array.

Return Value

Returns a read-only view on the original array with the given shape.

Example:

In the example below, an array is broadcasted to a given shape.

import numpy as np

x = np.array([1, 2, 3])
y = np.array([10, 20])

#x is broadcasted to shape (3,3)
x1 = np.broadcast_to(x, (3, 3))

#displaying results
print("x1 contains:")
print(x1)

The output of the above code will be:

x1 contains:
[[1 2 3]
 [1 2 3]
 [1 2 3]]

❮ NumPy - Array Manipulation