In this article, we will learn about vectorization and various techniques involved in implementation using Python 3.x. Or earlier.
What is Vectorization?
Vectorization is a technique to implement arrays without the use of loops. Using a function instead can help in minimizing the running time and execution time of code efficiently. Various operations are being performed over vector instead of arrays such as dot product of vectors which is also known as scalar product as it produces single output, outer products which results in square matrix of dimension equal to (length X length) of the vectors, Element wise multiplication which products the element of same indexes and dimension of the matrix remain unchanged.
Dot product / Inner product
Let’s see the implementation
Example
import time import numpy import array p = array.array('q') for i in range(100000,200000): p.append(i); q = array.array('q') for i in range(200000, 300000): q.append(i) # classic dot product tic = time.process_time() dot_value = 0.0; for i in range(len(a)): dot_value += p[i] * q[i] toc = time.process_time() print("dot_product of vector arrays = "+ str(dot_value)); print("Computation time taken = " + str(1000*(toc - tic )) + "ms") n_tic = time.process_time() n_dot_product = numpy.dot(a, b) n_toc = time.process_time() print("\nn_dot_product of vector arrays = "+str(n_dot_product)) print("Computation time taken= "+str(1000*(n_toc - n_tic))+"ms")
Output
dot_product of vector arrays = 3833313333350000.0 Computation time taken = 116.51723400000068ms n_dot_product of vector arrays = 3833313333350000 Computation time taken= 2.5412239999997865ms
Now let's discuss the functions used abouve in some detail
outer(a, b) − This function takes two numpy arrays as input variables and returns the outer product of two vectors.
multiply(a, b) − This function takes two numpy arrays as input variables and return the matrix product of two arrays.
dot(a, b) − This function takes two numpy arrays as input variables and returns the dot product of two arrays.
zeros((n, m)) − This function takes shape & type as input variables and return a matrix of given shape and type, initilialized with zeros.
process_time() − This function returns the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep
Conclusion
In this article, we learnt about the vectorization in Python.