Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 02

Linear Algebra for AI — Vectors, Matrices, Dot Products & Eigenvalues

Learn linear algebra for AI: vectors, matrices, tensors, vector operations, dot products, and eigenvalues used in machine learning models.

Beginner60 min

What You Will Learn

  • scalar
  • vector
  • matrix
  • Tensor
  • addition
  • substraction
  • magnitude
  • unit vector
  • scalar opr
  • dot product
  • matrix multiplication
  • Eigenvalues & Eigenvectors

scalar

A scalar is a single numerical value that has magnitude only and no direction.

Think of it as the simplest object in mathematics—it is just one number.

Definition

Scalar = One number representing a quantity without direction.

Examples:

Quantity Scalar Value
Temperature 32°C
Age 25
Weight 70 kg
Salary ₹50,000
Probability 0.85

All of these are scalars because they are described by one value only.

What does magnitude mean?

This is the most important word in Linear Algebra.

Magnitude means how much or how big a quantity is.

For a scalar, the magnitude is simply the size of the value itself, because a scalar has no direction.

Simple intuition

Imagine a thermometer:

Diagram

The temperature is 20°C.

  • Scalar = 20
  • Magnitude = 20

There is no "north", "south", "left", or "right". Just the amount.

Examples of scalar magnitude

Scalar Magnitude
Age = 25 25
Weight = 70 kg 70
Salary = ₹50,000 50,000
Probability = 0.85 0.85

The magnitude is simply the numerical size.

Important distinction

Many beginners confuse value and magnitude.

Positive scalar

Diagram

Negative scalar

Diagram

Magnitude is the size, so it is always non-negative.

Mathematically:

Equation |-8| = 8

The vertical bars mean absolute value, which gives the magnitude of a scalar.

Remember this rule

Scalar = a quantity.
Magnitude = the size of that quantity.

Checkpoint: If someone says "The scalar is -15", what is its magnitude?
Answer: 15

Visual intuition

Diagram

Unlike vectors, there is no arrow because a scalar has no direction.

Mathematical notation

A scalar is usually written as a lowercase letter:

Equation a = 5

Here:

  • Symbol: a
  • Value: 5
  • Magnitude: |a| = 5

Another example:

Equation x = 12.7

Scalars vs vectors

Scalar

Diagram

A scalar is a single value — magnitude only.

Vector

Diagram

A vector has both magnitude and direction.

Python examples

python
# Integer scalar
age = 25

# Float scalar
temperature = 36.5

# Boolean scalar
is_verified = True

# Probability scalar
p = 0.91

print(age)
print(temperature)
print(abs(-8))  # magnitude of -8 → 8

Each variable stores one value, so each is a scalar.

Scalars in Machine Learning

Scalars appear everywhere in ML:

ML term Example
Learning rate 0.001
Loss value 0.245
Accuracy 98.2%
Epoch number 20
Batch size 32

For example, during training:

Equation Learning Rate = 0.001

The learning rate is a scalar because it is a single number controlling how much the model updates its weights.

Why magnitude matters in Machine Learning

Suppose your model's loss is:

Equation Loss = 0.25
  • Scalar = 0.25
  • Magnitude = 0.25

The optimizer only sees this as a numerical amount. Direction comes later when gradients (vectors) are introduced.

Real-world analogy

Imagine you're driving a car:

  • Speed = 60 km/h → Scalar (only how fast = magnitude)
  • Velocity = 60 km/h North → Vector (magnitude + direction)

The moment you add direction, it becomes a vector.

Key points

  • A scalar is one numerical value.
  • It has magnitude only.
  • It has no direction.
  • Magnitude means size; for scalars it is the absolute value.
  • Examples include temperature, age, loss, learning rate, and accuracy.
  • Scalars are the foundation for vectors, matrices, and tensors.

Interview questions

  1. What is a scalar?
  2. What does magnitude mean for a scalar?
  3. Does a scalar have direction?
  4. If a scalar is -15, what is its magnitude?
  5. Give three real-world examples of scalars.
  6. Is the learning rate in machine learning a scalar or vector? Why?

Next lesson: Vector — a quantity with both magnitude and direction.

vector

A vector is a quantity that has both magnitude and direction.

