Open In App

Vectors for ML

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Vectors are a basic notion in machine learning and are very significant in data representation as well as data processing. They are applied in most machine learning algorithms that are used in classification, regression, clustering and deep learning. By definition, a vector is a mathematical concept with magnitude and direction. In machine learning, it is employed to encode numerical data so that computers can easily process and analyze the information.

What is a Vector?

A vector is a list of numbers in order, commonly referred to as components or elements. They are different features of points of data expressed in an ordered form. A vector in a two-dimensional plane can be defined as:

\nu= (x1, x2)

For example- a vector for someone's height (in cm) and weight (in kg) might be:

\nu= (170, 65)

In higher dimensions, vectors will have more components, such as:

\nu= (x1, x2, x3, ..........., xn)

where n is the number of features of a specific data point.

Code to Create and Print a Vector in NumPy:

Python
import numpy as np

vector = np.array([170, 65])

print("Vector:", vector)

Output
Vector: [170  65]

Scalars, Vectors and Matrices

  • Scalars: Any single value from our dataset would represent a scalar like integers or floating-point numbers, employed in mathematical computation.
  • Vectors: Vectors are one-dimensional number arrays that hold several values in a linear sequence.
  • Matrices: Matrices are two-dimensional arrays of multiple vectors that are placed in rows and columns.

These mathematical structures play an essential role in machine learning models, facilitating effective calculations and data representation.

Python
import numpy as np

# Define a 3x3 matrix
mat = np.array([[1, 2, 3], 
                   [4, 5, 6], 
                   [7, 8, 9]])

print(mat)

Output
Matrix:
[[1 2 3]
 [4 5 6]
 [7 8 9]]

Vectors in Machine Learning Models

Vectors are used at various points in machine learning models:

1. Input: Machines only process data numerically. The inputs like images, text or sensor inputs must be converted into numbers and represented as vectors. Models are able to efficiently analyze data by this organized representation. Feature engineering methods assist in converting raw data into useful vector representations for training models.

2. Model: The heart of machine learning is creating models that take in input data and learn to make predictions. In deep learning models, this is done through a neural network wherein the layers of the neural network employ linear algebra (such as matrix and vector multiplication) in order to update your parameters. Vectors can be used for operations like scaling, rotation and reducing dimensions, which allows computations to be efficient and increase model accuracy.

3. Outputs: The results of machine learning models can take various forms, such as numbers, images or even vectors. f we’re classifying images, the output will be a category of image. For instance, in natural language processing (NLP), a vector may represent the final output of a model and serve as input for another task, such as classification or recommendation systems. Vector operations such as similarity calculations, clustering and projections are used to interpret and refine model outputs.

Types of Vectors in Machine Learning

1. Row and Column Vectors

  • A row vector is a one-dimensional array represented in a row format: (x1, x2, x3)
  • A column vector is a one-dimensional array represented in a column notation:

\begin{bmatrix}x_1 \\x_2 \\x_3 \\\vdots \\x_n\end{bmatrix}

2. Zero Vector

A vector with all elements as zero. Example:

\nu= (0, 0, 0)

Zero vectors are useful when solving optimization problems and are the origin in vector space.

3. Unit Vector

A vector of magnitude 1. It is frequently used to denote direction:

\mathbf{u} = \frac{\mathbf{v}}{\|\mathbf{v}\|}

where \|\nu\| is the magnitude of vector v.

4. Sparse and Dense Vectors

  • Sparse Vectors consist primarily of zeros and are employed in text analysis and recommendation systems.
  • Dense Vectors consist primarily of non-zero values and are employed in image processing and deep learning.

Importance of Vectors in Machine Learning

1. Feature Representation

Vectors are used to represent data points in numerical form. For example, in natural language processing (NLP), words are translated into word vectors by techniques such as Word2Vec or TF-IDF.

2. Distance and Similarity Measures

Similarity between data points is typically calculated in machine learning by vector distance measures like:

3. Transformations and Projections

Vectors enable mathematical transformations such as rotation, scaling and translation. These are employed in methods such as Principal Component Analysis (PCA) to project datasets into lower dimensions.

Vector Operations in Machine Learning

1. Vector Addition and Subtraction

Python
import numpy as np

a = np.array([2, 3])
b = np.array([1, 4])

# Perform addition
add = a + b

# Perform subtraction
sub = a - b

print("Addition:", add)
print("Subtraction:", sub)

Output
Addition: [3 7]
Subtraction: [ 1 -1]

2. Scalar Multiplication

Python
import numpy as np

a = np.array([1, 2, 3])

scalar = 3

# Perform scalar multiplication
res = scalar * a

print("Scalar Multiplication:", res)

Output
Scalar Multiplication: [3 6 9]

3. Dot Product

Python
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Compute the dot product
prod = np.dot(a, b)

print("Dot Product:", prod)

Output
Dot Product: 32

4. Cross Product

Python
import numpy as np

c = np.array([1, 2, 3])
d = np.array([4, 5, 6])

# Compute the cross product
prod = np.cross(c, d)

print("Cross Product:", prod)

Output
Cross Product: [-3  6 -3]

Application of Vectors in Machine Learning

Vectors play a crucial role in various machine learning algorithms and natural language processing (NLP) techniques.

  • Linear Regression- Linear regression employs vectors to denote the independent and dependent variable relationship: Y = Xw + b where X is a feature vector, w is a weights vector and b is the bias term.
  • Support Vector Machines (SVMs)- SVMs utilize vector mathematics to identify the best hyperplane separating various classes in classification problems.
  • Neural Networks- Neural networks store weights, activations and gradients as vectors, making them crucial for deep learning models.
  • Clustering (K-Means Algorithm)- The K-Means algorithm allocates points to clusters based on vector distances.
  • Word Embeddings in NLP- Methods such as Word2Vec and GloVe map words to vectors, capturing their semantic meaning.
  • Sentence and Document Embeddings- Sentences and documents can also be encoded as vectors using techniques like BERT and Doc2Vec

Article Tags :
Practice Tags :

Similar Reads