MITB Banner

Complete Guide To Vectors in Linear Algebra With Implementation in Python

I will take you to a journey while discussing a really basic yet an important aspect of mathematics that is used in Machine Learning, vectors.

Share

vectors

The study of Mathematics has come a long way. From the discovery of zero to solving complex integral equations which somewhere surely has impacted the lives of many. Well, not many realise it but if maths had not been there, we wouldn’t have been able to sit in front of our laptops, these beautiful architectural monuments we see today wouldn’t have existed, we would never have been able to reach the moon or discover more about the black hole etc.

Realising the importance of maths and how it is valued among the researchers of tech giants for the purpose of Machine Learning or Computation, I will take you to a journey while discussing a really basic yet an important aspect of mathematics that is used in Machine Learning, vectors.

Overview

  1. Physical Quantities.
  2. Vector and Scalars
  3. Representation of vectors.
  4. Types of Vectors
  5. Operations on Vectors
  6. Section Formula
  7. Concept of Euclidean Distance between two vectors.
  8. Where are vectors used in Machine Learning

What are Physical Quantities

Any quantity that can be measured in terms of numbers is called a physical quantity. Measurements in terms of size, mass etc. Let us understand it better with an example, your mom has asked you to buy 2 Kilograms of tomatoes, you convey the amount you want to buy to the vendor. He “measures” the mass and then gives you the desired quantity of tomatoes. Here since the mass can be measured in terms of numbers, it will be called a physical quantity. 

Let’s delve a little more into it. 

Physical quantities can be categorized into 2 categories: Scalars and Vectors.

Scalars and Vectors

Concisely, Scalars can be understood as the quantities which do not vary with direction example: mass, time, length etc. whereas vector quantities are the ones which do vary with direction example: force, velocity, momentum etc.

So if we talk about 1 meter of cloth, it will stay the same no matter how to see and from where you see it. It is independent of direction, place it is seen from. But this is not the case when it comes to the term “force”, here in the image below we see the value of force changes when we vary direction where it is exerted from. The same is for velocity,  acceleration etc. And this is exactly why they are called vector quantities.

Source: study.com

Representation of a vector

A vector is denoted by an arrow (→).

The direction where the vector is pointing is called the vector’s direction. The length of the vector describes the magnitude of the vector.

Vector Notation/forms:

Source: Math is fun

Let’s say the magnitude of the vector is denoted by r and the angle at which it is inclined is theta.

Then,

Source: math is fun

There is a correlation between all the values,

X = rcosθ
Y = rsinθ
r = √ ( x^2 + y^2 )
θ = tan^-1 ( y / x )

Types of vectors

  1. Zero Vector or NULL Vector: A vector with zero magnitudes is called a NULL vector. The tail and the head of this vector are the same. Denoted by vector O.
vectors

Source: qsstudy.com

  1. Unit Vector: As the name suggests, a unit vector is the one with the value of its magnitude equalling to one. Denoted by .

 =r/|r|

Source: Wikipedia

Here i,j,k are three unit vectors in the direction of x,y,z respectively.

  1. Collinear Vector: Two vectors are said to be collinear if they are either parallel to each other or lie on the same line irrespective of their direction.

Source: quora

  1. Coplanar vector: A set of vectors is called coplanar if they all lie on the same plane.
vectors

Source: Quora

Note: Collinear vectors will always be coplanar, but coplanar vectors may or may not be collinear. 

  1. Equal vector: a set of vectors, is called equal if they have the same magnitude and sense.
  1. Position vector: It can be understood as a fixed vector which is located in a point in the geometrical space with respect to another point, usually the origin.

These are some basic terminologies that one can understand, effortlessly. 

Now just like numbers, vectors have operations too. You guessed it right, arithmetic operations. 

Operations on Vectors

  1. Vector addition

CODE 

import numpy as np #pip install numpy
a=np.array([1,2,3])
b=np.array([4,5,3])
print(a+b)
Output: [5,7,6]

