AIDS Module 2
AIDS Module 2
Introduction:
The mathematical foundations needed for Artificial Intelligence and Machine Learning
(AI/ML) primarily include linear algebra, calculus, probability and statistics, and optimization.
Linear algebra provides the tools to represent and manipulate data using vectors and matrices,
which form the backbone of data processing in machine learning algorithms. Calculus,
particularly differential calculus, is essential for understanding how models learn, especially
during gradient-based optimization used in training neural networks. Probability and statistics
help in modeling uncertainty, making predictions, and analyzing patterns within data.
Optimization techniques are critical for improving model performance by minimizing errors or
loss functions. A strong grasp of these mathematical concepts is important because they enable
practitioners to understand the inner workings of algorithms, design better models, interpret
results correctly, and troubleshoot problems effectively, rather than treating AI systems as black
boxes.
◆ 1. Data Representation
Computers handle all kinds of information—such as images, audio, video, and text—by
converting them into numbers.
Linear algebra gives a clear structure to organize this numerical data using:
Vectors: a list of numbers in a line (1D)
Matrices: rows and columns of numbers (2D)
Tensors: data in more than two dimensions (like colored images or video frames)
• Example:
¸
‘
)
An image is stored as a matrix of pixel brightness values.
A sentence is stored as a vector showing the importance of each word.
◆ 2. Efficient Computation
Modern computers, especially GPUs (Graphics Processing Units), are built to do matrix
and vector calculations very quickly.
Linear algebra makes it possible to handle large amounts of data smoothly and perform
operations like addition and multiplication on that data.
•
)
‘ Example:
¸
Training an AI model (like face detection) involves multiplying large matrices many times.
GPUs do this in seconds.
◆ 3. Transformation of Data
Linear algebra helps change the form or orientation of data using operations like rotation,
scaling, and projection.
These transformations are useful in computer graphics, animations, and when trying to
understand high-dimensional data.
• Example:
¸
‘
)
To rotate a 3D object in a game or a simulation, a matrix is used to apply the change.
Vectors in AI/ML
Vectors are essential building blocks in Artificial Intelligence (AI) and Machine Learning
(ML). They are not just lines with direction and magnitude — they are the language through
which computers understand and manipulate information.
A vector is simply an ordered list of numbers. It can represent various forms of data:`
。
ˆ· Original Image (3×3 pixels):
.ç'
•
[ 0 50 100 ]
[150 200 250 ]
[ 30 60 90 ]
We can flatten this into a vector (1D array):
[0, 50, 100, 150, 200, 250, 30, 60, 90]
˛C* Now this vector can be fed into an ML model to recognize the image (e.g., a number
or a letter).
📄 2. Document as a Vector of Word Frequencies (Bag of Words)
Suppose we have 3 simple documents:
Doc 1: "AI is powerful"
Doc 2: "AI is learning"
Doc 3: "Machine learning is useful"
These vectors help models understand the content similarity between documents.
🎥 3. User’s Movie Preferences as a Vector of Ratings
Imagine 3 movies:
["Inception", "Avatar", "Titanic"]
User A’s ratings:
Inception: 5
Avatar: 3
Titanic: 0 (not watched)
So, User A’s preference vector is:
[5, 3, 0]
User B’s ratings:
Inception: 4
Avatar: 3
Titanic: 5
So, User B’s vector is:
[4, 3, 5]
Now, using vector similarity, a recommendation system can say:
"A and B have similar tastes — recommend Titanic to User A."
Vector operations
⬛ 1. Vector Addition
Let:
𝐀 = [3, 5, 7], 𝐁 = [1, 4, 2]
𝐀 + 𝐁 = [3 + 1, 5 + 4, 7 + 2] = [4, 9, 9]
Used in: Gradient updates, combining feature vectors.
⬛ 2. Vector Subtraction
Let:
𝐀 = [5, 8, 6], 𝐁 = [2, 3, 1]
𝐀 − 𝐁 = [5 − 2, 8 − 3, 6 − 1] = [3, 5, 5]
Used in: Error calculation, tracking changes during optimization.
⬛ 3. Scalar Multiplication
Let:
𝐀 = [2, 4, 6], k = 0.5
k · 𝐀 = [0.5 × 2, 0.5 × 4, 0.5 × 6] = [1, 2, 3]
Used in: Scaling weights, adjusting learning rate effects.
⬛ 4. Dot Product
Let:
𝐀 = [1, 2, 3], 𝐁 = [4, 5, 6]
𝐀 · 𝐁 = (1 × 4) + (2 × 5) + (3 × 6) = 4 + 10 + 18 = 32
Used in: Measuring similarity, computing neuron activations.
⬛ 7. Normalization
Let:
𝐀 = [3, 4]
‖𝐀‖ = √(3² + 4²) = 5
𝐀 (normalized) = [3/5, 4/5] = [0.6, 0.8]
Used in: Scaling vectors for stable learning, computing cosine similarity.
ç̇
+ Example:
⏷
<
‘
1
_
Let’s say we have a small 2×2 image:
Image A:
[ 100 150 ]
[ 200 250 ]
Each number is a pixel's brightness.
We flatten this 2D matrix row by row into a 1D vector:
𝐀→ = [100, 150, 200, 250]
This makes image data easier to use in ML models (which often take input as 1D vectors).
Now consider another image:
𝐁→ = [80, 160, 190, 240]
Option 1: Clipping
Values above 255 are clipped to 255:
Result = [180, 255, 255, 255]
Advantage: Simple
Disadvantage: Loses contrast or detail in bright areas
Vector Norms
⬛’ Application in Normalization
After blending the two images:
A→ + B→ = [180, 310, 390, 490]
These values exceed the valid grayscale range [0, 255], so we normalize.
* Summary Table
˛
C
Norm Type Meaning Role in AI/ML
L2 Euclidean magnitude Similarity, distance metrics, KNN, clustering
L1 Sum of absolute values Sparsity, feature selection, Lasso regularization
L∞ Maximum absolute value Thresholding, adversarial robustness
◆ Mathematical Form
In ordinary linear regression, we minimize the mean squared error (MSE):
𝑳𝒐𝒔𝒔 = 𝜮 (𝒚ᵢ − ŷᵢ)²
In Lasso Regression, we add a penalty term based on the absolute values of the
coefficients:
𝑳𝒐𝒔𝒔 = 𝜮 (𝒚ᵢ − ŷᵢ)² + 𝝀 𝜮 |𝒘ⱼ|
Where:
yᵢ = actual output
ŷᵢ = predicted output
wⱼ = model coefficients
λ = regularization parameter (controls the amount of shrinkage)
◆ Simple Example
Imagine you're predicting house prices using:
Area (sq. ft)
Number of rooms
Distance to school
Wallpaper color
Lasso might assign a weight of zero to "wallpaper color" if it's irrelevant. This removes
the noise and keeps only important predictors.
◆ Summary
Lasso regularization improves model performance by penalizing unnecessary complexity
and encouraging simpler, more generalizable solutions.
MATRICES IN AI/ ML
Matrices are fundamental tools in Artificial Intelligence (AI) and Machine Learning (ML)
because they provide a structured way to represent and manipulate large amounts of data and
mathematical operations efficiently. In these fields, almost everything—from inputs to
outputs, from features to model parameters—is stored and processed using matrices.
1. Matrix Addition
Matrix addition is done element-wise, meaning the corresponding elements of two
matrices are added together.
⬛ Condition: Both matrices must have the same dimensions.
Example:
Let:
A = [1 2] B = [4 5]
[3 4] [6 7]
Then:
A + B = [1+4 2+5] = [5 7]
[3+6 4+7] [9 11]
o 2. Matrix Subtraction
Like addition, subtraction is also element-wise.
Example:
Let:
A = [5 7] B = [1 2]
[9 11] [3 4]
Then:
A- B = [5−1 7−2] = [4 5]
[9−3 11−4] [6 7]
B = [5 6] (2×2)
[7 8]
Then:
A × B = [ (1×5 + 2×7) (1×6 + 2×8) → [19 22]
(3×5 + 4×7) (3×6 + 4×8) ] [43 50]
So:
A × B = [19 22]
[43 50]
◆ 4. Scalar Multiplication
Each element of a matrix is multiplied by a single scalar value.
Example:
k=2
A = [1 2]
[3 4]
k × A = [2×1 2×2] = [2 4]
[2×3 2×4] [6 8]
◆ 5. Transpose of a Matrix
The transpose of a matrix flips rows into columns.
Example:
A = [1 2 3]
[4 5 6]
Aᵀ = [1 4]
[2 5]
[3 6]
⬛ 2. Column Matrix
What it is: A matrix with only one column.
Use: Often used to hold weights or results.
Example:
[2]
[3]
[4]
It's like stacking scores vertically.
⬛ 3. Square Matrix
What it is: Same number of rows and columns.
Use: Used for transformations, like rotating or scaling images.
⬛ 4. Diagonal Matrix
What it is: All values outside the diagonal are zero.
Use: Used to scale things easily. Each number on the diagonal affects a specific value — like
changing brightness per color channel in an image.
⬛ 5. Identity Matrix
What it is: A special diagonal matrix where all diagonal values are 1.
Use: Acts like the number 1 in multiplication. It keeps values the same — helpful in
simplifying equations.
⬛ 6. Zero Matrix
What it is: All values are zero.
Use: Used to reset values or as placeholders.
⬛ 7. Symmetric Matrix
What it is: The matrix is the same when flipped across the diagonal.
Use: Common in statistics to show how features relate — for example, how height and
weight vary together.
⬛ 8. Orthogonal Matrix
What it is: A matrix where rows and columns are at right angles (mathematically speaking).
Use: Useful for rotations and simplifying data in PCA (Principal Component Analysis), a
method to reduce data size.
⬛ 9. Sparse Matrix
What it is: A matrix with mostly zeros.
Use: Happens when data is huge but only a few values matter — like storing how many times
each word appears in millions of documents (most words don’t appear in most documents).
⬛ Matrix Decomposition
Matrix decomposition is the process of breaking down a complex matrix into simpler, more
manageable components — typically a product of two or more matrices. This helps reduce
computational complexity and improves numerical stability. Just as prime factorization
simplifies a number, matrix decomposition simplifies matrices to their core building blocks.
In data science, signal processing, and AI/ML, matrix decomposition plays a vital role in
solving systems of equations, dimensionality reduction, data compression, image
recognition, and recommendation systems.
Example:
Let
A= ⎡2 3⎤
⎣4 7⎦
We want:
L=⎡1 0⎤ U=⎡2 3⎤
⎣ l₂₁ 1 ⎦ ⎣0 1⎦
Steps:
u₁₁ = 2, u₁₂ = 3
l₂₁ = 4 ÷ 2 = 2
u₂₂ = 7 − (2 × 3) = 1
Final L and U:
L=⎡1 0⎤ U=⎡2 3⎤
⎣2 1⎦ ⎣0 1⎦
Check: LU = A ✔
k 2. QR Decomposition
×
˙
´
.
Definition:
QR decomposition breaks a matrix A into:
Q (orthogonal matrix: QᵀQ = I)
R (upper triangular matrix)
A = QR
Use Case:
Used in solving linear regression, particularly when the system has more rows than
columns (overdetermined).
Example:
Let
A= ⎡ 1 1⎤
⎣ 1 −1 ⎦
Q = (1/√2) × ⎡ 1 1⎤
⎣ 1 −1 ⎦
R = √2 × ⎡ 1 0⎤
⎣0 1⎦
Thus, A = QR ✔
« 3. Cholesky Decomposition
#
o
Definition:
If A is symmetric and positive definite, it can be decomposed into:
A = LLᵀ
Where L is a lower triangular matrix and Lᵀ is its transpose.
Example:
Let
A= ⎡ 4 2⎤
⎣2 3⎦
Assume
L= ⎡ a 0⎤
⎣b c⎦
Solving:
a² = 4 → a = 2
ab = 2 → b = 1
b² + c² = 3 → 1 + c² = 3 → c = √2
Then,
L= ⎡2 0⎤
⎣ 1 √2 ⎦
So, A = LLᵀ ✔
, 4. Eigen Decomposition
Definition:
Eigen decomposition expresses a square matrix A as:
A = P D P⁻¹
Where:
P is the matrix of eigenvectors
D is a diagonal matrix of eigenvalues
P⁻¹ is the inverse of the eigenvector matrix
Use Case:
Foundational in Principal Component Analysis (PCA), signal analysis, and system
dynamics.
Example:
Let
A= ⎡ 4 1⎤
⎣2 3⎦
Eigenvalues: λ₁ = 5, λ₂ = 2
Eigenvectors:
v₁ = ⎡ 1 ⎤ v₂ = ⎡ −1 ⎤
⎣1⎦ ⎣2⎦
P = ⎡ 1 −1 ⎤
⎣1 2⎦ D= ⎡5 0⎤
⎣0 2⎦
So, A = P D P⁻¹ ✔
Use Case:
Used in image compression, noise filtering, recommendation engines, and PCA.
Example:
Let
A= ⎡ 3 1⎤
⎣1 3⎦
SVD gives:
U = ⎡ 1/√2 −1/√2 ⎤
⎣ 1/√2 1/√2 ⎦
Σ=⎡4 0⎤
⎣0 2⎦
Vᵀ = Uᵀ
Then, A = U Σ Vᵀ ✔
H
µ
_ Summary Table
–'
‘
l_
Decomposition Form Matrix Type Applications
LU A = LU Square matrix Solving equations, matrix
inversion
QR A = QR Any matrix Least squares, orthogonal
projections
Cholesky A = LLᵀ Symmetric, positive- Engineering, simulations
definite
Eigen A= Square matrix PCA, dynamics, control
PDP⁻¹ systems
SVD A= Any matrix PCA, compression, noise
UΣVᵀ filtering
• Importance of SVD
Q̇
Used in dimensionality reduction (PCA, LSA)
Compresses data while preserving essential features
Solves ill-conditioned or overdetermined systems
Useful in image compression, noise reduction, natural language processing
Foundation of recommender systems
⏷ U, Σ, and Vᵗ
U (Left Singular Vectors):
Columns are eigenvectors of A × Aᵗ
Represents directions in input space
Uᵗ × U = I (orthogonal)
Σ (Singular Values):
Diagonal matrix with values σ₁ ≥ σ₂ ≥ ... ≥ σᵣ > 0
Shows importance of each dimension
Vᵗ (Transpose of Right Singular Vectors):
V contains eigenvectors of Aᵗ × A
Vᵗ × V = I (orthogonal)
🔁 Geometric Interpretation
SVD shows how any linear transformation works as:
Rotation (V) → Scaling (Σ) → Rotation (U)
This means any matrix operation is equivalent to:
Rotating data
Stretching it along principal directions
Rotating it again
⌘
Step-by-Step SVD Example
Aᵗ = [ 3 1 ]
[1 3]
Now compute Aᵗ × A:
Aᵗ × A =
[3 1] [3 1] = [ (3×3 + 1×1) (3×1 + 1×3) ]
[1 3] × [1 3] [ (1×3 + 3×1) (1×1 + 3×3) ]
= [ 9+1 3+3 ]
[ 3+3 1+9 ]
= [ 10 6]
[6 10 ]
➤ For λ = 16:
M = [ 10 6 ]
[ 6 10 ]
Solve (M – 16I) × v = 0
M – 16I =
[10 – 16 6]
[6 10 –16] =
[ -6 6]
[ 6 –6 ]
From row 1:
–6x + 6y = 0 → x = y
So, an eigenvector is:
v₁ = [1, 1]ᵗ
Normalize:
‖v₁‖ = √(1² + 1²) = √2
v₁ (unit) = [1/√2, 1/√2]ᵗ
➤ For λ = 4:
M – 4I =
[6 6]
[6 6]
From row 1:
6x + 6y = 0 → x = –y
So, an eigenvector is:
v₂ = [1, –1]ᵗ
Normalize:
‖v₂‖ = √(1² + (–1)²) = √2
v₂ (unit) = [1/√2, –1/√2]ᵗ
➤ Compute u₁ = (1/4) × A × v₁
Where v₁ = [1/√2, 1/√2]ᵗ
A × v₁ =
[3 1] [1/√2] = [ (3×1/√2 + 1×1/√2) ] = [4/√2]
[1 3] × [1/√2] [ (1×1/√2 + 3×1/√2) ] [4/√2]
➤ Compute u₂ = (1/2) × A × v₂
Where v₂ = [1/√2, –1/√2]ᵗ
A × v₂ =
[3 1] [1/√2] = [ (3×1/√2 + 1×(–1/√2)) ] = [2/√2]
[1 3] × [–1/√2] [ (1×1/√2 + 3×(–1/√2)) ] [–2/√2]
u₂ = (1/2) × [2/√2, –2/√2]ᵗ = [1/√2, –1/√2]ᵗ
● Final Answer:
◎
’
"
´
A = U × Σ × Vᵗ
[3 1]
[1 3]=
[ 1/√2 1/√2 ] [4 0] [ 1/√2 1/√2 ]
[ 1/√2 –1/√2 ] × [ 0 2 ] × [ 1/√2 –1/√2 ]
Ç Spectral Decomposition
If a square matrix A is diagonalizable, it can be written as:
𝐴 = 𝑃𝐷𝑃−1𝐴 = 𝑃𝐷𝑃 − 1
Where:
A is a square matrix.
P is a matrix whose columns are the eigenvectors of A.
D is a diagonal matrix containing the eigenvalues of A.
P⁻¹ is the inverse of matrix P.
Alternatively, for symmetric matrices:
A=QΛQTA = QΛQ^TA=QΛQT
Where:
Q is an orthogonal matrix (i.e., QT=Q−1Q^T = Q^{-1}QT=Q−1).
Λ is a diagonal matrix of eigenvalues.
⬛ Final Result:
A≈
Q × Λ × Qᵗ ≈
| 0.525 0.357 | × | 5.618 0 | × | 0.525 0.851 |
| 0.851 −0.934 | | 0 1.382 | | 0.357 −0.934 |
This reconstruction gives a close approximation of the original matrix A.
# Application: Dimensionality Reduction via PCA
ç/¡
◎
● Principal Component Analysis (PCA)
´
’"
Principal Component Analysis (PCA) is a statistical technique used to reduce the
dimensionality of a dataset while retaining as much variability (information) as possible. It
does this by transforming the original variables into a new set of uncorrelated variables
called principal components. These components are ordered in such a way that the first few
retain most of the variation present in all of the original variables.
|2-λ 0.8 |
| 0.8 0.6 - λ | = 0
Compute the determinant:
(2 - λ)(0.6 - λ) - (0.8)^2 = 0
= λ² - 2.6λ + (1.2 - 0.64)
= λ² - 2.6λ + 0.56 = 0
Now solve this quadratic equation:
λ = [2.6 ± √(2.6² - 4×1×0.56)] / 2
= [2.6 ± √(6.76 - 2.24)] / 2
= [2.6 ± √4.52] / 2
This gives:
λ₁ ≈ 2.06
λ₂ ≈ 0.54
For λ₂ ≈ 0.54:
Substitute λ = 0.54 into (C - λI):
[ 2 - 0.54 0.8 ] = [ 1.46 0.8 ]
[ 0.8 0.6 - 0.54 ] = [ 0.8 0.06 ]
Solve the system:
1.46x + 0.8y = 0
=> y = -1.825x
So an eigenvector corresponding to λ₂ is:
v₂ = [1, -1.825]
Now that we have eigenvectors, we can project the data onto these vectors.
The eigenvector corresponding to the larger eigenvalue (approximately 2.06) is the
principal component direction — it captures the maximum variance in the data.
If we want to reduce the 2D data to 1D, we can project the data points onto the first
principal component (v₁).
Practice questions
A = [2.0 0.8]
[0.8 0.6]
12. Execute PCA in Python on a dataset with 2 features and reduce it to 1 dimension.
Display both original and reconstructed data using a plot.
13. Compute the SVD of a grayscale image using Python. Display the effect of
choosing k = 10, 50, and 100 singular values.
14. Use NumPy to reconstruct a symmetric matrix from its eigenvalues and
eigenvectors. Verify if the original and reconstructed matrices match.
15. Apply PCA to reduce the Iris dataset to 2 dimensions using Python. Visualize the
result using a scatter plot and label each class.