What is the difference between Vectorisation and Broadcasting?
Python vectorization and broadcasting are the two important Python techniques that are performed on arrays. Broadcasting helps to implement vectorizations on arrays of different shapes. Both are used to implement arrays.
Let's look at how vectorization differs from broadcasting:
| Vectorisation | Broadcasting |
|---|---|
| Vectorization is a technique to perform operations on arrays without using loops. It uses inbuilt precompiled functions and operators like " +", "-", etc. to perform operations on arrays. | With the help of broadcasting, we can perform arithmetic operations on arrays of different shapes. |
| Vectorization is used to increase the speed of the python program. | As arithmetic operations are performed on arrays of the same shape in an element-by-element manner. To perform arithmetic operations on arrays of different shapes, broadcasting is used. |
| np.vectorize() function is used | np.broadcast() function is used |
| Vectorization makes use of built-in functions such as np.sum and np.dot. | In the broadcasting dimension smaller of the array is broadcast to a larger dimension of the array to make them of the same shape. |
| Vectorization can be done within a single array. | Broadcasting is done between arrays of different dimensions. |
There are some conditions for broadcasting:
Condition 1: Arrays are of the same dimensions. Condition 2: One of the arrays should be a one-dimensional (or 1-D array). If condition 2 is satisfied, then stretch the lower dimension of the array to a higher dimension and perform arithmetic operations.
Example:
Array A:
| 1 | 2 | 3 |
|---|
Array B:
| 4 |
|---|
| 5 |
| ------ |
| 6 |
| ------ |
After broadcast, array A becomes:
| 1 | 2 | 3 |
|---|---|---|
| 1 | 2 | 3 |
| ------ | --- | ------ |
| 1 | 2 | 3 |
| ------ | --- | ------ |
After the broadcast, array B becomes:
| 4 | 4 | 4 |
|---|---|---|
| 5 | 5 | 5 |
| ------ | --- | ------ |
| 6 | 6 | 6 |
| ------ | --- | ------ |
Final result:
| 5 | 6 | 7 |
|---|---|---|
| 6 | 7 | 8 |
| ------ | --- | ------ |
| 7 | 8 | 9 |
| ------ | --- | ------ |
Now let's understand all the above points with the help of examples:
Vectorization Examples
Remember: All in-built functions or operators are performed in an element-by-element manner on arrays.
Code 1:
Output:
Explanation:
In the above example, array arr is created using the np.arange function, and array arr1 is created using the simple np.array method. Then using the "+" operator of Python for arithmetic operations, addition between the elements of two arrays is performed, and the final result is stored in the arr2 variable. By printing arr2, we get the sum of arrays as output.
Code 2:
Output:
Explanation:
The array arr is created with the np.arange function, and the sum of the array's elements is found with the np.sum function and stored in arr2.
Code 3:
Output:
Explanation:
Array arr is created using the np.arange function and arr1 using np.array. A function func having two arguements is defined, which stores the sum of parameters. Using the np.vectorize function, the functionalities of func are stored in the vecfunc variable. Now we only have to give parameters inside the vecfunc and get our desired result.
Code 4:
Output:
Explanation:
In this example, the array arr is created using the np.arange function, and the array is output using the print statement. Using for loop, i as the iterable sum of elements of an array is calculated and the updated sum is stored in the Sum variable, which begins from 0. st_time1 and ed_time1 are two variables in which we store starting time and ending time of the program, by subtracting these variables we get the execution time of the program.
Using the np.sum method, elements of the array are added, and the final result is stored in the arr2 variable. st_time2 and ed_time2 are the two variables here for calculating the start time and ending time of the program, and their subtraction gives the execution time of the program. Here, we can see that execution time is reduced in the case of vectorization than in loops. That's why vectorization is used.
Broadcasting Examples
Code:
Output:
Explanation:
In the above example, array arr of shape (1x3), i.e., 1 row and 3 columns, is created using the np.arange function, and array arr1 is created using the np.array method of shape (3x1), i.e., 3 rows and 1 column. Then two arrays are automatically broadcast and added using the "+" operator in an element-by-element manner.
Code:
Output:
Explanation:
Array arr of shape (1x3), i.e. 1 row and 3 columns, is created using the np.arange function, and arr1 of shape (3x1) is created using the simple np.array method. Using the np.broadcast function of the Numpy library, two arrays of different shapes are broadcast and added together using the "+" operator in an element-by-element manner.
Code:
Output:
Explanation:
In this example, the np.arange function is used to create array arr of shape (3x3), i.e., 3 rows and 3 columns, and the simple np.array method is used to create arr1 of shape (3x1). np.broadcast function, arrays are broadcast into a similar shape and we get the product of the elements of the arrays as output.
Conclusion
- Vectorization is used to perform operations on arrays without using loops. It is faster than loops.
- With the help of broadcasting techniques, arithmetic operations can be performed on arrays of different shapes and sizes.
- The numpy.vectorize and numpy.broadcast functions are used for vectorization and broadcasting, respectively.