Vector = Magnitude + Direction

This is the next concept after scalars in your Linear Algebra roadmap.

Definition

A vector tells you how much (magnitude) and where (direction).

In AI/ML, a vector is how a computer represents real-world objects as numbers. The model cannot understand text, images, or users directly—it only understands vectors.

Intuition: Scalar vs Vector

A scalar only tells you how much.

A vector tells you how much and where.

Scalar

Diagram

Vector

Diagram

Real-world example

Imagine you're walking.

  • 5 km → Scalar (magnitude only)
  • 5 km East → Vector (magnitude + direction)

The word East makes it a vector.

Mathematical notation

A vector is written with an arrow or in bold.

Matrix v

This vector has:

  • X direction = 3
  • Y direction = 4

Visual representation

Diagram

The arrow shows the direction, and its length represents the magnitude.

Components of a vector

A 2D vector has two components:

Equation v = (x, y)

Example:

Equation v = (3, 4)
Component Value
X 3
Y 4

In 3D:

Equation v = (x, y, z)

Example: (2, 5, 7)

Magnitude of a vector

Unlike a scalar, a vector's magnitude is calculated using the Pythagorean theorem.

For the vector:

Equation v = (3, 4)
Equation ‖v‖ = √(3² + 4²) = 5

So:

  • Vector = (3, 4)
  • Magnitude = 5

For a scalar, magnitude is the absolute value.
For a vector, magnitude is the length of the arrow.

Python example

python
import numpy as np

v = np.array([3, 4])

print(v)
print(np.linalg.norm(v))

Output:

text
[3 4]
5.0

np.linalg.norm() calculates the vector's magnitude.

The big idea in AI

A vector converts something from the real world into numbers.

Diagram

For example, a person becomes a vector:

Matrix x
  • 30 = Age
  • 180 = Height (cm)
  • 75000 = Salary

This is called a feature vector.

Vectors in Machine Learning

Vectors are everywhere in AI.

ML object Vector?
Image pixel row ✅
Word embedding ✅
User features ✅
GPS coordinates ✅
Learning rate ❌ (Scalar)

Scenario 1: House Price Prediction

Imagine you are building a model to predict house prices.

One house = One vector

Matrix x
Position Meaning
3 Bedrooms
1200 Square feet
2 Bathrooms
5 Age of house

The model receives this vector and predicts:

Equation y = ₹82,00,000

Why vector? Every house has multiple features, so one vector stores them together.

Why vectors are everywhere in AI

AI Application What becomes a vector?
House Price ML House features
Fraud Detection Transaction features
CRM Customer behavior
ChatGPT Words & sentences
Image AI Pixels
Recommendation User + Product
Voice AI Audio signals

Scalar vs Vector summary

Property Scalar Vector
Values One Multiple
Magnitude Yes Yes
Direction No Yes
Example 25°C 25 km East
ML Example Learning rate Word embedding

Key points

  • A vector has magnitude and direction.
  • It is represented by an arrow or ordered numbers like (3, 4).
  • The numbers are called components.
  • Magnitude is the length: ‖v‖ = √(x² + y²).
  • In AI, a vector is how real-world objects become numbers for models.
  • Vectors represent data, features, embeddings, gradients, and coordinates.

Interview questions

  1. What is the difference between a scalar and a vector?
  2. Why is (3, 4) called a vector?
  3. What are vector components?
  4. How do you calculate the magnitude of a 2D vector?
  5. Where are vectors used in Machine Learning?
  6. What is a feature vector?

Remember this sentence

A vector is the language of AI. Everything the model learns—text, images, customers, houses, and audio—is ultimately represented as vectors of numbers.

Next lesson: Matrix — where many vectors are stored together as a dataset.

matrix

A matrix is a rectangular table of numbers arranged in rows and columns.

Matrix = Collection of vectors organized as rows and columns

In Machine Learning, a single vector represents one object, while a matrix represents many objects together.

From scalar → vector → matrix

Diagram
  • Scalar → one value
  • Vector → one object's features
  • Matrix → many objects (dataset)

What does a matrix look like?

A matrix with 3 rows and 4 columns:

