Table of contents
1.
Introduction
2.
What is Vectorization?
2.1.
Python
2.2.
Python
3.
What is Broadcasting?
3.1.
Python
4.
Difference between Vectorisation and Broadcasting
5.
Frequently Asked Questions
5.1.
What is Vectorization?
5.2.
What is Broadcasting?
5.3.
What is the difference between Vectorisation and Broadcasting in Python?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference Between Vectorisation and Broadcasting

Introduction

There are many operations performed on arrays in Python. Some of them take a lot of time, while some of them occur fast. Also, there are different techniques used for performing operations on arrays. Vectorisation and Broadcasting are two such essential techniques. Moreover, broadcasting is a technique that helps implement vectorization on arrays of different dimensions.

Difference Between Vectorisation and Broadcasting

In this article, we will study the difference between Vectorisation and Broadcasting.

What is Vectorization?

Vectorization is a technique used in Python on arrays to implement functions on arrays without using for loops. It is implemented using the numpy module and uses pre-defined functions. These functions are optimized and reduce the calculation time. Numpy is a C implementation of arrays in Python, and thus, vectorized functions implemented using numpy are faster.

Let us see some examples of vectorization as follows. First, let us see a simple Python program to find a new array with its elements as the sum of two elements at the same index of two arrays.

  • Python

Python

import numpy as npn
arr1=npn.arange(1, 30, 4)
arr2=npn.array([2,4,6,8,10,12,14,16])
print("Elements of arr1: ", arr1)
print("Elements of arr2: ", arr2)
def myfunc(a1, a2):
   return a1+a2
vectorfunction=npn.vectorize(myfunc)
print("Sum of the elements of array1 and array2 are: \n", vectorfunction(arr1, arr2))
You can also try this code with Online Python Compiler
Run Code

 

OUTPUT

vectorization output

Here, we use the .arange() function to generate an array with a maximum value of 30, a common difference of 4, and starting with 1. Next, we develop a 1D array using the .array() function. Then, we create a function named .myfunc() to return the sum of two passed parameters. We use the .vectorize() function to store the functionalities of the .myfunc() function in the vectorfunction. And finally, we print it.

Now, let us see an example that will clarify that vectorization reduces the time taken to complete the computations.

  • Python

Python

import numpy as npn
import time
arr=npn.arange(1,40,4)
print("Elements of the array arr: \n", arr)
st_time=time.time()
sum=0
for i in arr:
   sum=sum+i
e_time=time.time()
print("Sum of the elements of the array is: \n", sum)
print("Time taken using loop is: \n", e_time-st_time, "seconds")


s_time=time.time()
arr2=npn.sum(arr)
end_time=time.time()
print("Sum of the elements of the array is: \n", arr2)
print("Time taken using vectorization is: \n", end_time-s_time, "seconds")
You can also try this code with Online Python Compiler
Run Code

 

OUTPUT

time taken by vectorization and for loop

Here, we use the time module to find the time at that point of time in the code. We take an array and find its sum using two methods. We can see that the sum using for loop takes more time than the vectorization method. Thus, we use the vectorization methods.

What is Broadcasting?

Broadcasting is a technique that deals with arrays of different dimensions. Along with some constraints, the smaller array is broadcasted to the larger one to have compatible shapes. It provides a means for vectorization of arrays.

However, in some cases, broadcasting is not advised since it takes up a lot of memory space. It can also slow down the computation.

Let us see an example of it below:

  • Python

Python

import numpy as npn
arr1=npn.array(([1,9,8]))
arr2=npn.array([[1], [4], [7]])
print("Array1 elements: ", arr1)
print("Array2 elements: ", arr2)
npn.broadcast(arr1, arr2)
print("Sum by broadcasting \n", arr1+arr2)
You can also try this code with Online Python Compiler
Run Code

 

OUTPUT

broadcasting output

Here, arr1 is created using the .array() function of dimension 1 row x 3 columns. Also, arr2 is created of size 3 rows x 1 column. Finally, these two arrays undergo Broadcasting using the .broadcast() function and are added.

Difference between Vectorisation and Broadcasting

Now that we have discussed both operations separately, let us note some points displaying the difference between Vectorisation and Broadcasting.

S.No Vectorization Broadcasting
1 It performs operations on arrays without using for loops. It uses in-built functions and operators like +, -, etc., to operate on arrays. It helps in doing arithmetic operations on arrays of different dimensions.
2 It increases the speed and decreases the computation time of the Python program. Arithmetic operations are performed on arrays of different shapes, taking longer than vectorizations
3 It uses np.vectorize() function. It uses np.broadcast() function.
4 It can be done on a single array. It requires arrays of different shapes. The smaller array is broadcasted to the enormous array to make them of the same form.

Frequently Asked Questions

What is Vectorization?

Vectorization is a technique used in Python on arrays to implement functions on arrays without using for loops. It is implemented using the numpy module and uses pre-defined functions. These functions are optimized and reduce the calculation time.

What is Broadcasting?

Broadcasting is a technique that deals with arrays of different dimensions. Along with some constraints, the smaller array is broadcasted to the larger one to have compatible shapes. It provides a means for vectorization of arrays.

What is the difference between Vectorisation and Broadcasting in Python?

The critical difference between Vectorisation and Broadcasting is that vectorization is applied on arrays to reduce their computational complexity than using for loops. While Broadcasting is used to do arithmetic operations on arrays of different dimensions.

Conclusion

In Python, arrays have a vast range of operations and uses. Two essential techniques used for performing different operations on arrays are vectorization and Broadcasting. In this article, we studied the difference between Vectorisation and Broadcasting in Python. We started with their definitions and implementations and finally noted their differences.

If you wish to dive deep into this topic, do read the following articles:-

 

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Codestudio. Also, you can enroll in our courses and check out the mock test and problems available. Please check out our interview experiences and interview bundle for placement preparations. 

Happy Coding!

Live masterclass