NumPy : Beyond the Basics

Kavita RanaKavita Rana
4 min read

In our previous NumPy blog, we laid the groundwork, understanding the essence of arrays and touched advanced concepts. Now, let's take a leap into the more intricate features, unlock advanced functionalities, and discover the nuances that make NumPy a true powerhouse.

Broadcasting

Arithmetic operations cannot be performed between two arrays with different shapes until and unless the dimensions of the arrays match. Broadcasting is an attempt to make the dimensions of arrays with different shapes, same or compatible so that an operation can be performed. It essentially extends the smaller array to match the shape of the larger one, enabling element-wise operations. Broadcasting is implicit in NumPy.

When will broadcasting take place?

Broadcasting is not required when the shapes of the arrays involved in an operation are already compatible. Two shapes are compatible when, at each corresponding dimension, they are either equal or one of them is 1.

If you have a 2D array and a 1D array , they are of different dimensions and to make them compatible you will have to make them have same number of dimensions. Which means the 1D array will be stretched to become a 2D array.

import numpy as np
arr_2d = np.array([[1, 2, 3],[4, 5, 6]])
arr_1d = np.array([10, 20, 30])
#Broadcasting takes place where the arr_1d becomes ([10,20,30],[10,20,30])

result = arr_2d + arr_1d
print(result) 

>>Out: 
[[11 22 33]
 [14 25 36]]

If the number of dimensions of two arrays are different, broadcasting makes them same by adding a new dimension with size 1 to the head of the array with the smaller dimension. [documentation]

So if lets say we have a 3D array with the shape (3,3,3) and a 1D array with the shape (3). The broadcasting rules in NumPy automatically pad the smaller dimensions with ones to make the shapes compatible. So, the 1D array with shape (3) would be broadcasted to (1, 3), and then to (1, 1, 3) to match the shape of the 3D array.

If the sizes of each of the dimension of the two arrays do not match, dimensions with the size 1 will be stretched to the size of the other array. If there is a dimension in both array is not 1 then broadcasting is not possible and an error is raised. [documentation]

arr3 = np.array([[1, 2, 3],
                 [4, 5, 6]]) #shape is (2,3)

arr4 = np.array([1, 2, 3, 4])#shape is (4) no dimension has same value or 1. 

result = arr3 + arr4 
#ValueError: operands could not be broadcast together with shapes (2,3) (4,)

In summary :

  • If the number of dimensions of two arrays is different, broadcasting adds a new dimension with size 1 to the smaller array to match the larger one.

  • If the sizes of each dimension of the two arrays do not match, dimensions with size 1 will be stretched to the size of the other array.

  • If there is a dimension in both arrays that is not 1, broadcasting is not possible, and an error is raised.

If you understood and liked the blog so far dont forget to like and share it!!

More examples

a = np.arange(12).reshape(3,4)
b = np.arange(3)

print(a+b) 
'''Number of dimensions is not same.b is the smaller array so we add 1 
in its head (1,3). Since 1 is present, we stretch it in order for it to 
have same size in dimensions (3,3).
The sizes of both arrays still dont match and a value error is raised.
'''
a = np.arange(4).reshape(1,4)
b = np.arange(3).reshape(3,1)

'''The number of dimensions is same. We stretch the dimension=1 to have the 
so that they have same size in each dimension. a will be 3,4 and b will be 3,4
Arrays are compatible. 
'''
a = np.arange(3).reshape(3,1)
b = np.arange(3).reshape(1,3)

"""the number of dimensions is same.We have to stretch the arrays so they have
the same size in each dimension. a becomes 3,3 and b becomes 3,3.
Arrays are compatible. 
"""
a = np.arange(12).reshape(3,4)
b = np.arange(12).reshape(4,3)
'''The number of dimensions is same.The size in each dimension is not same and 
there is no 1 in either of the arrays. They are not compatible. 
'''

If you want more examples to practice with type this prompt in ChatGPT and learn:

"I want to test my understanding on NumPy broadcasting. Can you give code snippets with two arrays a and b and i will answer if they are compatible or not for an operation."

Conclusion

Hope you love this blog. If you do , like and share it. Comment any topic you would like me to cover in next blog. Happy Coding.

References

  1. https://numpy.org/doc/stable/user/basics.broadcasting.html

  2. https://machinelearningmastery.com/broadcasting-with-numpy-arrays/

2
Subscribe to my newsletter

Read articles from Kavita Rana directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kavita Rana
Kavita Rana

I am currently pursuing a degree in Computer Science with a specialization in Business Analytics and Optimization.