NumPy broadcast() Function in Python

Spread the love

NumPy broadcast() function in Python is used to return an object that mimics broadcasting. It describes the ability of NumPy to treat arrays of different shapes during arithmetic operations. NumPy arithmetic operations are usually done on pairs of arrays on an element-by-element basis. In the simplest case, the two arrays must have exactly the same shape, then these operations will smoothly perform. Arrays with different sizes cannot add, subtract, or generally use in arithmetic.

To overcome this problem by duplicating the smaller array that is, it contains the same dimensions and size as the larger array. This process is called array broadcasting and is available in NumPy when performing array arithmetic, which can greatly reduce and simplify your code.

In this tutorial, you will learn the concept of array broadcasting and how to implement it in Python NumPy.

1. Quick Examples of broadcast() Function

If you are in a hurry, below are some quick examples of Python NumPy broadcast() function.


# Below are the quick examples
# Example 1 : Add two arrays using broadcasting
arr1 = np.array([10, 20,30]) # (1,3)
arr2 = np.array([40]) # (1,)
arr=arr1+arr2

# Example 2 : Add two arrays using broadcast()
arr = np.array([[4], [7], [12]])
arr1 = np.array([3, 6, 9])
arr2 = np.broadcast(arr, arr1)
out = np.empty(arr2.shape)
out.flat = [u+v for (u,v) in arr2]
print(out)

# Example # : Add two arrays using broadcasting rule
arr = np.array([[4], [7], [12]])
arr1 = np.array([3, 6, 9])
arr2=arr+arr1
print(arr2)

2. Syntax of numpy.broadcast()

Following is the syntax of numpy.broadcast().


# Syntax of numpy.broadcast()
numpy.broadcast(in1, in2, …)

2.2 Parameters of broadcast() Function

Following are the parameters of the broadcast() function.

  • in1,in2, … These are the input parameters(like arrays).

2.3 Return Value of NumPy broadcast()

Broadcast the input arguments against one another and return an object encapsulating the result. It has shape and nd attributes, among others, and can use as an iterator.

3. Usage of NumPy broadcast()

Element-to-element operations are not possible if the dimensions of two arrays differ. Because of the broadcasting capabilities, operations on arrays of non-similar shapes are still possible in NumPy. The smaller array is broadcast to the size of the larger array in order for their shapes to be compatible.

Broadcasting is possible if the following rules are satisfied

First rule: If the two arrays are having different dimensions, we should make equal dimensions. Padded 1’s in the shape of fewer dimension array on the left side. For example,

Here, I have declared two arrays named x, y :

x= (4,3), y =(3,) after applying the above rule the dimensions of x, y arrays changed into x = (4,3), y = (1,3). Now two arrays having same dimensions.

The second rule : If the size of two arrays are not same in any dimension, the array size equal to 1 in that dimension, is expanded to match other size of the same dimension.

x = (4,3), y = (1,3) after applying second rule the shapes of x, y arrays changed into x = (4,3), y = (4,3).

Now both arrays are having same dimensions and the same shape, the arithmetic operation will perform normally.

4. Use broadcast() on NumPy Arrays

In order to add two arrays, we have to satisfy broadcasting rules. Let’s take an example,


import numpy as np
# Add two arrays using broadcasting
arr1 = np.array([10, 20,30]) # (1,3)
arr2 = np.array([40]) # (1,)
arr=arr1+arr2
print(arr)

# Output:
# [50 60 70]

Here, happened the broadcast because of miss-match in the array dimension.

Using numpy.broadcast() we can add the two arrays.


# Add two arrays using broadcast()
arr = np.array([[4], [7], [12]])
arr1 = np.array([3, 6, 9])
arr2 = np.broadcast(arr, arr1)
out = np.empty(arr2.shape)
out.flat = [u+v for (u,v) in arr2]
print(out)

# OutPut
# [[ 7. 10. 13.]
#  [10. 13. 16.]
#  [15. 18. 21.]]

Alternatively use the below code we will get the same result as above.


# Add two arrays using broadcasting rule
arr = np.array([[4], [7], [12]])
arr1 = np.array([3, 6, 9])
arr2=arr+arr1
print(arr2)

# Output :
# [[ 7 10 13]
#  [10 13 16]
#  [15 18 21]]

Conclusion

In this article, I have explained the concept of array broadcasting and how to implement it using the NumPy broadcast() function with example.

Happy Learning!!

References

Leave a Reply

You are currently viewing NumPy broadcast() Function in Python