Understanding NumPy Broadcasting

NumPy

NumPy, short for Numerical Python, is a fundamental library for scientific computing in Python. It supports large multi-dimensional arrays and matrices and a collection of mathematical functions to operate on these arrays efficiently.

NumPy

NumPy Broadcasting

Numpy broadcasting is a feature that allows element-wise operations on arrays of different shapes.

Rules of Broadcasting
 

1. Matching Dimensions

When operating on 2 arrays, numpy compares their shapes, starting from the trailing dimensions. They are compatible if they are equal or one of them is of size 1.

import numpy as np
a= np.array([1,2,3])
b= 2
res=a+b
print(res)

a is an array of one dimension and b is of size 1, if we add both of them then the resultant is 1* 3, and 2 is added in all the elements of an array a.

2. Shape Expansion

If the arrays have different shapes, the smaller array is virtually replicated along the dimensions to operate.

import numpy as np
c= np.array([[1,2,3],[4,5,6]])
d= np.array([1,2,3])
rest= c+d
print(rest)

Here, c is an array of 2-D and d is of 1-D then d replicates itself to perform an arithmetic operation.

Advantages of Broadcasting

  1. Saves Memory
  2. A smaller array
  3. replicates itself to operate.
  4. Eliminates the need for Python Loops.

Arithmetic operations are performed automatically with the help of this library and need not to write the Python loops.

In some cases, Broadcasting stretches both arrays to form an output array larger than the initial arrays.

import numpy as np
e= np.array([1,2])
r= e[:,np.newaxis]
f=np.array([3,4])
re= r+f
print(re)

Here, r is an array of 2*1 dimensions and f is an array of 1*2 dimensions. After adding both the arrays, the resultant is 2*2 dimensions.


Similar Articles