NumPy vectorization involves performing mathematical operations on entire arrays, eliminating the need to loop through individual elements.
We will see an overview of NumPy vectorization and demonstrate its advantages through examples.
NumPy Vectorization
We've used the concept of vectorization many times in NumPy. It refers to performing element-wise operations on arrays.
Let's take a simple example. When we add a number with a NumPy array, it adds up with each element of the array.
Output
[11 12 13 14 15]
Here, the number 10 adds up with each array element. This is possible because of vectorization.
Without vectorization, performing the operation would require the use of loops.
Example: Numpy Vectorization to Add Two Arrays Together
Output
Sum between two arrays: [[1 3 5] [4 6 8]]
In this example, we have created two 2D arrays array1 and array2, and added them together.
This is a vectorized operation, where corresponding elements of two arrays are added together element-wise.
NumPy Vectorization Vs Python for Loop
Even though NumPy is a Python library, it inherited vectorization from C programming. As C is efficient in terms of speed and memory, NumPy vectorization is also much faster than Python.
Let's compare the time it takes to perform a vectorized operation with that of an equivalent loop-based operation.
Python for loop
Output
For loop time: 4.76837158203125e-06
NumPy Vectorization
Output
Vectorization time: 1.5020370483398438e-05
Here, the difference in execution time between vectorization and a for loop is significant, even for simple operation.
This comparison illustrates the performance benefits of vectorization, especially when working with large datasets.
NumPy Vectorize() Function
In NumPy, every mathematical operation with arrays is automatically vectorized. So we don't always need to use the vectorize() function.
Let's take a scenario. You have an array and a function that returns the square of a positive number.
Now, to apply the function find_square() to array1, we have two options: use a loop or vectorize the operation.
Since loops are complicated and slow by nature, it's efficient and convenient to use vectorize().
Let's see an example.
Output
[ 0 0 4 9 16]
In this example, we used the vectorize() function to vectorize the find_square() function. We then passed array1 as a parameter to the vectorized function.