Assignment 1 – Complete Explanation
(From Basics to Advanced)
🔵 PART A – Computer Vision + Deep
Learning
1. 🎥 Video Capture (Webcam vs 5G Camera)
Concept:
A video is just a sequence of images (frames).
● Each frame = image
● Video = frames per second (FPS)
Key Properties:
● Resolution → Width × Height (e.g., 1920×1080)
● FPS → Frames per second
● Duration → Total time
Python (OpenCV) Code:
import cv2
cap = [Link](0) # 0 = webcam
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = [Link]('[Link]', fourcc, 20.0, (640, 480))
while True:
ret, frame = [Link]()
if not ret:
break
[Link](frame)
[Link]('frame', frame)
if [Link](1) & 0xFF == ord('q'):
break
[Link]()
[Link]()
[Link]()
2. 🧠 Frame Analysis (Size, Intensity)
Image Representation:
● Image = matrix of pixels
● Color image = 3 channels (RGB)
Intensity:
● Pixel value range: 0–255
● 0 = black, 255 = white
Code:
import numpy as np
[Link] # (height, width, channels)
# Average intensity
gray = [Link](frame, cv2.COLOR_BGR2GRAY)
intensity = [Link](gray)
print(intensity)
3. 🤖 YOLOv8n (Object Detection)
Concept:
YOLO = You Only Look Once
● Detects objects in one pass
● Gives bounding boxes + labels
YOLOv8n = lightweight version
Install:
pip install ultralytics
Code:
from ultralytics import YOLO
import time
model = YOLO('[Link]')
start = [Link]()
results = model('[Link]')
end = [Link]()
print("Time:", end - start)
4. ⏱️ Time Measurement
Concept:
● Measure performance of model
import time
start = [Link]()
# run model
end = [Link]()
print(end - start)
5. 🔗 Triplet Loss (Similarity Matching)
Concept:
Used in Face Recognition
Triplet =
● Anchor (A)
● Positive (P) → same person
● Negative (N) → different person
Formula:
Loss = max(0, d(A,P) - d(A,N) + margin)
Goal:
● Make same faces closer
● Different faces farther
Matching %:
● Use embeddings distance
from [Link] import euclidean
dist = euclidean(vec1, vec2)
similarity = 1 / (1 + dist)
print(similarity * 100)
🔵 PART B – Basic Programming +
Mathematics
1. 🧮 nCr (Combination)
Formula:
nCr = n! / (r! (n-r)!)
Factorial:
n! = n × (n-1) × ... × 1
Code:
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n-1)
def nCr(n, r):
return factorial(n) // (factorial(r) * factorial(n-r))
2. 🔁 LCM (Least Common Multiple)
Concept:
LCM = smallest number divisible by both
Relation:
LCM(a,b) = (a × b) / GCD(a,b)
Code:
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return (a * b) // gcd(a, b)
3. 📄 File Handling
Input Format:
Each line: n r C/L
Code:
with open('[Link]', 'r') as f:
for line in f:
n, r, op = [Link]()
n = int(n)
r = int(r)
if op == 'C':
print(nCr(n, r))
else:
print(lcm(n, r))
📌 FINAL REVISION NOTES
PART A:
● Video = frames
● Image = matrix
● Intensity = pixel values
● YOLO = object detection
● Triplet loss = similarity learning
PART B:
● nCr → combinations
● Factorial recursion
● LCM via GCD
● File reading loop
⚠️ COMMON MISTAKES
● Wrong factorial (forget base case)
● Integer division issues
● Not converting string → int
● Not measuring time properly
● Confusing similarity vs distance
🎯 YOU SHOULD NOW KNOW
● Computer vision basics
● Object detection using YOLO
● Face similarity concept
● Core math functions
● File handling in Python
If you want, I can next:
● Give you FULL WORKING PROJECT CODE 🔥
🎯
● Help you RUN this on your system (step-by-step)
● Or prepare VIVA questions from this assignment
Assignment 2 – Complete Breakdown
(Machine Learning Lab)
🔵 PART 1 – FIND-S ALGORITHM (CORE
ML CONCEPT)
🧠 What is Find-S?
👉
Find-S is a supervised learning algorithm used to find the:
Most specific hypothesis that fits all positive examples
Key Idea:
● Start with most specific hypothesis
● Generalize only when needed
● Ignore negative examples
📊 Given Dataset:
Sky AirTemp Humidity Wind PlayTenni
s
Sunny Warm Normal Strong Yes
Sunny Warm High Strong Yes
Rainy Cold High Strong No
Sunny Warm High Strong Yes
🧩 Step-by-Step Execution
Step 1: Initialize Hypothesis
H = [Ø, Ø, Ø, Ø]
Step 2: Process first positive example
(Sunny, Warm, Normal, Strong)
H = [Sunny, Warm, Normal, Strong]
Step 3: Second positive example
(Sunny, Warm, High, Strong)
Compare:
● Sky → same
● AirTemp → same
● Humidity → different → generalize → ?
● Wind → same
H = [Sunny, Warm, ?, Strong]
Step 4: Third example (Negative → Ignore)
Step 5: Fourth positive example
(Sunny, Warm, High, Strong)
Compare with H:
No change needed
✅ Final Hypothesis:
H = [Sunny, Warm, ?, Strong]
🔮 Prediction:
Condition: {Sunny, ?, Normal, ?}
Compare with H:
● Sky = Sunny ✔
● AirTemp = ? ✔
● Humidity = ? ✔
● Wind = ? ✔
👉 Prediction = YES (Play Tennis)
🔵 PART 2 – HEART DATASET + FIND-S
🧠 Tasks Overview:
1. Load dataset
2. Detect data types
3. Remove columns
4. Handle missing values
5. Convert to binary
6. Apply Find-S
7. Test new example
📥 Load Dataset
import pandas as pd
df = pd.read_csv('[Link]')
🔍 Detect Data Types
print([Link])
❌ Drop Columns
df = [Link](['ChestPain', 'Thal'], axis=1)
🧼 Handle Missing Values (Median)
df = [Link]([Link](numeric_only=True))
🔄 Convert to Binary
Concept:
● Convert numerical → binary using threshold
for col in [Link]:
if df[col].dtype != 'object':
threshold = df[col].median()
df[col] = (df[col] > threshold).astype(int)
🎯 Select 5 Examples
data = [Link][:5]
X = [Link]('AHD', axis=1).values
y = data['AHD'].values
⚙️ Find-S Implementation
def find_s(X, y):
hypothesis = None
for i in range(len(X)):
if y[i] == 1: # positive
if hypothesis is None:
hypothesis = X[i].copy()
else:
for j in range(len(hypothesis)):
if hypothesis[j] != X[i][j]:
hypothesis[j] = '?'
return hypothesis
h = find_s(X, y)
print(h)
🔮 Testing New Example
Example:
<Age=high, Sex=?, RestBP=low, ...>
Convert to binary same way
def predict(h, x):
for i in range(len(h)):
if h[i] != '?' and h[i] != x[i]:
return "No"
return "Yes"
📌 FINAL NOTES
Find-S Rules:
● Start specific
● Generalize only for positive examples
● Ignore negative examples
Binary Conversion:
● Use median threshold
Important:
● Keep consistency in preprocessing
⚠️ COMMON MISTAKES
❌
❌
● Using negative examples
❌
● Wrong threshold
❌
● Not converting test data same way
● Confusing '?' meaning
🎯 YOU SHOULD NOW KNOW
● What hypothesis means
● How Find-S works
● Data preprocessing steps
● Binary transformation logic
🚀 NEXT
I can help you with:
🔥
● Step-by-step dry run of Find-S (very important)
● Viva questions
● Visual intuition of hypothesis space
Assignment 3 – Candidate Elimination
(FULL EXPLANATION)
🔵 CORE IDEA – VERSION SPACE
🧠 What is Version Space?
Version Space = Set of all hypotheses consistent with training data
Represented by:
● S boundary → Most specific hypotheses
● G boundary → Most general hypotheses
🔵 DATASET
x1 = +
x2 = +
x3 = -
x4 = +
🔵 INITIALIZATION
S0 = {<Ø, Ø, Ø, Ø, Ø, Ø>}
G0 = {}
🔵 STEP-BY-STEP EXECUTION
✅ Step 1: x1 (Positive)
S =
G=
✅ Step 2: x2 (Positive)
Compare with S:
Humidity differs → ?
S = <Sunny Warm ? Strong Warm Same>
G unchanged
❌ Step 3: x3 (Negative)
Specialize G to exclude x3
G becomes:
<Sunny ? ? ? ? ?>
Remove overly specific
✅ Step 4: x4 (Positive)
Generalize S:
6th attribute differs → ?
5th attribute differs → ?
S = <Sunny Warm ? Strong ? ?>
Remove inconsistent G members
✅ FINAL BOUNDARIES
S Boundary:
<Sunny Warm ? Strong ? ?>
G Boundary:
<Sunny ? ? ? ? ?>
🔵 VERSION SPACE
All hypotheses between S and G
Example hypotheses:
● <Sunny Warm ? Strong ? ?>
● <Sunny Warm ? ? ? ?>
● <Sunny ? ? Strong ? ?>
🔵 PREDICTIONS
Test 1:
Matches S → YES
Test 2:
Fails S (Cold ≠ Warm) → NO
Confidence:
Higher if matches both S and G
🔵 LIST-THEN-ELIMINATE
Concept:
● List all hypotheses
● Remove inconsistent ones
Steps:
1. Generate all hypotheses
2. Remove those inconsistent with training data
🔵 COMPARISON
Method Efficienc
y
Candidate Elimination Fast
List-Then-Eliminate Very Slow
🔵 KEY DIFFERENCE
Candidate Elimination:
● Maintains boundaries
● Efficient
List-Then-Eliminate:
● Checks entire hypothesis space
● Not scalable
📌 FINAL NOTES
● S → most specific
● G → most general
● Version space = between S and G
● Positive → generalize S
● Negative → specialize G
⚠️ COMMON MISTAKES
● Wrong generalization
● Not removing inconsistent hypotheses
● Confusing S and G updates
🎯 YOU SHOULD KNOW
● How CE works step-by-step
● How to update S and G
● Difference vs Find-S
● Version space meaning
🚀 NEXT
I can help you with:
● FULL DRY RUN (exam style)
● Visualization of S & G movement
● Viva questions (VERY important)
Assignment 4 – ID3 Algorithm (FULL
MASTERY GUIDE)
🔵 CORE CONCEPT – DECISION TREE
(ID3)
🧠 What is ID3?
👉
ID3 (Iterative Dichotomiser 3) is used to build a decision tree using:
Information Gain (IG)
Goal:
● Choose attribute that gives BEST split
● That means → maximum Information Gain
🔵 STEP 1 – ENTROPY (VERY
IMPORTANT)
Formula:
Entropy(S) = -p₁ log₂(p₁) - p₂ log₂(p₂)
Where:
● p₁ = probability of Yes
● p₂ = probability of No
From dataset (given in image):
Total = 14
Yes = 9
No = 5
Entropy(S):
= -(9/14)log₂(9/14) - (5/14)log₂(5/14)
≈ 0.94
🔵 STEP 2 – INFORMATION GAIN
Formula:
IG(S, A) = Entropy(S) - Σ (|Sv|/|S|) × Entropy(Sv)
🔵 STEP 3 – CALCULATE FOR
ATTRIBUTES
1️⃣ Outlook
Values:
● Sunny (5: Yes=2, No=3)
● Overcast (4: Yes=4, No=0)
● Rain (5: Yes=3, No=2)
Entropy:
● Sunny ≈ 0.97
● Overcast = 0
● Rain ≈ 0.97
Weighted entropy:
= (5/14)(0.97) + (4/14)(0) + (5/14)(0.97)
≈ 0.693
IG(Outlook):
= 0.94 - 0.693
≈ 0.247
2️⃣ Temperature
Values:
● Hot, Mild, Cool (calculate similarly)
IG(Temperature) ≈ 0.029
3️⃣ Humidity
IG(Humidity) ≈ 0.151
4️⃣ Wind
IG(Wind) ≈ 0.048
🔵 STEP 4 – ROOT NODE
👉 Highest IG = Outlook
So root = Outlook
🔵 STEP 5 – NEXT LEVEL
For Outlook = Sunny
Split using Humidity
For Outlook = Rain
Split using Wind
For Outlook = Overcast
Direct YES (pure)
🔵 FINAL DECISION TREE
Outlook?
│
├── Sunny → Humidity?
│ ├── High → No
│ └── Normal → Yes
│
├── Overcast → Yes
│
└── Rain → Wind?
├── Strong → No
└── Weak → Yes
🔵 IF-THEN RULES
1. IF Outlook = Sunny AND Humidity = Normal → Yes
2. IF Outlook = Sunny AND Humidity = High → No
3. IF Outlook = Overcast → Yes
4. IF Outlook = Rain AND Wind = Weak → Yes
5. IF Outlook = Rain AND Wind = Strong → No
🔵 ARRAYS (FROM QUESTION)
OutlookArray = {3 2 2 3 4 0}
TemperatureArray = {2 2 3 1 4 2}
These represent encoded values of categorical features
🔵 CODE (OPTIONAL BUT USEFUL)
from [Link] import DecisionTreeClassifier
import pandas as pd
# load data
# encode categorical values
model = DecisionTreeClassifier(criterion='entropy')
[Link](X, y)
📌 FINAL NOTES
● Entropy = impurity measure
● IG = reduction in impurity
● Highest IG = best split
● Tree grows recursively
⚠️ COMMON MISTAKES
● Wrong entropy calculation
● Not weighting entropy
● Picking wrong root
● Not splitting correctly
🎯 YOU SHOULD KNOW
● Entropy formula
● IG formula
● How to build tree manually
● How to interpret tree
🚀 NEXT
I can help you with:
● FULL step-by-step calculation (line by line)
🔥
● Short tricks to solve IG quickly
● Viva questions
Assignment 5 – Randomized Decision
Trees (FULL GUIDE)
🔵 CORE IDEA
This assignment is NOT normal ID3.
👉
It is introducing you to a concept similar to:
Random Forest (very important ML algorithm)
🧠 WHAT IS HAPPENING HERE?
Instead of always choosing BEST attribute:
● Take top 3 attributes (based on Information Gain)
● Randomly choose one among them
👉 This introduces randomness
👉 Prevents overfitting
🔵 STEP-BY-STEP PROCESS
1. Take 10 random samples
data_sample = [Link](n=10, random_state=42)
2. Compute Information Gain for all attributes
Same as Assignment 4
3. Select Top 3 attributes
sorted_attrs = sorted([Link](), key=lambda x: x[1], reverse=True)
top3 = [attr for attr, _ in sorted_attrs[:3]]
4. Choose Random Root
import random
root = [Link](top3)
5. Recursively build tree
Repeat:
● Calculate IG
● Pick top 3
● Choose randomly
6. Build 5 Trees
trees = []
for i in range(5):
tree = build_tree_random(data_sample)
[Link](tree)
🔵 TREE STRUCTURE (EXAMPLE)
Tree 1:
Outlook → Humidity → Yes/No
Tree 2:
Temperature → Wind → Yes/No
Tree 3:
Humidity → Outlook → Yes/No
(Each tree different due to randomness)
🔵 PREDICTION
Input:
Outlook = Sunny
Temperature = Cool
Humidity = High
Wind = Weak
Each tree gives output:
Tree 1 → No
Tree 2 → Yes
Tree 3 → No
Tree 4 → Yes
Tree 5 → No
FINAL OUTPUT (Voting)
Majority:
No = 3
Yes = 2
👉 Final Prediction = NO
🔵 WHY THIS METHOD?
Problem with single tree:
● Can overfit
Solution:
● Use multiple trees
● Combine results
👉 This is exactly how Random Forest works
🔵 COMPLETE PYTHON CODE
import pandas as pd
import random
# Load dataset
df = pd.read_csv('[Link]')
# Remove Day column
df = [Link]('Day', axis=1)
# Function to calculate entropy
import math
def entropy(col):
values = col.value_counts(normalize=True)
return -sum(p * math.log2(p) for p in values)
# Information Gain
def information_gain(df, attr, target='PlayTennis'):
total_entropy = entropy(df[target])
values = df[attr].unique()
weighted_entropy = 0
for v in values:
subset = df[df[attr] == v]
weighted_entropy += (len(subset)/len(df)) * entropy(subset[target])
return total_entropy - weighted_entropy
# Random tree builder
def build_tree_random(df):
if len(df['PlayTennis'].unique()) == 1:
return df['PlayTennis'].iloc[0]
IG = {col: information_gain(df, col) for col in [Link] if col != 'PlayTennis'}
top3 = sorted(IG, key=[Link], reverse=True)[:3]
root = [Link](top3)
tree = {root: {}}
for value in df[root].unique():
subset = df[df[root] == value]
if [Link]:
tree[root][value] = df['PlayTennis'].mode()[0]
else:
tree[root][value] = build_tree_random([Link](root, axis=1))
return tree
# Build 5 trees
trees = [build_tree_random([Link](10)) for _ in range(5)]
# Prediction function
def predict(tree, sample):
if not isinstance(tree, dict):
return tree
root = next(iter(tree))
value = sample[root]
return predict(tree[root].get(value, 'No'), sample)
# Test sample
sample = {'Outlook':'Sunny','Temperature':'Cool','Humidity':'High','Wind':'Weak'}
results = [predict(t, sample) for t in trees]
print(results)
📌 FINAL NOTES
● Random selection improves generalization
● Multiple trees = better accuracy
● Majority voting used
⚠️ COMMON MISTAKES
● Not randomizing properly
● Using full dataset instead of sample
● Not recalculating IG at each level
🎯 YOU SHOULD KNOW
● ID3 vs Random Trees
● Information Gain
● Ensemble learning basics
🚀 NEXT
I can help you with:
● Drawing 5 trees (exactly like assignment)
● Step-by-step IG calculation for random sample
● Viva questions (Random Forest, ID3)
Assignment 6 – Gradient Descent (FULL
MASTER GUIDE)
🔵 CORE IDEA – LINEAR REGRESSION
🧠 Problem:
We want to find best fitting line:
h(x) = θ₀ + θ₁x
This predicts profit based on population
🔵 COST FUNCTION (VERY IMPORTANT)
Mean Squared Error (Half):
J(θ₀, θ₁) = (1/2m) Σ (h(x) - y)²
👉
Goal:
Minimize this cost
🔵 GRADIENT DESCENT
Idea:
● Start with random θ₀, θ₁
● Move step by step to minimum error
Update Rule:
θ₀ = θ₀ - α * (1/m) Σ(h(x) - y)
θ₁ = θ₁ - α * (1/m) Σ(h(x) - y)x
Where:
● α = learning rate
🔵 TYPES OF GRADIENT DESCENT
1️⃣ Batch Gradient Descent
● Uses ALL data points
● Stable but slower
2️⃣ Stochastic Gradient Descent (SGD)
● Uses ONE data point at a time
● Faster but noisy
🔵 LEARNING RATE EFFECT
α Value Effect
Too small Slow learning
Too large Diverges ❌
✅
Good value Fast convergence
🔵 COMPLETE PYTHON CODE
import numpy as np
import [Link] as plt
# Load data
data = [Link]('[Link]', delimiter=',')
X = data[:,0]
y = data[:,1]
m = len(y)
# Initialize parameters
theta0 = 0
theta1 = 0
# Learning rate
alpha = 0.01
iterations = 100
# Gradient Descent
for i in range(iterations):
h = theta0 + theta1 * X
error = h - y
theta0 -= alpha * (1/m) * [Link](error)
theta1 -= alpha * (1/m) * [Link](error * X)
# Plot
[Link](X, y)
[Link](X, theta0 + theta1*X)
[Link]()
🔵 STOCHASTIC GRADIENT DESCENT
for i in range(iterations):
for j in range(m):
h = theta0 + theta1 * X[j]
error = h - y[j]
theta0 -= alpha * error
theta1 -= alpha * error * X[j]
🔵 EXPERIMENTS REQUIRED
1. Learning Rates
● 0.001 → slow
● 0.05 → good
● 0.1 → may diverge
2. Initial Values
Try:
● θ₀ = 0, θ₁ = 0
● θ₀ = 1, θ₁ = 1
● θ₀ = -1, θ₁ = 2
3. Plot each iteration
[Link](X, theta0 + theta1*X)
🔵 PREDICTION
For new population x:
h(x) = θ₀ + θ₁x
📌 FINAL NOTES
● Gradient descent finds minimum cost
● Learning rate controls speed
● Batch vs SGD difference important
⚠️ COMMON MISTAKES
❌
❌
● Wrong update formula
● Too large learning rate
● Not normalizing data (optional but helpful)
🎯 YOU SHOULD KNOW
● Cost function meaning
● Update rules
● Difference between GD types
● Convergence behavior
🚀 NEXT
I can help you with:
● Graph visualization per iteration
🔥
● Step-by-step manual calculation
● Viva questions
Assignment 7 – CNN (Convolutional
Neural Network) – FULL GUIDE
🔵 CORE IDEA – CNN (VERY
IMPORTANT)
🧠 What is CNN?
CNN = Neural network for images
👉 It automatically learns features like:
● Edges
● Shapes
● Textures
🔵 HOW CNN WORKS (INTUITION)
1. Convolution Layer
● Applies filters
● Detects patterns
2. Activation (ReLU)
● Adds non-linearity
3. Pooling
● Reduces size
● Keeps important info
4. Flatten
● Converts to vector
5. Dense Layer
● Final classification
🔵 ARCHITECTURE (AS PER QUESTION)
● Input Layer
● Conv2D (Layer 1)
● Conv2D (Layer 2)
● Average Pooling
● Dropout
● Dropout
● Dense (Sigmoid)
🔵 DATASET
From Kaggle (blood cancer detection)
👉 8 classes → multi-class classification
🔵 DATA PREPROCESSING
from [Link] import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)
train_data = train_datagen.flow_from_directory(
'dataset/',
target_size=(224,224),
batch_size=32,
class_mode='categorical',
subset='training'
)
val_data = train_datagen.flow_from_directory(
'dataset/',
target_size=(224,224),
batch_size=32,
class_mode='categorical',
subset='validation'
)
🔵 MODEL BUILDING
from [Link] import Sequential
from [Link] import Conv2D, AveragePooling2D, Dropout, Flatten, Dense
model = Sequential([
Conv2D(32, (3,3), activation='relu', input_shape=(224,224,3)),
Conv2D(64, (3,3), activation='relu'),
AveragePooling2D(pool_size=(2,2)),
Dropout(0.25),
Dropout(0.25),
Flatten(),
Dense(8, activation='softmax')
])
🔵 COMPILATION
[Link](
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy']
)
🔵 TRAINING
history = [Link](
train_data,
validation_data=val_data,
epochs=10
)
🔵 TESTING (CONFUSION MATRIX)
from [Link] import confusion_matrix
import numpy as np
predictions = [Link](val_data)
y_pred = [Link](predictions, axis=1)
y_true = val_data.classes
cm = confusion_matrix(y_true, y_pred)
print(cm)
🔵 ACTIVATION FUNCTIONS
ReLU
● f(x) = max(0, x)
Sigmoid
● Output between 0 and 1
Softmax
● Multi-class probability
🔵 IMPORTANT NOTE
👉 Question says Sigmoid, but since 8 classes:
Use SOFTMAX (correct approach)
🔵 DATA SPLIT
● Train → 70%
● Validation → 20%
● Test → 10%
📌 FINAL NOTES
● CNN = automatic feature extractor
● Convolution = pattern detection
● Pooling = size reduction
● Dropout = prevents overfitting
⚠️ COMMON MISTAKES
❌
❌
● Using sigmoid for multi-class
❌
● Not normalizing images
❌
● Wrong input shape
● Not splitting data correctly
🎯 YOU SHOULD KNOW
● CNN architecture
● Why convolution works
● Activation functions
● Confusion matrix meaning
🚀 NEXT
I can help you with:
● Running this on Google Colab step-by-step
● Improving accuracy
● Viva questions (VERY important)
Assignment 8 – Clustering (K-Means +
Expectation Maximization)
🔵 CORE IDEA – UNSUPERVISED
LEARNING
🧠 What is Clustering?
Clustering = grouping similar data points
👉 No labels given (unlike previous assignments)
🔵 PART 1 – K-MEANS CLUSTERING
🧠 Goal:
Divide data into k groups (k=2 here)
⚙️ ALGORITHM STEPS
1. Initialize k centroids randomly
2. Assign each point to nearest centroid
3. Update centroid (mean of cluster)
4. Repeat until convergence
🔵 DISTANCE MEASURES
1️⃣ Euclidean Distance
Distance between two points
Formula:
√[(x1-x2)² + (y1-y2)² + (z1-z2)²]
2️⃣ Cosine Similarity
Measures angle between vectors
Formula:
cos(θ) = (A·B) / (|A||B|)
👉 Higher = more similar
🔵 PYTHON CODE (K-MEANS)
import numpy as np
from [Link] import KMeans
# Load data
data = [Link]('[Link]', delimiter=',')
# KMeans (Euclidean)
kmeans = KMeans(n_clusters=2)
[Link](data)
labels = kmeans.labels_
centroids = kmeans.cluster_centers_
print(centroids)
🔵 COSINE SIMILARITY VERSION
from [Link] import cosine_similarity
# Normalize data first
from [Link] import normalize
data_norm = normalize(data)
kmeans_cos = KMeans(n_clusters=2)
kmeans_cos.fit(data_norm)
🔵 VISUALIZATION
import [Link] as plt
[Link](data[:,0], data[:,1], c=labels)
[Link]()
🔵 PART 2 – EXPECTATION
MAXIMIZATION (EM)
🧠 Idea:
Instead of hard clustering → soft clustering
Each point has probability of belonging to each cluster
⚙️ STEPS
1. Initialize means, covariance
2. E-step → calculate probabilities
3. M-step → update parameters
4. Repeat
🔵 PYTHON CODE (EM)
from [Link] import GaussianMixture
gmm = GaussianMixture(n_components=2)
[Link](data)
labels = [Link](data)
means = gmm.means_
print(means)
🔵 DIFFERENCE
K-Means EM
Hard clustering Soft clustering
Uses mean Uses mean +
only variance
Faster More accurate
📌 FINAL NOTES
● K-Means → simple & fast
● EM → probabilistic
● Distance matters a lot
⚠️ COMMON MISTAKES
❌
❌
● Not normalizing for cosine
❌
● Wrong k value
● Not plotting clusters
🎯 YOU SHOULD KNOW
● K-means steps
● Euclidean vs Cosine
● EM intuition
🚀 NEXT
I can help you with:
● Step-by-step clustering visualization
🔥
● Manual calculation of 1 iteration
● Viva questions
Assignment 9 – Naive Bayes (FULL
MASTER GUIDE)
🔵 CORE IDEA – NAIVE BAYES
🧠 What is Naive Bayes?
👉
Naive Bayes is a probabilistic classifier based on:
Bayes Theorem
📌 Bayes Theorem:
P(C|X) = [P(X|C) * P(C)] / P(X)
Where:
● C = Class (Yes/No)
● X = Features
🔵 NAIVE ASSUMPTION
👉 All features are independent
P(X|C) = P(x1|C) * P(x2|C) * ...
🔵 GIVEN DATASET (FROM IMAGE)
Total = 14
Yes = 9
No = 5
🔵 STEP 1 – PRIOR PROBABILITIES
P(Yes) = 9/14
P(No) = 5/14
🔵 STEP 2 – CONDITIONAL
PROBABILITIES
For input:
(Sunny, Cool, High, Strong)
For YES class:
P(Sunny|Yes) = 2/9
P(Cool|Yes) = 3/9
P(High|Yes) = 3/9
P(Strong|Yes) = 3/9
For NO class:
P(Sunny|No) = 3/5
P(Cool|No) = 1/5
P(High|No) = 4/5
P(Strong|No) = 3/5
🔵 STEP 3 – CALCULATE PROBABILITY
For YES:
P(Yes|X) ∝ (9/14)(2/9)(3/9)(3/9)(3/9)
For NO:
P(No|X) ∝ (5/14)(3/5)(1/5)(4/5)(3/5)
Compare:
If P(Yes|X) > P(No|X) → YES
Else → NO
🔵 PART 2 – DEPENDENCY
INTRODUCED
Now:
Humidity depends on Wind
Given:
P(Humidity=High | Wind=Weak) = 0.4
P(Humidity=High | Wind=Strong) = 0.8
New Probability:
Instead of independent:
P(X|C) = P(Outlook|C)*P(Temp|C)*P(Wind|C)*P(Humidity|Wind)
🔵 UPDATED CALCULATION
Replace:
P(High|C) → P(High|Wind)
🔵 FINAL DECISION
Compare updated probabilities
📌 FINAL NOTES
● Naive Bayes assumes independence
● Works well even if assumption is wrong
● Easy and fast
⚠️ COMMON MISTAKES
❌
❌
● Not calculating prior properly
❌
● Using wrong counts
● Forgetting independence assumption
🎯 YOU SHOULD KNOW
● Bayes theorem
● Conditional probability
● Independence assumption
● Effect of dependency
🚀 NEXT
I can help you with:
● Full numerical calculation (step-by-step)
🔥
● Shortcut tricks for exams
● Viva questions
Assignment 10 – Expectation
Maximization (EM) + Gaussian Mixture
Model (GMM)
🔵 CORE IDEA – EM ALGORITHM
🧠 What is EM?
👉
EM is used when:
Some variables are hidden (latent variables)
Example:
● We don’t know which cluster a point belongs to
🔁 TWO MAIN STEPS
1️⃣ E-Step (Expectation)
● Compute probability (responsibility)
● How likely each point belongs to each cluster
2️⃣ M-Step (Maximization)
● Update parameters:
○ Mean (μ)
○ Covariance (Σ)
○ Weights (π)
🔁 Repeat until convergence
🔵 GAUSSIAN MIXTURE MODEL (GMM)
🧠 Idea:
Data is generated from multiple Gaussian distributions
Each cluster:
● Mean → center
● Covariance → spread
● Weight → importance
🔵 MATHEMATICAL INTUITION
Probability:
P(x) = Σ π_k * N(x | μ_k, Σ_k)
🔵 IMPLEMENTATION (PYTHON)
import pandas as pd
from [Link] import GaussianMixture
from sklearn import datasets
import [Link] as plt
from [Link] import silhouette_score
# Load dataset (Iris)
iris = datasets.load_iris()
X = [Link]
# Model
gmm = GaussianMixture(n_components=3, random_state=42)
[Link](X)
# Predictions
labels = [Link](X)
# Evaluation
score = silhouette_score(X, labels)
print("Silhouette Score:", score)
🔵 VISUALIZATION
[Link](X[:,0], X[:,1], c=labels)
[Link]("Feature 1")
[Link]("Feature 2")
[Link]()
🔵 CONVERGENCE ANALYSIS
🧠 Idea:
Track log-likelihood over iterations
gmm = GaussianMixture(n_components=3, max_iter=100, verbose=2)
[Link](X)
👉 Higher log-likelihood = better fit
🔵 INITIALIZATION IMPACT
Try different initializations:
gmm1 = GaussianMixture(n_components=3, init_params='kmeans')
gmm2 = GaussianMixture(n_components=3, init_params='random')
Compare:
● Speed
● Accuracy
🔵 EVALUATION METRICS
1. Silhouette Score
● Range: -1 to 1
● Higher = better clustering
2. BIC (optional)
● Lower = better
🔵 DIFFERENCE: K-MEANS vs GMM
K-Means GMM
Hard clustering Soft clustering
Spherical Flexible
clusters shapes
Fast More accurate
📌 FINAL NOTES
● EM = iterative optimization
● GMM = probabilistic clustering
● Convergence = stable parameters
⚠️ COMMON MISTAKES
❌
❌
● Not understanding E vs M step
❌
● Ignoring covariance
● Not checking convergence
🎯 YOU SHOULD KNOW
● EM steps clearly
● GMM components
● Convergence behavior
● Evaluation metrics
🚀 NEXT
I can help you with:
● Step-by-step EM iteration example
🔥
● Visualization of Gaussian clusters
● Viva questions