NumPy - Broadcasting

NumPy Broadcasting

Broadcasting 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 are smoothly performed. However, if the arrays have different shapes, NumPy uses broadcasting to align them, allowing element-wise operations to be conducted easily.

Rules of Broadcasting

For broadcasting to work, the following rules must be satisfied −

  • If arrays have a different number of dimensions, the shape of the smaller-dimensional array is padded with ones on the left side until both shapes have the same length.
  • The size of each dimension must either be the same or one of them must be one.
  • Broadcasting is applied from the last dimension to the first dimension.

For instance, consider two arrays with shapes (3, 4) and (4,). The broadcasting rules will align the shapes as follows −

  • Pad the smaller array’s shape: The smaller array shape (4,) is padded to (1, 4).
  • Align dimensions: The shapes (3, 4) and (1, 4) are aligned as (3, 4) and (3, 4) respectively.
  • Perform element-wise operation: The operation is applied to the aligned shapes.

Adding a Scalar to an Array

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.

Example

In the following example, we are broadcasting the scalar “10” to each element of the array, resulting in each element being increased by 10 −

import numpy as np

# Creating an array
array = np.array([[1, 2, 3], [4, 5, 6]])

# Adding a scalar
result = array + 10
print(result)

Following is the output obtained −

[[11 12 13][14 15 16]]

Adding Arrays of Different Shapes

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.

Example 1

In this example, we broadcast the second array “array2” to match the shape of the first array “array1” −

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)

This will produce the following result −

[[11 22 33]
 [14 25 36]]

Example 2

Following is another example to broadcast two arrays with different shapes in NumPy −

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)

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.

array

Broadcasting with Multi-Dimensional Arrays

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.

Example

In the following example, we are creating two 3D arrays and then adding them with broadcasting −

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)

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.]]]

Applying Functions with Broadcasting

Broadcasting not only simplifies arithmetic operations between arrays of different shapes but also allows functions to be applied across arrays. These functions can include −

  • Mathematical Functions: Functions that perform mathematical operations, such as addition, subtraction, multiplication, and division.
  • Statistical Functions: Functions that compute statistical properties, like mean, median, variance, and standard deviation.
  • Reduction Functions: Functions that reduce the dimensions of an array by performing operations such as sum, product, or maximum.
  • Logical Operations: Functions that perform logical operations, such as comparisons and logical operations (e.g., AND, OR, NOT).

When applying functions to arrays with different shapes, broadcasting ensures that the function is applied element-wise.

Example

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 −

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)

After executing the above code, we get the following output −

[[10 10 10]
 [20 20 20]]
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team