what is Vectorization in Python? Comparing the result of Vectorized and Non vectorized operation using Magic function?

Kiranjeet kaur
4 min readDec 9, 2022

--

What is Vectorization ?

Modern computers can do many tasks simultaneously especially, but not only tasks involving mathematics. Pandas and the underlying NumPy support A number of methods for computation And vectorization in particular works with most of the functions in the NumPy library

This is the process of applying mathematics operation on arrays directly (internally NumPy converts to vectors and apply results on all elements in one step parallelly ) instead of iterating every element one by one using for loops to apply operation such as sum, average , multiplication etc.

“Vectorization” is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.

So , Vectorization used for converting a scalar program to a vector program. Vectorized programs can run multiple operations from a single instruction, whereas scalar can only operate on pairs of operands at once.

Why we use Vectorization ?

So from definition of Vectorization we know that it will speedup the process of program by running simultaneously operation on each element of array . Modern days we perform operation on large datasets which required to perform the same in fast and efficient manner . So if we use the for loops to perform such operations on large datasets that will degrade the performance . We need way to speedup our program run time using modern techniques like Vectorizations .

Vectorization is used greatly in scientific computing where huge chunks of data needs to be processed efficiently.

Vectorized advantages:

  1. Code is more concise and easier to read
  2. lesser line of code is easy to debug if any issues , Insead of large complex code .
  3. Very fast for large dataset operations
  4. vectorization results in more “Pythonic” code. Without vectorization, our code would be littered with inefficient and difficult to read for loops.

Lets take an example of calculating Average :

How we will check which method is faster ? The Jupyter notebook has a magic function which can help. The IPython interpreter has something called magic functions that begin with a percentage sign. So here we’re actually going to use cellular magic function. These start with two percentage signs, an wrap the code in the current Jupyter cell. The function we’re going to use is called time it. This function will run our code a few times to determine on average how long it takes.

So first let’s create a big series of random numbers, and this is actually used a lot when demonstrating techniques with pandas, so you should get used to seeing this.

Creating Pandas Series

Lets calculate Average without vectorization with Magic Function:

Calculating Average without Vectorization

Now let’s see the same Average calculation using Vectorization .

So we can see there is Significant increase in speed using Vectorization for Same operation .

A related feature in pandas and NumPy is called broadcasting. With broadcasting, we can apply an operation to every value in the series, changing the series. Broadcasting is another extension to vectorization where arrays need not be of the same sizes for operations to be performed on them like addition, subtraction, multiplication, etc.

For instance, if we wanted to increase every random variable by two, we could do so quickly using the plus equals operator directly on the series object.

Note: If this is completely new to you, I will suggest to check below Links for more understanding .

https://www.geeksforgeeks.org/vectorization-in-python/#:~:text=What%20is%20Vectorization%20%3F,running%20time%20of%20code%20efficiently.

https://www.askpython.com/python-modules/numpy/vectorization-numpy

Thank You for Reading !! You can also Subscribe to my New Articles.

In case of questions/comments, do not hesitate to write in the comments below

--

--

Kiranjeet kaur
Kiranjeet kaur

Written by Kiranjeet kaur

#Artificial Intelligence #GirlWhoCodes #SpaceLover #Data Scientist At Accenture

No responses yet