How Vectors are Used in Machine Learning

How Vectors are Used in Machine Learning

8 mins read2.9K Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Aug 27, 2024 17:42 IST

Vectors are mathematical objects that contain both magnitude and direction, and they can be represented by the directed line segments (lines having directions) whose lengths are their magnitude. It is used to describe the movement of an object from one point to another. In this article, we will briefly discuss, what is vector, types of vectors, and vector arithmetic.

2023_01_MicrosoftTeams-image-110.jpg

A vector is a data structure with at least two components: magnitude and direction. It is most commonly used in machine learning to represent the data in the most optimized and organized way.

In machine learning, while training any model, if our dataset contains the images and text, they are first converted into numbers and then stored in the form of vectors and matrices to represent these data, i.e., the very first step in building a machine learning model is vectorizing the data. Once the data is vectorized, you can easily use different linear algebra operations (or tools) to perform model training, data augmentation, etc.

One of the most common examples of vectors in machine learning is Support Vector Machines (a supervised machine learning algorithm that uses classification techniques to solve the problem). SVM analyzes the vectors across n-dimensional space to find the optimal hyperplane for a given dataset.

We don’t need a deep mathematical understanding of vectors to use vectors in machine learning. This article will discuss vectors, geometric representation, types of vectors, and the operations that can be performed on these vectors.

Without further delay, let’s dive deep to learn more about vectors.

Table of Content

Recommended online courses

Best-suited Data Science courses for you

Learn Data Science with these high-rated online courses

1.18 L
12 months
80 K
4 months
2.5 L
2 years
90 K
24 months
2.5 L
2 years
Free
4 weeks
1.24 L
48 months

What is a Vector?

Definition

Vectors are mathematical objects that contain both magnitude and direction, and they can be represented by the directed line segments (lines having directions) whose lengths are their magnitude. It is used to describe the movement of an object from one point to another.

2023_01_image.jpg
  • The direction of the vector is from its tail to its head.
  • Notation: The vector between two points (A & B) can be given by:
    • a or A or AB
    • Standard Form: A = ai +bj+ck, where a, b, c are real numbers, and ij, and k are the unit vectors along the x, y, and z-axis.

Example: Velocity of a car.

In simple terms, a car’s velocity means the car’s speed is moving in a particular direction.

It can also be defined as a Tuple of one or more scalar values.

Example: V = (a, b, c); here, a, b, and c are scalars (real values).

Types of Matrix Transpose of Matrix
Symmetric Matrix Skew-Symmetric Matrix

Now, we will discuss how to find the magnitude of the vector.

Magnitude

The magnitude of any vector can be easily calculated by taking the square root of its component, i.e., if 

A = ai + bj + ck, then,

Magnitude of A = |A| = sqrt (a2b2c2)

Example: a = 3i + 4j – 7k. Find the magnitude of a.

Answer:

2023_01_image-1.jpg

Note: In Machine learning, we use different vector norms (or magnitude) to find the distance, which are also called distance matrices:

Types of Vectors

There are different types of vectors, but here we will discuss seven different types of vectors that are commonly used:

Zero Vector

A vector is said to be a zero vector if the magnitude of the vector is zero. 

  • It is denoted by: O = (0, 0, 0). 
  • Also known as Additive Identity
    • i.e., A + O = A = O + A

Unit Vector

A vector is said to be a unit vector if the magnitude of the vector is one.

2023_01_image-2.jpg

Negative Vector

A vector is said to be a negative vector of a given vector if it has the same magnitude but points in the opposite direction.

  • In simple terms, when we multiply any vector with the -1, it changes the direction of the vector.
    • i.e., (-1)v = -v
2023_01_image-3.jpg

Parallel Vectors

Two vectors, a, and b are said to parallel if they have the same direction but not the same magnitude.

2023_01_image-4.jpg

Equal Vectors

Two vectors (a and b) are said to be equal if they have the same magnitude and direction.

  • If a = x1i + y1j + z1k, and b = x2i + y2j + z2k, then
    • a = b if and only if x1x2y1y2, and z1z2
      • i.e., two vectors are equal if their corresponding components are equal.