Matrix A
  • Rows = Horizontal
  • Columns = Vertical
Diagram

Matrix in Machine Learning

Scenario 1: House Price Dataset

Suppose you have 4 houses.

Each house is a vector.

Together they become a matrix.

Feature Matrix (X)

House House vector
House 1 [3, 1200, 2]
House 2 [2, 900, 1]
House 3 [4, 1800, 3]
House 4 [3, 1400, 2]

This becomes one matrix:

Matrix X

Here:

  • Rows = Houses
  • Columns = Features

The target prices are another vector:

Matrix y

This is exactly how supervised ML is trained.

Visual interpretation

Diagram

Every row is one training example.

Every column is one feature.

Scenario 2: Customer Churn

Imagine 1 million customers.

Customer Matrix

Customer Age Bill Complaints
C1 24 899 3
C2 31 699 0
C3 45 1299 5

Instead of processing one customer at a time, ML processes the entire matrix.

This makes training fast and efficient.

Python example

python
import numpy as np

# Feature matrix
X = np.array([
    [3, 1200, 2],
    [2, 900, 1],
    [4, 1800, 3],
    [3, 1400, 2]
])

print(X.shape)
print(X)

Output:

text
(4, 3)

Meaning:

  • 4 rows = 4 houses
  • 3 columns = 3 features

Matrix multiplication in AI

This is one of the most important operations in Deep Learning.

Diagram

Every neural network layer performs matrix multiplication:

Equation Y = XW + b

Where:

  • X = Input feature matrix
  • W = Weight matrix (learned by AI)
  • b = Bias vector
  • Y = Output predictions

This equation powers neural networks, transformers, and LLMs.

Scalar vs Vector vs Matrix

Concept ML Meaning Example
Scalar One value Learning rate = 0.001
Vector One sample One house's features
Matrix Entire dataset 10,000 houses × 20 features

Key points

  • A matrix is a table of numbers arranged in rows and columns.
  • Each row usually represents one training sample.
  • Each column represents one feature.
  • Images are matrices of pixels.
  • Neural networks use matrix multiplication to learn patterns.

Interview question

Why do ML models use matrices instead of vectors?

Answer: A vector represents one data sample, while a matrix stores many samples together, allowing the model to train efficiently using batch processing and matrix operations.

Next lesson: Tensor — multi-dimensional arrays beyond 2D matrices.

Tensor

A tensor is a multi-dimensional collection of numbers.

Tensor = Generalization of scalars, vectors, and matrices

The easiest way to understand tensors is by building up from the previous concepts.

From scalar → vector → matrix → tensor

Diagram
Object Dimensions Example
Scalar 0D 5
Vector 1D [3, 4]
Matrix 2D 3 × 4 table
Tensor 3D+ Images, videos, batches

A tensor is not a new type of data—it is simply data with more dimensions.

Why do we need tensors?

Imagine you have one image.

A grayscale image is a matrix.

Its shape is:

Equation 28 × 28

Now imagine a color image.

It has Red, Green, and Blue channels.

Shape becomes:

Equation 28 × 28 × 3

This is a 3D tensor.

  • Height = 28
  • Width = 28
  • Channels = 3

Visual intuition

Think of a tensor as stacked matrices.

Diagram

Each colored layer is a matrix.

Together they form one tensor.

ML Scenario 1: Computer Vision

Suppose you're building an AI that recognizes cats and dogs.

The model receives images like this:

Image Tensor Shape
Grayscale 28×28×1
RGB 224×224×3
HD Image 1080×1920×3

The CNN processes tensors—not JPEG files.

One image tensor

Equation (224, 224, 3)

This is one RGB image.

Python example (PyTorch)

python
import torch

# RGB image tensor
image = torch.rand(224, 224, 3)

print(image.shape)

Output:

text
torch.Size([224, 224, 3])

Batch of images:

python
batch = torch.rand(32, 224, 224, 3)

print(batch.shape)

Output:

text
torch.Size([32, 224, 224, 3])

Tensor vs Matrix vs Vector

Concept Dimensions AI Example
Scalar 0D Learning rate
Vector 1D Customer features
Matrix 2D Dataset (houses × features)
Tensor 3D+ Images, videos, LLM batches

