Introduction to Vectors for Data Science

Vishal Kumar
5 min readApr 9, 2022

--

Vectors in Data Science tell the properties of a data point in different dimensions. Different components of a data point forms a vector each component is related to one dimension.

Point/Vector P in 2-D space

In the above image point P is a vector in 2 Dimension space with x1 and x2 component or (x,y component).

(0,0) represents the origin.

Point/Vector Q in 3-D space

In the above image point Q is a vector in 3-D space with x1,x2,x3 components. A mosquito at one position in a square room is like a data point in 3-D space 😄.

Similarly we can have N-Diminsion in a vector but it’s hard to plot N-D vector on a 2D surface. A vector in N-D will look like this V = [x1,x2,x3,……,xN]

Distance of a point from origin:

Let’s see how we can calculate distance of a point from origin in a space.

Distance of a point from origin in 2D, 3D and N-D space

In the above image we have computed distance of three points from origin, point P in 2D, point Q in 3D and point X in N-D. We can use simple Pythagoras theorem to compute the distance a point from origin.

Distance between two points:

Let’s see how we can calculate distance between two points in a space.

Distance between two points in 2D space

In the above image, we are calculating the distance “d” between two points “P” and “Q”. Calculation of distance between two points in a space is similar to calculating the distance of a point from origin. You can consider in the above example if let, point Q is origin then the co-ordinates of point Q would be (0,0) and the same formula will get converted to the previous formula we used to find distance of point from origin.

Distance between two points in 3D and N-D space

Types of Vector Representation:

There are two types of vector representation,

Row Vector:

A row vector has one row and n columns.

Row Vector Representation

Column Vector:

A column vector has one column and n rows.

Column Vector Representation

Addition of two vector:

Addition of two vectors A & B

In the above image we can see how to add two vectors.

Multiplication of two vector:

There are two type of multiplication we can perform on vectors, Dot Product and Cross Product, For Data Science related study Cross Product is not used frequently so we will focus on Dot Product.

Transpose:

Before performing the dot product on two vectors perform transpose operation on one of the vector iff both the vectors are of same representation e.g. both the vectors are row vectors. Transpose of a vector converts the row vector to column vector and column vector to row vector.

Transpose of vector A

In the above image vector A^T is transpose of vector A.

Dot Product:

We represent the dot product of two vectors by putting a dot between the vectors e.g. (A . B).

Note: For performing the dot product between two vectors number of column in vector 1 and number of row in vector 2 should be same. Which means dimension of both the vectors should be same. Before performing the dot product perform the transpose operation on one of the vector iff both the vectors are of same representation e.g. both the vectors are row vectors.

Dot Product of vector A and B

Geometric Intuition Behind Dot Product:

Now since we have learned what is dot product and how to compute it, let’s see what is the geometric intuition behind it so that we can connect the dots.

A . B = ||A|| ||B|| Cos θ

Above equation also calculate the dot product of two vectors A and B this equation can be used to calculate the angle between two vectors. Here ||A|| represents the length of the vector A and θ represents the angle between vector A and B.

Now let’s see how to calculate the angle between two vectors.

Angle between two vectors A and B

In the above image we can see how easily we can compute the angle between two vectors. Now let’s look at one interesting case.

What if the dot product between two vectors is zero?

Vectors Perpendicular to Each Other

In the above image we can see that if the dot product of two vectors is zero, both the vectors are perpendicular to each other.

Projection of Vector:

Projection of one vector on another vector is like throwing light on one vector and projecting it’s shadow on another vector. Let’s see how to get projection of one vector on another vector.

Projection of Vector A on B

In the above image AB is the projection of vector A on vector B. Just assume that a light is coming from the bulb above vector A and the shadow of A is getting projected on B.

Unit Vector:

A unit vector is represented by hat on top of vector. It represents the single unit of a vector.

A unit vector always has the same direction as the vector.

Length of the unit vector is 1, ||A^|| = 1

Unit Vector of Vector A

--

--

Vishal Kumar

Data Scientist, Data Science Enthusiast working on NLP, Knowledge Graphs and deep learning.