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.
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:
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
Negative scalar
Magnitude is the size, so it is always non-negative.
Mathematically:
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
Unlike vectors, there is no arrow because a scalar has no direction.
Mathematical notation
A scalar is usually written as a lowercase letter:
Here:
- Symbol:
a - Value:
5 - Magnitude:
|a| = 5
Another example:
Scalars vs vectors
Scalar
A scalar is a single value — magnitude only.
Vector
A vector has both magnitude and direction.
Python examples
# 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 → 8Each 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:
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:
- 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
- What is a scalar?
- What does magnitude mean for a scalar?
- Does a scalar have direction?
- If a scalar is
-15, what is its magnitude? - Give three real-world examples of scalars.
- 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
Vector
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.
This vector has:
- X direction = 3
- Y direction = 4
Visual representation
The arrow shows the direction, and its length represents the magnitude.
Components of a vector
A 2D vector has two components:
Example:
| Component | Value |
|---|---|
| X | 3 |
| Y | 4 |
In 3D:
Example: (2, 5, 7)
Magnitude of a vector
Unlike a scalar, a vector's magnitude is calculated using the Pythagorean theorem.
For the vector:
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
import numpy as np
v = np.array([3, 4])
print(v)
print(np.linalg.norm(v))Output:
[3 4]
5.0np.linalg.norm() calculates the vector's magnitude.
The big idea in AI
A vector converts something from the real world into numbers.
For example, a person becomes a vector:
- 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
| Position | Meaning |
|---|---|
| 3 | Bedrooms |
| 1200 | Square feet |
| 2 | Bathrooms |
| 5 | Age of house |
The model receives this vector and predicts:
Why vector? Every house has multiple features, so one vector stores them together.
Scenario 2: Customer Churn Prediction
A telecom company wants to know who will leave.
Customer vector:
| Feature | Value |
|---|---|
| Age | 24 |
| Monthly bill | ₹899 |
| Months subscribed | 12 |
| Complaints | 3 |
AI output:
- 0.92 → Likely to leave
- 0.08 → Will stay
This vector becomes the input to a classification model.
Scenario 3: ChatGPT / Text Embeddings
Suppose the sentence is:
"I love machine learning."
The AI doesn't store words directly.
It converts them into a high-dimensional vector:
Not 3 numbers—but often 768, 1024, or 4096 dimensions.
Similar meaning → Similar vectors
The closer two vectors are, the more similar their meaning.
This is the foundation of:
- ChatGPT memory search
- RAG (Retrieval-Augmented Generation)
- Semantic search
- Recommendation systems
Scenario 4: Image Recognition (Computer Vision)
A handwritten digit is actually numbers.
An image of 28 × 28 pixels becomes:
784 pixel values become one long vector.
The neural network predicts:
Digit = 7
Scenario 5: Real Estate Recommendation
Every property becomes a vector.
| Feature | Value |
|---|---|
| BHK | 2 |
| Area | 1200 |
| Parking | 1 |
| Floor | 8 |
| Amenities score | 4 |
| Metro nearby | 1 |
A user's preference is another vector:
AI compares these vectors to recommend the most similar properties.
This is how Netflix, Amazon, and real estate recommendation engines work.
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
- What is the difference between a scalar and a vector?
- Why is
(3, 4)called a vector? - What are vector components?
- How do you calculate the magnitude of a 2D vector?
- Where are vectors used in Machine Learning?
- 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
- 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:
- Rows = Horizontal
- Columns = Vertical
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:
Here:
- Rows = Houses
- Columns = Features
The target prices are another vector:
This is exactly how supervised ML is trained.
Visual interpretation
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.
Scenario 3: ChatGPT (Embeddings)
Every sentence becomes a vector.
Many sentences become a matrix.
Suppose 4 sentences are converted into embeddings:
Each row = one sentence embedding.
A vector database stores millions of these rows.
When you ask ChatGPT a question:
- Your query becomes a vector.
- The database compares it against the embedding matrix.
- The closest rows are retrieved (RAG).
Scenario 4: Images
A grayscale image:
A 4×4 image is literally a matrix.
- Row = Pixel row
- Column = Pixel column
CNNs process image matrices directly.
Python example
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:
(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.
Every neural network layer performs matrix multiplication:
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
| 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:
Now imagine a color image.
It has Red, Green, and Blue channels.
Shape becomes:
This is a 3D tensor.
- Height = 28
- Width = 28
- Channels = 3
Visual intuition
Think of a tensor as stacked matrices.
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
This is one RGB image.
ML Scenario 2: Batch Training
Neural networks don't train on one image.
They train on batches.
Suppose batch size = 32.
The tensor shape becomes:
Meaning:
- 32 → Images
- 224 → Height
- 224 → Width
- 3 → RGB channels
This 4D tensor is the standard input to CNNs.
ML Scenario 3: ChatGPT (LLMs)
Language models also use tensors.
Sentence:
"I love AI"
First it becomes tokens.
Each token becomes an embedding vector.
If embedding size = 768:
- 3 tokens
- 768 features each
For an entire batch:
Meaning:
| Dimension | Meaning |
|---|---|
| 16 | Batch size |
| 128 | Tokens |
| 768 | Embedding dimensions |
Every transformer layer processes tensors of this shape.
ML Scenario 4: Real Estate AI
Imagine you're predicting property prices using images and metadata.
Property image
Tensor:
Property features
Vector:
The AI combines:
- Tensor → Visual information
- Vector → Structured features
This is called a multimodal model.
Example inputs:
Modern real estate AI often uses this architecture.
Python example (PyTorch)
import torch
# RGB image tensor
image = torch.rand(224, 224, 3)
print(image.shape)Output:
torch.Size([224, 224, 3])Batch of images:
batch = torch.rand(32, 224, 224, 3)
print(batch.shape)Output:
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:
Example:
We simply add each position:
- X: 2 + 4 = 6
- Y: 3 + 1 = 4
Visual intuition
The purple vector is the result of adding A and B.
Example with numbers
Let
Add each element:
| Position | A | B | Result |
|---|---|---|---|
| 1 | 1 | 4 | 5 |
| 2 | 2 | 5 | 7 |
| 3 | 3 | 6 | 9 |
ML Use Case 1: Customer Feature Engineering
Imagine you're building a recommendation system.
A customer has demographic features:
| Feature | Value |
|---|---|
| Age | 25 |
| Salary | 70,000 |
| Purchases | 3 |
Now suppose today's activity is another vector:
The customer bought 2 more items.
Vector addition updates the feature vector:
Why this matters: ML models often combine historical and real-time behavior into one feature vector.
ML Use Case 2: ChatGPT & Word Embeddings
Large Language Models represent every word as a vector.
Suppose:
The model combines meaning using vector addition:
This creates a richer semantic representation before attention layers process it.
Real transformer intuition
Sentence:
"Luxury apartment"
Two embeddings:
The combined vector represents the phrase more effectively than either word alone.
ML Use Case 3: Real Estate Recommendation
This directly matches a property recommendation SaaS platform.
Property vector
| Feature | Value |
|---|---|
| BHK | 2 |
| Area | 1200 |
| Parking | 1 |
| Floor | 8 |
| Amenities | 4 |
User preference vector
Meaning:
- Wants 100 extra sq.ft.
- Wants better amenities
Combined preference
Now the recommendation engine searches properties closer to this updated preference.
Business insight: Instead of replacing user preferences, many recommendation systems incrementally adjust vectors using additions.
ML Use Case 4: Computer Vision
A CNN receives an image tensor.
Before entering the next layer, it adds a bias vector.
Formula:
Where:
- X = Feature vector
- b = Bias vector
- Y = Updated features
Example:
This happens billions of times during deep learning.
ML Use Case 5: Autonomous Vehicle Sensor Fusion
A self-driving car receives information from multiple sensors.
| Sensor | Vector |
|---|---|
| Camera | [120, 80] |
| Radar | [5, -2] |
Combined position estimate:
Vector addition helps merge observations into a more accurate estimate.
Python implementation
Using NumPy:
import numpy as np
A = np.array([2, 3, 4])
B = np.array([5, 1, 6])
C = A + B
print(C)Output:
[7 4 10]In PyTorch:
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:
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.
Example:
Think of it as:
| Component | Calculation | Result |
|---|---|---|
| X | 8 − 3 | 5 |
| Y | 5 − 2 | 3 |
Visual intuition
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:
The model is 6 lakh below the correct answer.
Every ML algorithm learns by minimizing this error.
Memory tip: Subtraction = Actual − Prediction = Error
ML Use Case 2: Real Estate Recommendation
A user wants:
A property has:
Subtract them:
| Difference | Meaning |
|---|---|
| +1 | Needs 1 more bedroom |
| +300 | Needs 300 more sq.ft |
| +1 | Needs 1 more parking |
Instead of saying "not matching", AI knows how much it differs.
This is how recommendation systems rank properties.
ML Use Case 3: ChatGPT / Embeddings
Every sentence is a vector.
Imagine two sentence embeddings:
Difference:
This difference helps AI measure semantic similarity.
- Smaller difference → More similar meaning
- Larger difference → Different meaning
This is used in:
- Semantic Search
- RAG
- Vector Databases
- Recommendation Engines
ML Use Case 4: Computer Vision
Suppose AI tracks a moving car.
Frame 1 position:
Frame 2 position:
Movement:
The car moved:
- 15 pixels right
- 12 pixels down
Object tracking uses subtraction continuously.
Python example
import numpy as np
A = np.array([8, 5])
B = np.array([3, 2])
C = A - B
print(C)Output:
[5 3]Addition vs Subtraction
| Operation | Meaning in AI |
|---|---|
| Addition (+) | Combine information |
| Subtraction (−) | Find difference or error |
Example:
Addition updates customer activity.
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:
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).
The vector is:
The magnitude is the actual distance you walked.
Formula
For a 2D vector:
Example:
Answer: Magnitude = 5
Example 1 (Easy)
Step 1:
Step 2:
Step 3:
Magnitude = 10
ML Use Case 1: Customer Similarity
Imagine two customers.
| Customer | Purchase Vector |
|---|---|
| A | [3, 5] |
| B | [30, 50] |
Customer B buys 10× more, but their buying pattern is the same.
The magnitudes are:
- A → 5.83
- B → 58.3
ML often calculates magnitude before normalizing vectors so customers can be compared fairly.
ML Use Case 2: Word Embeddings (ChatGPT)
Every word is a vector.
Example:
The embedding has a magnitude.
Before comparing meanings, LLMs usually normalize vectors so comparison depends on direction rather than size.
This is why cosine similarity works well.
ML Use Case 3: Image Brightness
A grayscale image row:
A brighter image:
The second vector has a much larger magnitude because pixel intensities are higher.
Computer vision models often normalize pixel magnitudes before training.
ML Use Case 4: Gradient Descent
This is one of the most important uses.
During training, the model computes a gradient vector.
Suppose:
Its magnitude tells us how large the update should be.
- Small magnitude → Small parameter update
- Large magnitude → Large parameter update
Optimizers like SGD and Adam depend on gradient magnitudes.
Python Example
import numpy as np
v = np.array([3, 4])
magnitude = np.linalg.norm(v)
print(magnitude)Output:
5.0np.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.
| 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:
Formula:
This means:
Unit Vector = Original Vector ÷ Magnitude
Step-by-step example
Step 1: Original vector
Step 2: Find magnitude
Step 3: Divide every component
Now check the magnitude:
Result: (0.6, 0.8) is a unit vector.
Visual intuition
Notice:
- Direction stays the same
- Length becomes 1
ML Use Case 1: ChatGPT & Embeddings (Most Important)
Every sentence becomes an embedding vector.
Suppose two sentence embeddings are:
They have different magnitudes, but the same meaning direction.
Before comparing them, AI converts both into unit vectors.
Why?
Because ChatGPT wants to compare meaning, not sentence length.
This is called normalization.
ML Use Case 2: Semantic Search (RAG)
Imagine your vector database stores millions of documents.
User searches:
"Luxury apartments in Bangalore"
The query becomes a vector.
The database also stores unit vectors.
Now cosine similarity compares direction only.
Result:
- Similar apartments appear first.
- Long documents don't unfairly dominate.
ML Use Case 3: Recommendation System (Real Estate App)
User preference:
Property A:
Property B:
Without normalization, Property A looks much larger.
After converting to unit vectors, AI realizes Property B matches the user's preference much better.
This is why recommendation engines normalize feature vectors.
Python Example
Using NumPy:
import numpy as np
v = np.array([3, 4])
unit = v / np.linalg.norm(v)
print(unit)Output:
[0.6 0.8]Using PyTorch:
import torch
v = torch.tensor([3., 4.])
unit = v / torch.norm(v)
print(unit)Real AI Pipeline
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
- Addition — Increase a value
- Subtraction — Find the difference
- Multiplication — Scale a value
- Division — Reduce proportionally
- Power / Square Root — Measure growth or distance
1. Scalar Addition (+)
Addition increases a quantity.
ML use case: Epoch counter
During training:
| Before | Add | After |
|---|---|---|
| 19 | +1 | 20 |
Every training loop increases the epoch by 1.
epoch = 19
epoch = epoch + 1
print(epoch) # 202. Scalar Subtraction (−)
Subtraction finds the difference.
ML use case: Prediction error
Actual accuracy = 90%
Predicted = 75%
Error:
The model has 15% error.
3. Scalar Multiplication (×)
Multiplication scales a value.
ML use case: Learning rate
Gradient = 0.8
Learning rate = 0.1
Update:
The learning rate is a scalar that controls how much the model updates its weights.
4. Scalar Division (÷)
Division reduces a value proportionally.
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:
Average:
This average is reported after every epoch.
5. Power & Square Root
Power means repeated multiplication.
Square root is the opposite.
ML use case: RMSE
Root Mean Squared Error is one of the most common regression metrics.
Don't worry about the full formula yet.
Just remember:
- Find error
- Square it
- Average it
- Take square root
Real AI Scenario (House Price)
A model predicts:
| Price | |
|---|---|
| Actual | ₹80L |
| Predicted | ₹74L |
Step 1: Subtraction (Error)
Step 2: Square
Step 3: Average (if many houses)
Step 4: Square Root
RMSE is calculated.
Notice how scalar operations build the evaluation metric.
Python examples
# 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:
0.30 6 0.08 0.30 36Where 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.
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:
Dot product:
Example
Multiply corresponding elements:
Answer = 23
Visual meaning
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:
If Q · D₁ is larger, Document 1 is more similar.
This is how:
- RAG
- Vector databases
- Semantic search
- ChatGPT retrieval
find relevant documents.
ML Use Case 2: Recommendation System
A real estate recommendation example.
A user wants:
Property A:
Property B:
Dot products:
Very large → Strong match
Raw dot products are often dominated by large features like area, which is why production systems usually normalize vectors first and use cosine similarity. The intuition remains: vectors pointing in similar directions are treated as better matches.
ML Use Case 3: Neural Networks
Every neuron performs a dot product.
Input features:
Weights:
Dot product:
This value becomes the neuron's output before activation.
Every deep learning layer performs millions of dot products.
ML Use Case 4: Computer Vision
An image patch:
A filter (kernel):
CNN computes:
This dot product detects patterns like:
- Edges
- Lines
- Corners
Convolution is fundamentally repeated dot products.
Python Example
import numpy as np
A = np.array([2, 3])
B = np.array([4, 5])
result = np.dot(A, B)
print(result)Output:
23Dot Product vs Multiplication
| Operation | Result |
|---|---|
| Scalar × Scalar | One number |
| Vector + Vector | New vector |
| Dot Product | One scalar |
Notice:
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
Think of them as tables:
Matrix A
Matrix B
Step 2: Find the first cell
Take Row 1 of A:
Take Column 1 of B:
Multiply and add:
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:
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.
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:
Meaning:
- Each bedroom contributes 8
- Each sq.ft contributes 0.05
Prediction:
For House 1:
Predicted score = 76
This is exactly how linear regression works.
ML Use Case 2: Neural Networks (Most Important)
Every neuron performs:
Where:
- X = Input features
- W = Learned weights
- b = Bias
- Y = Output
The multiplication X × W extracts patterns from the input.
Without matrix multiplication, neural networks cannot learn.
ML Use Case 3: ChatGPT / Transformers
Transformers use matrix multiplication continuously.
Input sentence:
"I love AI"
Embeddings become a matrix:
The transformer computes:
Three different weight matrices produce:
- Query
- Key
- Value
This is why GPT models perform billions of matrix multiplications every second.
ML Use Case 4: Real Estate AI
Suppose your property features are:
Weights learned by AI:
Multiply:
The AI produces a property score of 98.
This score can rank properties automatically.
Shape Rule (Very Important)
Not every matrix can be multiplied.
Rule: The inner numbers must match.
- 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
import numpy as np
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
C = A @ B
print(C)Output:
[[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:
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).
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.
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
After
If the length doubles:
- v = Eigenvector
- 2 = Eigenvalue
Meaning: Same direction, but twice as long.
The mathematical equation
This is the only formula you need to remember:
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.
ML Use Case 2: Face Recognition
A face image has thousands of pixels.
PCA finds the main facial patterns.
Examples:
- Eye spacing
- Nose shape
- Face width
These become eigenvectors.
The AI stores only the important components instead of every pixel.
This dramatically reduces memory.
ML Use Case 3: Recommendation System
Suppose your property dataset has 100 features.
Many are correlated:
- Area
- Carpet area
- Built-up area
PCA finds one important direction:
Property Size
Instead of three similar features, AI keeps one principal component.
This improves:
- Faster training
- Less storage
- Reduced noise
ML Use Case 4: Image Compression
A 1024×1024 image contains over 1 million numbers.
Eigenvectors identify the important visual patterns.
The AI keeps only the largest eigenvalues.
Result:
- Smaller image
- Similar quality
This idea is also used in signal processing.
Python Example
Using NumPy:
import numpy as np
A = np.array([[3, 1],
[1, 3]])
values, vectors = np.linalg.eig(A)
print(values) # Eigenvalues
print(vectors) # EigenvectorsOutput (approximately):
[4. 2.]
Eigenvectors...You don't need to memorize the numbers—the important part is understanding what they represent.
Visual summary
- 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.