NumPy Broadcasting Computation on Arrays

Free NumPy course with real-time projects Start Now!!

Broadcasting in NumPy is a very useful concept. The array size is a very important concept when performing arithmetic operations on arrays. Arrays cannot always be of the same shape.

We perform the array operations on corresponding array elements. Hence we broadcast the smaller array along with the larger array. This is done to achieve similar dimensions.

NumPy Broadcasting

NumPy Broadcasting

This functionality helps to perform arithmetic operations with ease. We use it to broadcast the arrays to convert them into similar shapes.
The following rules are applicable on the arrays for broadcasting:

1. If both the arrays do not have similar shapes, then we prepend the array of lower rank with 1s. This process continues until both the arrays have the same shape.
2. We can consider the arrays compatible if they have the same length in a dimension or if one of the arrays has size as 1.
3. Broadcasting is applicable if arrays are compatible in all the dimensions.
4. The shape of the arrays turns out to be equivalent element-wise after we perform broadcasting.
5. The smaller array appears as if it is a copy along the dimension.
6. The length of each dimension is either the same or 1.

a. Broadcasting array with the same shape

a = np.array([1.0, 2.0, 3.0])
b = np.array([2.0, 2.0, 2.0])
a * b

Output

array([ 2., 4., 6.])

b. Broadcasting an array along with a scalar value

a = np.array([1.0, 2.0, 3.0])
b = 2.0
a * b

Output

array([ 2., 4., 6.])

In both of the above examples, we have done broadcasting keeping in mind the broadcasting rules.

NumPy Broadcasting in 1-D array

Array broadcasting when one of the array dimensions are 1. The array with the smaller dimension is then broadcasted along the larger dimension.

import numpy as np 
arr1 = np.array([56,78,94]) 
print(arr1) 
arr2 = 5 
print(arr2) 
  
a=arr1+arr2 
print(a)

Output

[56 78 94]
5
[61 83 99]

Here we carry out broadcasting over the smaller array. The broadcasting is due to the miss-match shape of the arrays.

NumPy Broadcasting in 2-D array

One of the arrays is a 2-dimensional array. We perform broadcasting on the smaller array.

import numpy as np 
arr1 = np.array(([45,90,87],[56,78,94])) 
print(arr1) 
arr2 = 5 
print(arr2) 
  
a=arr1+arr2 
print(a) 

Output

[[45 90 87]
[56 78 94]]
5
[[50 95 92]
[61 83 99]]

Similarly we perform broadcasting on 3 or more dimensional arrays.

NumPy Broadcasting in 3-D array

We take a 3-dimensional array and broadcast along with a 1-D array.

import numpy as np
A = np.arange(24).reshape(2,3,4)
A
A.shape
B = np.arange(4)
B
B.shape
A+B

Output

array([[[ 0, 2, 4, 6],
[ 4, 6, 8, 10],
[ 8, 10, 12, 14]],[[12, 14, 16, 18],
[16, 18, 20, 22],
[20, 22, 24, 26]]])

NumPy Broadcasting Limitations

Broadcasting is a very easy and handy tool when working with Numpy arrays. Although, there are many rules and restrictions to perform the operations.

There are constraints on the dimension and shape of the arrays. We can perform broadcasting in cases where both the arrays have the same shape or only if either of the two has a size of 1. Also, we have to consider the dimensions in reverse order. We count it from the trailing dimension.

Summary

NumPy Broadcasting is a very important NumPy module for performing arithmetic operations. It helps to modulate the array shape automatically. We translate the shape of the array for compatible operations.

The array has to follow the rules of compatible shape for broadcasting. Arrays with the same shape or with one of the arrays with size one are compatible.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *