Table of Contents
ToggleBroadcasting in NumPy refers to the ability of performing operations on arrays with different shapes by automatically expanding the smaller array’s shape to match the larger array’s shape. This is useful when performing arithmetic operations or applying functions to arrays of different dimensions.
When performing arithmetic operations, NumPy operates on corresponding elements of the arrays. If the arrays have the same shape, operations are smoothly performed. However, if the arrays have different shapes, NumPy uses broadcasting to align them, allowing element-wise operations to be conducted easily.
For broadcasting to work, the following rules must be satisfied −
For instance, consider two arrays with shapes (3, 4) and (4,). The broadcasting rules will align the shapes as follows −
When adding a scalar to an array, NumPy uses broadcasting to apply the scalar to each element of the array. Broadcasting expands the scalar to match the shape of the array, enabling element-wise operations.
In the following example, we are broadcasting the scalar “10” to each element of the array, resulting in each element being increased by 10 −
# Open Compiler import numpy as np # Creating an array array = np.array([[1, 2, 3], [4, 5, 6]]) # Adding a scalar result = array + 10 print(result)
Output:
Following is the output obtained −
[[11 12 13]
[14 15 16]]
When adding arrays of different shapes, NumPy applies broadcasting rules to make their shapes compatible. Broadcasting works by stretching the smaller array across the larger one, so that both arrays have the same shape for element-wise addition.
This process eliminates the need for manually reshaping arrays before performing operations.
In this example, we broadcast the second array “array2” to match the shape of the first array “array1” −
# Open Compiler import numpy as np # Creating two arrays with different shapes array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([10, 20, 30]) # Adding arrays with broadcasting result = array1 + array2 print(result)
Output:
This will produce the following result −
[[11 22 33]
[14 25 36]]
Following is another example to broadcast two arrays with different shapes in NumPy −
# Open Compiler
import numpy as np
a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])
print ('First array:')
print (a)
print ('\n')
print ('Second array:')
print (b)
print ('\n')
print ('First Array + Second Array')
print (a + b)
Output:
The output of this program would be as follows −
First array:
[[ 0. 0. 0.]
[10. 10. 10.]
[20. 20. 20.]
[30. 30. 30.]]
Second array:
[1. 2. 3.]
First Array + Second Array
[[ 1. 2. 3.]
[11. 12. 13.]
[21. 22. 23.]
[31. 32. 33.]]
The following figure demonstrates how array b is broadcast to become compatible with a.
[Image placeholder: Broadcasting figure]
When performing operations between multi-dimensional arrays with different shapes, broadcasting rules align their dimensions so that they can be operated on element-wise.
This process involves stretching the smaller array to match the shape of the larger one, enabling operations to be performed smoothly.
In the following example, we are creating two 3D arrays and then adding them with broadcasting −
# Open Compiler import numpy as np # Creating two 3D arrays array1 = np.ones((2, 3, 4)) array2 = np.arange(4) # Adding arrays with broadcasting result = array1 + array2 print(result)
Output:
Following is the output of the above code −
[[[1. 2. 3. 4.]
[1. 2. 3. 4.]
[1. 2. 3. 4.]]
[[1. 2. 3. 4.]
[1. 2. 3. 4.]
[1. 2. 3. 4.]]]
Broadcasting not only simplifies arithmetic operations between arrays of different shapes but also allows functions to be applied across arrays. These functions can include −
When applying functions to arrays with different shapes, broadcasting ensures that the function is applied element-wise.
In this example, we use the numpy.maximum() function to perform an element-wise comparison between two arrays. The function compares each element of “array1” with the corresponding element of “array2”, and the result is an array where each element is the maximum of the corresponding elements from the input arrays −
# Open Compiler import numpy as np # Creating arrays array1 = np.array([[1, 2, 3], [4, 5, 6]]) array2 = np.array([[10], [20]]) # Applying a function with broadcasting result = np.maximum(array1, array2) print(result)
Output:
After executing the above code, we get the following output −
[[10 10 10]
[20 20 20]]
Key Takeaway: Master NumPy broadcasting for efficient array operations at Vista Academy!
