Python NumPy – broadcast_to() and broadcast_arrays()

Introduction to Python NumPy broadcast_to()

Python NumPy – numpy.broadcast_to() function broadcast an array to a new shape.

Syntax:

numpy.broadcast_to(array, shape, subok = False)

Parameters:

  • array : [array_liket] The array to broadcast.
  • shape : [tuple] The shape of the desired array.
  • subok : [bool, optional] If True, then sub-classes will be passed-through, otherwise by default, the returned array will be forced to be a base-class array.

Return: [array] The output array.

Code Examples of Python NumPy broadcast_to()

Example 01:

# welcome to softhunt.net
# Python program explaining
# numpy.broadcast_to() function

# importing numpy as np
import numpy as np

arr = np.array([5, 6, 7, 8, 9, 10])

softhunt = np.broadcast_to(arr, (6, 6))

print(softhunt)

Output:

[[ 5  6  7  8  9 10]
 [ 5  6  7  8  9 10]
 [ 5  6  7  8  9 10]
 [ 5  6  7  8  9 10]
 [ 5  6  7  8  9 10]
 [ 5  6  7  8  9 10]]

Example 02:

# welcome to softhunt.net
# Python program explaining
# numpy.broadcast_to() function

# importing numpy as np
import numpy as np

arr = np.array([9, 7, 4])

softhunt = np.broadcast_to(arr, (3, 3))

print(softhunt)

Output:

[[9 7 4]
 [9 7 4]
 [9 7 4]]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

Introduction to Python NumPy broadcast_arrays()

We may get a single broadcasted array with the help of two or more arrays by utilizing the numpy.broadcast_arrays() function.

Syntax:

Numpy.broadcast_arrays()

Return: It returns the broadcasted array using NumPy.

Code Examples of broadcast_arrays()

Example 01: We can see in this example that we can acquire the broadcasted array utilizing two or more NumPy arrays by using the numpy.broadcast_arrays() function.

# welcome to softhunt.net
# import numpy
import numpy as np

# using Numpy.broadcast_arrays() method
arr1 = np.array([[1, 2]])
arr2 = np.array([[3], [4]])

print(np.broadcast_arrays(arr1, arr2))

Output:

[array([[1, 2],
       [1, 2]]), array([[3, 3],
       [4, 4]])]

Example 02:

# welcome to softhunt.net
# import numpy
import numpy as np

# using Numpy.broadcast_arrays() method
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])

print(np.broadcast_arrays(arr1, arr2))

Output:

[array([[1, 2],
       [3, 4]]), array([[5, 6],
       [7, 8]])]

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

FAQs

What is NumPy and Why it used for in Python?

NumPy may be use to perform a wide range of array-based mathematical operations. It extends Python with sophisticated data structures that ensure fast computations with arrays and matrices, as well as a large library of high-level mathematical functions that work with these arrays and matrices.

What is the Broadcast in NumPy?

NumPy’s processing of arrays of diverse forms during arithmetic operations is refer to as broadcasting. The smaller array is “broadcast” across the bigger array, subject to specific limits, such that their shapes are compatible.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

  1. Broadcasting with NumPy Arrays

Leave a Comment