Where tensors are used in AI

AI Domain Tensor Example
Computer Vision RGB images (224,224,3)
Video AI Frames (30,224,224,3)
ChatGPT Token embeddings (128,768)
Speech Recognition Spectrogram tensors
Medical AI 3D MRI scans
Autonomous Cars Camera + LiDAR tensors

Key takeaway

A tensor is the universal data structure of Deep Learning. Scalars, vectors, and matrices are all special cases of tensors. Frameworks like PyTorch and TensorFlow perform almost every neural network operation using tensors.

Interview question

Q: Why do deep learning frameworks use tensors instead of matrices?

Answer: Because real-world AI data is often multi-dimensional—images (3D), video (4D), batches of text embeddings (3D), and medical scans (5D). Matrices are limited to 2D, while tensors can represent any number of dimensions.

addition

Vector addition is one of the most important operations in AI and Machine Learning. It is used to combine information from different sources—features, embeddings, gradients, sensor data, and multimodal inputs.

What is vector addition?

Vector addition means adding two vectors component by component.

If two vectors have the same dimensions:

Vector addition A + B = C

Example:

Vector addition example 2,3 plus 4,1 equals 6,4

We simply add each position:

  • X: 2 + 4 = 6
  • Y: 3 + 1 = 4

Visual intuition

Diagram

The purple vector is the result of adding A and B.

Example with numbers

Let

Equation A = (1, 2, 3), B = (4, 5, 6)

Add each element:

Equation C = (5, 7, 9)
Position A B Result
1 1 4 5
2 2 5 7
3 3 6 9

Python implementation

Using NumPy:

python
import numpy as np

A = np.array([2, 3, 4])
B = np.array([5, 1, 6])

C = A + B

print(C)

Output:

text
[7 4 10]

In PyTorch:

python
import torch

A = torch.tensor([1.0, 2.0, 3.0])
B = torch.tensor([4.0, 5.0, 6.0])

print(A + B)

Where vector addition appears in AI

AI Domain What is being added?
House Price ML Feature updates
CRM Customer behavior vectors
ChatGPT Word/position embeddings
Computer Vision Feature + bias vectors
Recommendation User + preference vectors
Robotics Sensor measurements

Why is vector addition important?

Nearly every modern AI model combines information through addition:

Diagram

The model is constantly creating new representations by adding vectors together.

Key points for your notes

  • Vector addition requires the same dimensions.
  • Addition is performed component by component.
  • It combines information rather than replacing it.
  • Used in embeddings, feature engineering, neural networks, recommendation systems, and sensor fusion.

Interview question

Q: Why must two vectors have the same dimensions for addition?

Answer: Because each component represents the same type of feature or coordinate. Adding a 3-feature customer vector to a 5-feature vector has no mathematical or semantic meaning.

Next lesson: Vector Subtraction.

substraction

After learning addition, subtraction is even easier.

Vector subtraction means finding the difference between two vectors.

In AI/ML, subtraction is used to measure error, change, distance, and movement.

Simple definition

Subtract each component one by one.

Equation A - B = C

Example:

Vector subtraction example (8,5) - (3,2) = (5,3)

Think of it as:

Component Calculation Result
X 8 − 3 5
Y 5 − 2 3

Visual intuition

Diagram

The red arrow shows the difference between the two positions.

Imagine:

  • A = Your destination
  • B = Your current location

A - B tells you how far and in which direction you still need to move.

ML Use Case 1: House Price Prediction (Error)

This is the most common use of subtraction in ML.

Suppose the true house price is ₹80 lakh.

Your model predicts ₹74 lakh.

Value (₹ lakh)
Actual 80
Predicted 74

The error is:

Equation Error = 80 - 74 = 6

The model is 6 lakh below the correct answer.

Every ML algorithm learns by minimizing this error.

Memory tip: Subtraction = Actual − Prediction = Error

Python example

python
import numpy as np

A = np.array([8, 5])
B = np.array([3, 2])

C = A - B

print(C)

Output:

text
[5 3]

Addition vs Subtraction

Operation Meaning in AI
Addition (+) Combine information
Subtraction (−) Find difference or error

Example:

Equation (25, 70000, 3) + (0, 0, 2) = (25, 70000, 5)

Addition updates customer activity.

Equation 80 - 74 = 6

Subtraction calculates prediction error.

Key points for your notes

  • Vector subtraction works component by component.
  • Both vectors must have the same dimensions.
  • In ML, subtraction is mainly used for error calculation, difference measurement, similarity, and object movement.
  • One of the most important formulas in machine learning is:
Equation Error = Actual - Predicted

Interview question

Q: Why is vector subtraction important in Machine Learning?

Answer: Because ML models learn by comparing predictions with actual values. Subtraction computes the error (residual), which is then used to update the model and improve its predictions.

Next lesson: Vector Magnitude.

magnitude

Magnitude is one of the most important concepts in ML because it tells us how large a vector is.

Magnitude = The length (size) of a vector

A scalar's magnitude is just its value, but a vector's magnitude is its length from the origin.

Intuition: Think of a GPS map

Imagine you're standing at the origin (0,0) and walk to (3,4).

Diagram

The vector is:

Equation v = (3, 4)

The magnitude is the actual distance you walked.

Formula

For a 2D vector:

Equation ‖v‖ = √(x² + y²)

Example:

Equation ‖v‖ = √(3² + 4²) = √(9 + 16) = √(25) = 5

Answer: Magnitude = 5

Example 1 (Easy)

Equation A = (6, 8)

Step 1:

Equation 6² = 36, 8² = 64

Step 2:

Equation 36 + 64 = 100

Step 3:

Equation √(100) = 10

Magnitude = 10

Python Example

python
import numpy as np

v = np.array([3, 4])

magnitude = np.linalg.norm(v)

print(magnitude)

Output:

text
5.0

np.linalg.norm() computes the vector magnitude.

Why magnitude matters in AI

AI Area Why magnitude is used
Recommendation Measure vector size before similarity
ChatGPT Normalize embeddings
Computer Vision Normalize pixel values
Neural Networks Gradient update size
Robotics Distance and movement

Scalar vs Vector Magnitude

Object Magnitude
Scalar 8 8
Scalar -8 8
Vector (3,4) 5
Vector (6,8) 10

Notice: Vector magnitude is calculated, not simply read.

Memory trick

Vector = Arrow → Magnitude is how long the arrow is.

If the arrow gets longer, its magnitude increases. If it gets shorter, its magnitude decreases.

Interview question

Q: Why is magnitude important in Machine Learning?

Answer: Magnitude measures the size of vectors. It is used for normalization, calculating distances, comparing embeddings, and determining gradient update strength during neural network training.

Next lesson: Unit Vector.

unit vector

A unit vector is a vector whose magnitude (length) is exactly 1.

Unit Vector = Direction only (length = 1)

This is one of the most important concepts in AI because models often normalize vectors into unit vectors before comparing them.

First understand the problem

Suppose you have two vectors pointing in the same direction.

Diagram
Vector Magnitude
(3,4) 5
(6,8) 10

They point in the same direction, but one is longer.

A unit vector removes the length and keeps only the direction.

Definition

A unit vector is written with a hat:

Equation v

Formula:

Equation unit vector v̂ = v / ‖v‖

This means:

Unit Vector = Original Vector ÷ Magnitude

Step-by-step example

Step 1: Original vector

Equation v = (3, 4)

Step 2: Find magnitude

Equation ‖v‖ = 5

Step 3: Divide every component

Equation unit vector (0.6, 0.8)

Now check the magnitude:

Equation √(0.6² + 0.8²) = 1

Result: (0.6, 0.8) is a unit vector.

Visual intuition

Diagram

Notice:

  • Direction stays the same
  • Length becomes 1

Python Example

Using NumPy:

python
import numpy as np

v = np.array([3, 4])

unit = v / np.linalg.norm(v)

print(unit)

Output:

text
[0.6 0.8]

Using PyTorch:

python
import torch

v = torch.tensor([3., 4.])

unit = v / torch.norm(v)

print(unit)

Real AI Pipeline

Diagram

This normalization step happens in many LLM and RAG systems.

Summary Table