Orthogonal Vectors

Two vectors, a, and b, are orthogonal if and only if they are perpendicular to each other.

  • The angle between them is a right angle.
  • Mathematically, vectors are orthogonal if the dot product of vectors is zero.
Programming Online Courses and Certification Python Online Courses and Certifications
Data Science Online Courses and Certifications Machine Learning Online Courses and Certifications

Until now, we have discussed what vectors are, types of vectors, and now it’s time to know how to perform arithmetic over these vectors.

Vector Arithmetic

Addition of Vectors

Two vectors, a, and b, are added using the Triangle Law of Addition.

If two vectors, a, and b, are represented as the side of a triangle with the magnitude and direction, then the third side of the triangle (magnitude and direction) will be the resultant vector.

2023_01_image-5.jpg
  • If a = a1i + b1j + c1k, b = a2i + b2j + c2k, then,
    • a+b = (a1+a2)i + (b1+b2) j + (c1+c2) k
  • Vector addition follows: 
    • Commutative law, i.e., a + b = b + a
    • Associative law, i.e., a + (b + c) = (a + b) + c

Subtraction of two Vectors

Vector subtraction is similar to vector addition, i.e., if a and b are two vectors, such that a = a1i + b1j + c1kb = a2i + b2j + c2k, then,

  • a – b = a + (b) = (a1 – a2) i + (b1 – b2) j + (c1 – c2) k

Scalar Multiplication of Vectors

If a = x1y1z1and b = x2i + y2j + z3k are two vectors, and c is any scalar (or constant), then:

  • ca = c (x1y1z1k) = (cx1) i + (cy1) j + (cz1) k
  • c(a+b) = ca + CB
  • (c+I) = ca + cI

Product

Two vectors, a, and b, can be multiplied in two ways:

Dot Product

A dot product (or a scalar product) is a mathematical operation that takes two products and returns a scalar product. It is calculated by multiplying the corresponding elements of the vectors.

  • If a and b are two vectors, a = (a1, a2, a3, …, an), and b = (b1, b2, b3, …, bn), then
    • a · b = (a1b1) + (a2b2) + … + (an * bn)
  • If the angle between two vectors is given, then:
    • a · b =|a| · |b| cos (theta), 

where, 

  • theta is the angle between a and b
  • |a|, and |b| are the magnitude of a and b.

Must Read: Dot Product – All That You Need to Know

Cross Product

Cross product is a binary operation (multiplication) that is performed on two vectors, and the resultant vector is perpendicular to both the given vectors. It is calculated by using the determinants, i.e.,

  • If a = a1i + a2j + a3k, and b = b1i + b2j + b3k, then
    • a x b = (a2b3 – a3b2)i – (a1b3 – a3b1)j + (a1b2 – a2b1)k
  • If the angle between two vectors is given, then:
2023_01_image-6.jpg

where,

|a| is the magnitude of a

|b| is the magnitude of b

theta: angle between a and b

n: unit vector at a right angle to both a and b

Must Read: Cross Product of Two Vectors

Also Read: Matrix Multiplication

Top Trending Article

Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python

Interview Questions

Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |

FAQs

What is a Vector?

Vectors are mathematical objects that contain both magnitude and direction, and they can be represented by the directed line segments (lines having directions) whose lengths are their magnitude. It is used to describe the movement of an object from one point to another.

What are the different types of vectors?

Zero Vector, Unit Vector, Negative Vector, Parallel Vector, Equal Vector, Orthogonal Vectors are some common types of vectors.

What is a dot product?

Vectors are mathematical objects that contain both magnitude and direction, and they can be represented by the directed line segments (lines having directions) whose lengths are their magnitude. It is used to describe the movement of an object from one point to another.

What is a cross product?

Vectors are mathematical objects that contain both magnitude and direction, and they can be represented by the directed line segments (lines having directions) whose lengths are their magnitude. It is used to describe the movement of an object from one point to another.

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio