Vectorization in Python

Vaishali
2 min readApr 4, 2020

--

In this article, we learn about Vectorization in Python.

VECTORIZATION

  • 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.

Example:

  • 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.
DOT PRODUCT

OUTER PRODUCT

  • 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.

Example:

  • 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.
OUTER PRODUCT

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.

Example:

  • 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.
ELEMENT WISE PRODUCT

If you are new to Data Science start with the following must-watch video:-

--

--

No responses yet