Concept Meaning
Vector Has magnitude + direction
Magnitude Length of the vector
Unit Vector Same direction, magnitude = 1
Formula v̂ = v / ‖v‖
Main AI Use Embedding normalization

Interview Question

Q: Why do LLMs convert embeddings into unit vectors?

Answer: To remove the effect of vector length and compare only semantic direction. This makes similarity search (cosine similarity) more accurate in RAG, vector databases, and recommendation systems.

One-line memory trick

Unit Vector = Original Vector ÷ Magnitude → Keeps direction, makes length = 1.

scalar opr

A scalar operation means performing arithmetic on single numbers.

In Machine Learning, scalars control the behavior of models—they are used as learning rates, loss values, probabilities, and thresholds.

Scalar = One number → Operations happen on one value

The 5 scalar operations

  1. Addition — Increase a value
Equation 5 + 3 = 8
  1. Subtraction — Find the difference
Equation 8 - 3 = 5
  1. Multiplication — Scale a value
Equation 4 × 3 = 12
  1. Division — Reduce proportionally
Equation 12 ÷ 3 = 4
  1. Power / Square Root — Measure growth or distance
Equation 5² = 25

1. Scalar Addition (+)

Addition increases a quantity.

Equation 20 + 5 = 25

ML use case: Epoch counter

During training:

Before Add After
19 +1 20

Every training loop increases the epoch by 1.

python
epoch = 19
epoch = epoch + 1

print(epoch)  # 20

2. Scalar Subtraction (−)

Subtraction finds the difference.

Equation 0.90 - 0.75 = 0.15

ML use case: Prediction error

Actual accuracy = 90%

Predicted = 75%

Error:

Equation 0.90 - 0.75 = 0.15

The model has 15% error.

3. Scalar Multiplication (×)

Multiplication scales a value.

Equation 10 × 2 = 20

ML use case: Learning rate

Gradient = 0.8

Learning rate = 0.1

Update:

Equation 0.8 × 0.1 = 0.08

The learning rate is a scalar that controls how much the model updates its weights.

Diagram

4. Scalar Division (÷)

Division reduces a value proportionally.

Equation 100 ÷ 4 = 25

ML use case: Average loss

Suppose 4 batches produce losses:

Batch Loss
1 0.30
2 0.25
3 0.35
4 0.30

Total:

Equation 1.20

Average:

Equation 1.20 ÷ 4 = 0.30

This average is reported after every epoch.

5. Power & Square Root

Power means repeated multiplication.

Equation 5² = 25

Square root is the opposite.

Equation √(25) = 5

ML use case: RMSE

Root Mean Squared Error is one of the most common regression metrics.

Equation RMSE formula

Don't worry about the full formula yet.

Just remember:

  1. Find error
  2. Square it
  3. Average it
  4. Take square root

Real AI Scenario (House Price)

A model predicts:

Price
Actual ₹80L
Predicted ₹74L

Step 1: Subtraction (Error)

Equation 80 - 74 = 6

Step 2: Square

Equation 6² = 36

Step 3: Average (if many houses)

Equation 36 ÷ n

Step 4: Square Root

RMSE is calculated.

Notice how scalar operations build the evaluation metric.

Python examples

python
# Addition
loss = 0.25 + 0.05

# Subtraction
error = 80 - 74

# Multiplication
update = 0.8 * 0.1

# Division
average = 1.2 / 4

# Power
squared = 6 ** 2

print(loss, error, update, average, squared)

Output:

text
0.30 6 0.08 0.30 36

Where scalar operations are used in ML

Operation ML Example
Addition Epoch counter
Subtraction Prediction error
Multiplication Learning rate × Gradient
Division Average loss
Power RMSE, MSE calculations

Interview question

Q: Why are scalar operations important in Machine Learning?

Answer: Scalars control model training and evaluation. They are used for learning rates, loss values, accuracy, probabilities, and error metrics, making them fundamental to optimization and performance measurement.

dot product

The dot product measures how similar two vectors point in the same direction.

Dot Product = Similarity between two vectors

This is one of the most important operations in Machine Learning, ChatGPT, RAG, recommendation systems, and Computer Vision.

Intuition first

Imagine two people pushing a box.

Diagram

Case 1: Same direction

Both push right → Box moves easily.

Dot product = Large Positive

Case 2: Opposite direction

One pushes left, one pushes right.

Dot product = Negative

Case 3: 90° direction

One pushes sideways.

Dot product = 0

The push doesn't help move the box forward.

Mathematical formula

For two vectors:

Equation A = (a₁, a₂), B = (b₁, b₂)

Dot product:

Equation A · B = a₁ b₁ + a₂ b₂

Example

Equation A = (2, 3), B = (4, 5)

Multiply corresponding elements:

Equation (2 × 4) + (3 × 5) = 8 + 15 = 23

Answer = 23

Visual meaning

Diagram

The closer the directions, the larger the dot product.

ML Use Case 1: ChatGPT (Most Important)

When you ask ChatGPT:

"Luxury apartments in Bangalore"

The sentence becomes an embedding vector.

Documents are also embedding vectors.

Item Vector
User Query Q
Document 1 D₁
Document 2 D₂

The system computes:

Equation Q · D₁
Equation Q · D₂

If Q · D₁ is larger, Document 1 is more similar.

This is how:

  • RAG
  • Vector databases
  • Semantic search
  • ChatGPT retrieval

find relevant documents.

Python Example

python
import numpy as np

A = np.array([2, 3])
B = np.array([4, 5])

result = np.dot(A, B)

print(result)

Output:

text
23

Dot Product vs Multiplication

Operation Result
Scalar × Scalar One number
Vector + Vector New vector
Dot Product One scalar

Notice:

Equation (2, 3) · (4, 5) = 23

The answer is not another vector. It is a single scalar.

Summary

Dot Product Value Meaning
Positive Similar direction
Zero Perpendicular / unrelated
Negative Opposite direction

Memory trick

Dot Product asks one question: "How much do these two vectors point in the same direction?"

That's why it's everywhere in AI—from ChatGPT retrieval to recommendation engines and every neural network neuron.

Interview question

Q: Why is the dot product important in Machine Learning?

Answer: It measures similarity between vectors. It powers semantic search, recommendation matching, neural network computations, and CNN convolutions—all by comparing how much two vectors align in direction.

matrix multiplication

Matrix multiplication is the heart of Deep Learning. Every neural network layer, transformer, and LLM performs matrix multiplication millions of times.

Matrix multiplication = Every row of Matrix A × Every column of Matrix B

Intuition first

Imagine you have:

  • Matrix A = Students' marks
  • Matrix B = Weightage of each subject

The multiplication combines them to produce a final score.

The key idea is simple:

  • Take one row from A
  • Take one column from B
  • Multiply corresponding numbers
  • Add them together
  • That becomes one cell in the answer

Step 1: Two matrices

Matrix equation

Think of them as tables:

Matrix A

Diagram

Matrix B

Diagram

Step 2: Find the first cell

Take Row 1 of A:

Equation (1, 2)

Take Column 1 of B:

Matrix

Multiply and add:

Equation (1 × 5) + (2 × 7) = 5 + 14 = 19

First cell = 19

Step 3: Repeat for every cell

Cell Calculation Answer
C₁₁ (1×5)+(2×7) 19
C₁₂ (1×6)+(2×8) 22
C₂₁ (3×5)+(4×7) 43
C₂₂ (3×6)+(4×8) 50

Final matrix:

Matrix C

Why rows × columns?

Think of it as asking:

How much does this row match this column?

Every answer becomes one value in the new matrix.

Diagram

ML Use Case 1: House Price Prediction

Imagine 3 houses with 2 features.

Input matrix (X)

House BHK Area
H1 2 1200
H2 3 1500
H3 4 1800

Weight matrix:

Matrix W

Meaning:

  • Each bedroom contributes 8
  • Each sq.ft contributes 0.05

Prediction:

Equation Y = XW

For House 1:

Equation (2 × 8) + (1200 × 0.05) = 16 + 60 = 76

Predicted score = 76

This is exactly how linear regression works.

Shape Rule (Very Important)

Not every matrix can be multiplied.

Rule: The inner numbers must match.

Diagram
  • 2×3 × 3×4 = ✅ 2×4
  • 2×3 × 2×4 = ❌ Not possible

Memory trick:

