The time complexity of any algorithm is very important in deciding whether an application is reliable or not. Running a large algorithm as quickly as possible is very important when it comes to applying real-time output. To do this, Python has several standard math functions for quickly performing operations on entire arrays of data without having to write loops. One such library that contains such a function is numpy . Let`s see how we can use this standard function in the case of vectorization.
What is vectorization?
Vectorization is used to speed up Python code without using a loop. Using such a function can help minimize your code execution time. Various operations are performed on a vector, such as the dot product of vectors, which is also known as the dot product, because it produces a single output, outer products that result in a square measurement matrix. equal to the length X of the length of vectors, multiplication by an element, which produces the product The element of the indices and the dimension of the matrix remain unchanged.
We will see how classical methods take longer than ordinary functions, calculating their processing time.
outer (a, b): Compute the outer product of two vectors.
multiply (a, b) : Matrix product of two arrays.
dot (a, b): Dot product of two arrays.
zeros ((n, m)) : Return a matrix of given shape and type, filled with zeros.
process_time (): Return the value (in fractional seconds) of the sum of the system and user CPU time of the c urrent process. It does not include time elapsed during sleep.
Dot product — it is an algebraic operation in which two vectors of equal length are multiplied so that it produces one number. The Dot Product is often referred to as a Inner Product . This product results in a scalar number. Let`s consider two matrices a and b of the same length, the dot product is performed by transposing the first matrix, and then is performed the multiplication of the mathematical matrix by a & # 39; (transpose a) and b, as shown in the image below.
Visual representation of the dot product —
Below here is the Python code:
dot_product = 833323333350000.0 Computation time = 35.59449199999999ms n_dot_product = 833323333350000 Computation time = 0.1559900000000225ms
Tensor product of two coordinate vectors is called
nx 1 and
mx 1 then the outer product of the vector leads to a rectangular matrix
nxm . If two vectors have the same dimension, then the resulting matrix will be a square matrix, as shown in the figure.
Visual representation of the external product —
Below is the Python code:
outer_product = [[0. 0. 0. ..., 0. 0. 0.] [200.201.202. ..., 397. 398. 399.] [400. 402. 404. ..., 794. 796. 798.] ..., [39400. 39597. 39794. ..., 78209. 78406. 78603. ] [39600. 39798. 39996. ..., 78606. 78804. 79002.] [39800. 39999. 40198. ..., 79202. 79401.]] Computation time = 39.821617ms outer_product = [[0 0 0 ..., 0 0 0] [200 201 202 ..., 397 398 399] [400 402 404 ..., 794 79 6 798] ..., [39400 39597 39794 ..., 78209 78406 78603] [39600 39798 39996 ..., 78606 78804 79002] [39800 39999 40198 ..., 79003 79202 79401]] Computation time = 0.2809480000000031ms pre>
Element wise product:
Elementwise multiplication of two matrices — it is an algebraic operation in which each element of the first matrix is multiplied by the corresponding element in the later matrix. The dimensions of the matrices must be the same.
Consider two matrices a and b , the element index in a — these are i and j, then a (i, j) is multiplied by b (i, j) , respectively, as shown in the picture below.
Visual representation of the wise product Element —
Below is the Python code:
Element wise Product = [0.00000000e + 00 5.00010000e + 04 1.00004000e + 05 ..., 4.99955001e + 09 4.99970000e + 09 4.99985000e + 09] Computation time = 23.516678000000013ms Element wise Product = [0 50001 100004 ... , 704582713 704732708 704882705] Computation time = 0.2250640000000248ms