The term broadcasting in NumPy refers to the ability of NumPy to treat arrays of different shapes during arithmetic operations. Arithmetic operations on arrays are usually done on corresponding elements. If two arrays are of exactly the same shape, then these operations are smoothly performed. In this tutorial, we will learn how we can use the broadcasting in Numpy to perform various operations. Moreover, we will take various scenarios with Python code and perform broadcasting.
Introduction to NumPy
NumPy is a library for the Python programming language, that adds support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. NumPy contains a large number of methods that can be used to perform different operations.
In this tutorial, our main focus is broadcasting in NumPy. But before going into broadcasting in NumPy, we need to install the NumPy module on our system.
You can use the pip command to install the NumPy module on your system.
#installing NumPy pip install numpy
Once the module is installed, you can check the version of the installed NumPy module by running the following commands in Python.
# importing the NumPy import numpy # printing the version print(numpy.__version__)
Output:

As you can see, I have NumPy version 1.21.5 installed on my system.
What is broadcasting in NumPy?
The term broadcasting in NumPy describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python. It does this without making needless copies of data and usually leads to efficient algorithm implementations. There are, however, cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows computation.
In other words, broadcasting in NumPy is performing different operations on all elements. For example, if we multiply two arrays together, then broadcasting will multiply the array element by element.
Example-1: Broadcasting in NumPy
Let us say that we want to multiply every element of array one with the corresponding elements of the second array. In general, we can use for loops to multiply as shown below:
# importing numpy import numpy as np # creating arrays a = np.array([1,2,3,4,5,6,7]) b = np.array([2,2,2,2,2,2,2]) result =[] # for loop for i in range(len(a)): result.append(a[i]*b[i]) # print print(result)
Output:

As you can see, we have multiplied each element from one array with the corresponding elements from the other array.
However, using for loop is not an efficient way. Numpy’s broadcasting allows us to do the same in a more efficient way. For example, see the code below:
# importing numpy import numpy as np # creating arrays a = np.array([1,2,3,4,5,6,7]) b = np.array([2,2,2,2,2,2,2]) # broadcasting in numpy print(a*b)
Output:

As you can see, we get the same result using broadcasting in NumPy.
Example-2: Broadcasting in NumPy
Now let us multiply a scalar value with a NumPy array. The scalar value will be multiplied with every element of the array as shown below:
# creating arrays a = np.array([1,2,3,4,5,6,7]) scalar = 10 # broadcasting in numpy print(scalar * a)
Output:

As you can see, the scalar value has been multiplied with each and every element of the array.
Example-3: Broadcasting in NumPy
Now let us see what will happen when we tried to multiply two arrays with different dimensionalities.
# creating arrays a = np.array([1,2,3,4,5,6,7]) b = np.array([2,2]) # broadcasting in numpy print( b* a)
Output:

As you can see, we get an error which means we can not apply broadcasting on two arrays with different dimensionalities.
But we can apply broadcasting in NumPy on multi-dimensional arrays as shown below:
# creating arrays a = np.array([[1,2],[3,4],[5,6]]) b = np.array([[2,2],[2,2],[2,2]]) # broadcasting in numpy print( b* a)
Output:

As you can see, we can also apply broadcasting in NumPy on multi-dimensional arrays as well.
Summary
The term broadcasting refers to how NumPy treats arrays with different Dimensions during arithmetic operations which lead to certain constraints, the smaller array is broadcast across the larger array so that they have compatible shapes. Broadcasting provides a means of vectorizing array operations so that looping occurs in C instead of Python as we know that Numpy is implemented in C. It does this without making needless copies of data and which leads to efficient algorithm implementations. There are cases where broadcasting is a bad idea because it leads to inefficient use of memory that slows down the computation. In this short article, we learned how we can use broadcasting in NumPy using various examples
Related Articles
- AttributeError: module ‘tensorflow’ has no attribute ‘Session’
- ValueError: invalid literal for int() with base 10 | Solved