Inside numbers must match. Outside numbers become the result.

Python Example

python
import numpy as np

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

B = np.array([[5, 6],
              [7, 8]])

C = A @ B

print(C)

Output:

text
[[19 22]
 [43 50]]

@ is Python's matrix multiplication operator.

Where Matrix Multiplication is Used in AI

AI System Purpose
Linear Regression Features × Weights
Neural Networks Input × Weight Matrix
ChatGPT Query, Key, Value computation
CNN Feature transformations
Recommendation User × Item embeddings

Key Takeaways

  • Matrix multiplication combines rows and columns.
  • Every output cell is a dot product.
  • It powers neural networks, transformers, LLMs, and recommendation systems.
  • Formula to remember:
Equation Y = XW + b

This is the foundation of deep learning.

Interview Question

Q: Why is matrix multiplication essential in deep learning?

Answer: Because it efficiently combines input features with learned weights. Every neuron, transformer attention layer, and LLM uses matrix multiplication to transform data into new representations that the model can learn from.

Eigenvalues & Eigenvectors

This is the last major topic in your Linear Algebra section. It looks difficult, but the core idea is actually simple.

Eigenvector = The direction that does not change after a transformation.

Eigenvalue = How much that direction is stretched or shrunk.

Think of it as Direction + Scaling.

Step 1: What is a transformation?

Imagine a square drawn on paper. An AI model applies a transformation (rotation, stretching, compression).

Diagram

The shape changes. But some directions stay the same.

Those special directions are eigenvectors.

Step 2: What is an eigenvector?

Imagine an arrow pointing diagonally.

Diagram

After transformation:

  • Direction stays the same ✅
  • Length changes ✅

That arrow is the eigenvector.

Step 3: What is an eigenvalue?

The eigenvalue tells how much the eigenvector changes in size.

Before

Diagram

After

Diagram

If the length doubles:

Equation Av = 2v
  • v = Eigenvector
  • 2 = Eigenvalue

Meaning: Same direction, but twice as long.

The mathematical equation

This is the only formula you need to remember:

Equation Av = lambda v

Where:

Symbol Meaning
A Transformation matrix
v Eigenvector
λ (lambda) Eigenvalue

Read it like this:

Matrix A transforms vector v into the same direction, scaled by λ.

ML Use Case 1: PCA (Principal Component Analysis)

This is the biggest real-world use.

Imagine a dataset of houses.

Each dot is one house.

The data is spread diagonally.

PCA asks:

What is the most important direction in this data?

That direction is the principal component, which is an eigenvector.

Before PCA

Data has 20 features.

  • BHK
  • Area
  • Price
  • Parking
  • Floor
  • Amenities
  • …

After PCA

AI keeps only the most important directions.

Instead of 20 features:

  • PC1
  • PC2

Much smaller, but most information is preserved.

Why eigenvalues?

Eigenvalues tell how important each direction is.

Eigenvalue Importance
8.5 Very important
3.2 Medium
0.1 Ignore

Higher eigenvalue = More information.

Python Example

Using NumPy:

python
import numpy as np

A = np.array([[3, 1],
              [1, 3]])

values, vectors = np.linalg.eig(A)

print(values)      # Eigenvalues
print(vectors)     # Eigenvectors

Output (approximately):

text
[4. 2.]
Eigenvectors...

You don't need to memorize the numbers—the important part is understanding what they represent.

Visual summary

Diagram
  • Green arrow → Direction stays the same (Eigenvector)
  • Length increases → Eigenvalue = 4
  • Gray arrow → Ordinary vectors change direction

Where eigenvalues & eigenvectors are used in AI

AI Application Why they're used
PCA Find the most important data directions
Face Recognition Reduce image dimensions
Image Compression Keep important visual information
Recommendation Systems Remove redundant features
Signal Processing Noise reduction

One-line memory trick

Eigenvector = Direction that never changes. Eigenvalue = How much that direction grows or shrinks.

Interview question

Q: Why is PCA based on eigenvectors?

Answer: PCA uses eigenvectors to find the directions of maximum variance in the data, and eigenvalues tell how much information (variance) each direction contains. This allows dimensionality reduction while preserving the most important patterns.