Python NumPy Broadcasting

In this tutorial, we will cover the concept of broadcasting in the NumPy library.

Sometimes while doing mathematical operations we need to consider arrays of different shapes. With the help of Numpy library, one is allowed to perform operations on arrays of different shapes. Suppose you want to add two matrices and both matrices have the same shape that is 3x3 and 3x3 then matrices can be added very easily. But what if you want to add matrices one with shape 3x3 and second with shape 2x2 then it will lead to an error. To resolve this problem there comes the concept of Broadcasting in NumPy.

Numpy Broadcasting

In Numpy, the term broadcasting refers to how Numpy handles array of different dimensions while performing any arithmetic operation. In broadcasting, Numpy generally broadcasts the smaller array across the larger array in order to have compatible shapes.

When we perform arithmetic operations in case of multi-dimensional arrays, it is done on corresponding elements. So in the case if two arrays are having the same shape, then performing arithmetic operations is super easy.

Let us take two examples where we will try to perform an arithmetic operation on two Arrays with the same shape and we will also show you what happens when we will perform operations on Arrays with different shapes.

Example 1: Adding two 1-d Arrays of the same shape

In the example given below we will add two one-dimensional arrays with the same shape:

import numpy as np

a = np.array([1,2,3,4])
b = np.array([2,7,8,9])
c = a+b;
print(c)


[ 3 9 11 13]

Example 2: Adding two 1-d Arrays of different shape

In the example given below we will add two one-dimensional arrays with different shapes and will check what we get in the output:

import numpy as np  

a = np.array([4,5,6,7])  
b = np.array([1,3,5,7,9,11,14])  
c = a+b;  
print(c) 


---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-2203233bfeba> in <module>
2 a = np.array([4,5,6,7])
3 b = np.array([1,3,5,7,9,11,14])
----> 4 c = a+b;
5 print(c)

ValueError: operands could not be broadcast together with shapes (4,) (7,)

As it is clear to you that whenever we will apply arithmetic operations on arrays with different shapes then it will result in an error.

Therefore, in NumPy, such operations can only be performed by using the concept of broadcasting. In broadcasting, generally, the smaller array is broadcast to the larger array in order to make their shapes compatible with each other.

Let us take an example to show you how broadcasting is done in NumpPy:

concept of broadcasting in Numpy

In the above figure, we have two matrices, one is of 3x3 and another one is of 1x3. In broadcastng, the 1x3 matrix, which is the smaller one, broadcast or stretch itself in order to get compatible with 3x3. And how does it stretches by creating additional fields copying the 1st row two more times to take the shape of a 3x3 matrix.

We also have some rules for broadcasting let us take a look at that.

Rules for NumPy Broadcasting

The concept of broadcasting in NumPy is only possible if the following cases are satisfied:

  1. The smaller dimension ndarray can be appended with '1' in its shape.

  2. The size of each output dimension should be the maximum of the input sizes in the dimension.

  3. It is important to note that input can be used in the calculation only if its size in a particular dimension matches the output size or its value is exactly 1.

  4. Suppose the input size is 1, then the first data entry should be used for the calculation along the dimension.

The concept of broadcasting in NumPy can be applied to the arrays only if the following rules are satisfied.

  1. All the arrays in the input must have the same shape.

  2. Arrays having the same number of dimensions, and the length of each dimension is either a common length or 1.

  3. Those arrays with the fewer dimension can be appended with '1' in its shape.

Let us ave another example in order to gain more understanding.

Example for NumPy Broadcasting:

Below we have a code snippet where we will do the addition of two arrays, of which one is of 3x4 and another is of 1x4, then the resultant array that will be formed should be of 3x4:

import numpy as np  

a = np.array([[1,2,3,4],[11,10,8,6],[10,20,39,3]])  
b = np.array([4,8,10,12])
  
print("\n The array a is :")  
print(a)  
print("\n The array b is :")  
print(b)  

print("\n After addition of array a and b resultant array is:")  
c = a + b;  
print(c)  


The array a is :
[[ 1 2 3 4]
[11 10 8 6]
[10 20 39 3]]

The array b is :
[ 4 8 10 12]

After addition of array a and b resultant array is:
[[ 5 10 13 16]
[15 18 18 18]
[14 28 49 15]]

Summary

In this tutorial, we have studied what is meant by the term broadcasting and in Numpy how arrays are stretched in order to perform mathematical operations. We had also covered rules for broadcasting and conditions under which broadcasting can be done. Then with the help of example, we tried to clear the concept of NumPy Broadcasting.