How Vectors are Used in Machine Learning

# How Vectors are Used in Machine Learning

clickHere
Vikram Singh
Assistant Manager - Content
Updated on Aug 29, 2023 14:04 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.

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.

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

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

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.

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.

### 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

### ParallelVectors

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

### EqualVectors

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.

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

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.

• If a = a1i + b1j + c1k, b = a2i + b2j + c2k, then,
• a+b = (a1+a2)i + (b1+b2) j + (c1+c2) k
• Commutative law, i.e., a + b = b + a
• Associative law, i.e., a + (b + c) = (a + b) + c

### Subtractionof 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 Multiplicationof 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:

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

Top Trending Article

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.