Let us say A = 1i+2j+3k and B = 4i+5j+3k

So A+B would be (1+4)i+(2+5)j+(3+3)k = 5i+7j+6k

Another pictorial representation of the same can be seen below.

vectors

Source: mathinsight.org

  1. Vector Subtraction

CODE

import numpy as np
a=np.array([3,5])
b=np.array([4,-2])
print(a-b)
Output: [-1,7]

Let us take A = 3i+5j and B = 4i-2j

So A-B= (3-4)i+(5-(-2))j = -i+7j

  1. Multiplication of scalars to vectors:

CODE

import numpy as np
a=np.array([3,5])
print(3*a)
Output: [9, 15]
  1. 3*a=a*3
  2. x(a+b)=xa+xb
  3. x(a+b)=(a+b)x

A=4i-3j, So 4A=4(4)i-4(3)j = 16i - 12j

Section Formula

If vector a and b are two unique position vectors of two different point say, A and B, then the position vector of a point which segments AB in a real ration of m:n is given by:

r = (m*b+n*a)/(m+n), here r,a,b are position vectors.

For external segmentation, the formula the same just one needs to replace “+” with “-”.

Let us understand it better with an example:

Ex: Find a position vector R which divides the line joining two points P and Q whose position vectors are (4 î - 2 ĵ + 3 k̂)  and (-2î+ 5ĵ+ 3k̂) respectively, in the ratio 2:1

  1. Internally
  2. Externally
  1. Internally

OP = 4 î - 2 ĵ + 3 k̂

OQ = -2î+ 5ĵ+ 3k̂

m=2

n=1

OR = 2(-2î+ 5ĵ+ 3k̂) + 1(4 î - 2 ĵ + 3 k̂)/2 + 1

OR = (-4î+ 10ĵ+ 6k̂ + 4 î - 2 ĵ + 3 k̂)/3

((-4+4)i + (10-2)j + (6+3)k)/3

OR = (8j +9k)/3

When segmented internally OR would be (8j +9k)/3.

  1. Externally

OP = 4 î - 2 ĵ + 3 k̂

OQ = -2î+ 5ĵ+ 3k̂

m=2

n=1

OR = 2(-2î+ 5ĵ+ 3k̂) - 1(4 î - 2 ĵ + 3 k̂)/2 - 1

(-4î+ 10ĵ+ 6k̂ - 4 î + 2 ĵ - 3 k̂)/1

((-4-4)i + (10+2)j + (6-3)k)

-8i + 12j + 3k

When segmented externally OR would be -8i + 12j + 3k.

Concept of Euclidean Distance

What is it exactly?

Euclidean distance is the distance that is measured between two points, here vectors.

The formula for Euclidean Distance:

vectors

Source: Predictive Hacks

Let’s understand it better with an example:

CODE

import NumPy as np #pip install numpy
a=np.array([4,6])
b=np.array([7,-2])
print(np.linalg.norm(a-b))
Output: 8.0622577

A = 4i + 6j

B = 7i - 2j

So the distance between A and B would be:

d(A,B) = √ (4-7)² + (6-(-2))² = √9+64 = √73

Therefore, the distance between vector A and B is √73.

Where is a vector used in Machine Learning?

Machine Learning comprises many decision-making algorithms which help us predict the outcome in a situation. Of these many, one specific algorithm which uses vectors is Support Vector Machine, abbreviated as SVM. 

An SVM analyses vectors across the given dimensional space for finding out the optimal hyperplane. The concept of Euclidean distance is often used here to find the distance between the data points and the hyperplane. 

Subscribe to AIM for more such amazing articles and newsletters! 

Share
Picture of Bhavishya Pandit

Bhavishya Pandit

Understanding and building fathomable approaches to problem statements is what I like the most. I love talking about conversations whose main plot is machine learning, computer vision, deep learning, data analysis and visualization. Apart from them, my interest also lies in listening to business podcasts, use cases and reading self help books.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India