Vectorization in Python
In this article, we learn about Vectorization in Python.
- Vectorization is a technique of executing operations on entire arrays without using a loop.
- Vectorization helps to speed up the Python code.
- There are various operations are being performed over vector instead of arrays such as Dot Product, Outer Product, Element wise Product.
DOT PRODUCT (INNER PRODUCT)
- The dot product of vectors which is also known as the scalar product as it produces a single output
- The dot product is an algebraic operation in which two equal length vectors are being multiplied and it produces a single number.
- Let’s consider two matrix a and b of the same length, the dot product is done by taking the transpose of the first matrix and then mathematical matrix multiplication of a’(transpose of a) and b is followed as shown in the figure below.
- The tensor product of two coordinate vectors is termed as Outer Product.
- The outer products which result in a square matrix of dimension equal to (length X length) of the vectors.
- Let’s consider two vectors a and b with dimension n x 1 and m x 1 then the outer product of the vector results in a rectangular matrix of n x m.
- If two vectors have the same dimension then the resultant matrix will be square as shown in the figure.
ELEMENT WISE PRODUCT
- Element wise multiplication of two matrices is the algebraic operation in which each element of the first matrix is multiplied by its corresponding element in the later matrix.
- The dimension of the matrices should be the same.
- In the below example, consider two matrices a and b, index of an element in a is i and j then a(i, j) is multiplied with b(i, j) respectively as shown in the figure below.
If you are new to Data Science start with the following must-watch video:-