0% found this document useful (0 votes)
6 views48 pages

Machine Learning Complete Book

This document is a comprehensive guide to Machine Learning using Python, aimed at beginners and those struggling to grasp the concepts. It covers fundamental topics such as types of machine learning (supervised, unsupervised, reinforcement), key algorithms, and essential Python libraries, with practical examples and analogies to facilitate understanding. The document also addresses common challenges like overfitting and underfitting, providing insights into evaluation metrics and model training.

Uploaded by

Hidden world
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views48 pages

Machine Learning Complete Book

This document is a comprehensive guide to Machine Learning using Python, aimed at beginners and those struggling to grasp the concepts. It covers fundamental topics such as types of machine learning (supervised, unsupervised, reinforcement), key algorithms, and essential Python libraries, with practical examples and analogies to facilitate understanding. The document also addresses common challenges like overfitting and underfitting, providing insights into evaluation metrics and model training.

Uploaded by

Hidden world
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MACHINE LEARNING

Using Python
A Complete Beginner-to-Expert Guide
If you can understand an email, you can understand Machine Learning.

CAP555 | Course: Machine Learning Using Python


How To Use This Book
This book was written for one person: someone who has been sitting in ML lectures for a
month and nothing is making sense.
Here is what makes this book different:
• Every concept is explained with a real-world analogy FIRST, then the technical
definition.
• Code is shown in plain, runnable Python — no shortcuts.
• Key terms are highlighted and explained the moment they appear.
• Each Unit ends with a quick recap and practice questions.

A NOTE ON MATH

You will see some math in this book. Do NOT panic. For every formula, we explain
what it is doing in plain English first. The math is just a shorter way of saying
something we already understand.
You do not need to memorize formulas. You need to understand what they are trying
to achieve.
UNIT I

Introduction to Machine Learning

Chapter 1: What is Machine Learning?


Let us start with the most important question: What exactly IS Machine Learning?

🧒 ANALOGY: Teaching a Child vs Programming a Computer

Imagine you want to teach a child to recognise dogs. You don't write rules like 'if it
has four legs AND fur AND a tail, it is a dog.' Instead, you show them hundreds of
pictures — 'this is a dog, this is not a dog' — and eventually, they figure it out on their
own. Machine Learning works the same way. Instead of programming rules, we
show the computer thousands of examples and let it find the patterns itself.

The Traditional Way vs The ML Way


Traditional Machine Learning
Programming
You write the rules The computer learns the rules from data
Rules → Computer → Data + Output → Computer → Rules
Output
Example: IF temperature > Example: Show 10,000 patient records, learn what fever looks
38 THEN fever like
Breaks when rules are too Works even for extremely complex patterns
complex
You must anticipate every It handles cases you never thought of
case

Formal Definition: Machine Learning is a field of artificial intelligence that enables computers
to learn from experience (data) and improve their performance on a task without being
explicitly programmed for that task.

Why Do We Need ML?


Some problems are too complex to write rules for:
• Recognising your face in a photo — how would you even begin writing rules for that?
• Translating languages — billions of sentence combinations exist.
• Recommending a YouTube video — depends on 200+ factors about each viewer.
• Detecting spam emails — spammers keep changing their tricks.
For all these problems, instead of writing rules, we collect data and let the machine learn.

Chapter 2: The Three Types of Machine Learning


Think of ML as three different styles of learning. You already know all three — you just didn't
know they had names.

Type 1: Supervised Learning


📚 ANALOGY: Learning with a Teacher

Imagine a student studying with a textbook where every question has the answer at
the back. The student practices: sees the question, guesses, checks the answer,
corrects themselves. Over time they get better. In supervised learning, every training
example has the correct answer (called a 'label') attached to it. The algorithm sees
the input, makes a prediction, compares it to the correct answer, and adjusts.

Examples of Supervised Learning:


• Predicting a house price based on its size and location (input = size/location, output =
price)
• Spam detection (input = email text, output = spam or not spam)
• Medical diagnosis (input = symptoms, output = disease name)

KEY TERMS

Label / Target: The correct answer we are trying to predict.


Features: The input variables the model uses to make predictions.
Training Data: The labelled examples we use to teach the model.

Type 2: Unsupervised Learning


ANALOGY: Sorting Letters Without Reading Them

Imagine you receive 1,000 letters and must sort them into groups, but the letters
have no labels. You start noticing patterns — these 200 are small and thin, these
100 are large and heavy, these 700 are medium. You create groups without anyone
telling you what the groups should be. Unsupervised learning finds hidden patterns
and structures in data without any labels.

Examples of Unsupervised Learning:


• Customer segmentation (grouping customers by buying habits without pre-defined
groups)
• Anomaly detection (finding unusual transactions in bank records)
• Topic modelling (finding what topics appear in thousands of news articles)

Type 3: Reinforcement Learning


🎮 ANALOGY: Learning to Play a Video Game

Imagine learning to play a new video game. Nobody explains the rules. You just
press buttons, and when something good happens you get points, when something
bad happens you lose lives. Over time you figure out which actions lead to good
outcomes. Reinforcement Learning works the same way — an 'agent' takes actions
in an environment, receives rewards or penalties, and learns to take better actions
over time.

Examples of Reinforcement Learning:


• AlphaGo — learned to play the game Go by playing millions of games against itself
• Self-driving cars — learn to steer by getting rewards for staying on the road
• Robot arms — learn to pick up objects through trial and error

Type Has Labels? Real World Example


Supervised YES — every example labelled Email spam filter, house price
predictor
Unsupervised NO — find patterns alone Customer grouping, news topic
finder
Reinforcement NO — learns from rewards Game AI, self-driving cars, robots

Chapter 3: Overfitting, Underfitting & The Bias-Variance


Tradeoff
This is one of the most important concepts in all of ML. Understanding this will help you debug
almost any model.
📝 ANALOGY: The Student Who Memorised vs The Student Who
Understood

Student A memorises every question and answer from past exams word-for-word.
On a new exam with slightly different questions, they fail completely. Student B
understands the concepts, so they do well even on new questions. But Student C
barely studied at all — they don't know enough to answer anything well. In ML:
Student A = Overfitting, Student B = The goal, Student C = Underfitting.

Underfitting
The model is too simple. It hasn't learned enough from the data. It performs badly on BOTH
training data AND new data. Think of a student who barely studied.
• Signs: High error on training data, high error on test data
• Fix: Use a more complex model, add more features, train longer

Overfitting
The model is too complex. It has memorised the training data, including the noise and random
quirks. It performs great on training data but fails on new, unseen data.
• Signs: Very low error on training data, but high error on test data
• Fix: Get more training data, simplify the model, use regularisation (explained in Unit II)

The Bias-Variance Tradeoff (Intuitive)


SIMPLE EXPLANATION

BIAS = How wrong the model is on average. High bias = Underfitting. The model has
made too many simplifying assumptions.
VARIANCE = How much the model's predictions change for different training sets.
High variance = Overfitting. The model is too sensitive to the specific data it was
trained on.
THE TRADEOFF: As you make the model more complex, bias decreases but
variance increases. The goal is to find the sweet spot in the middle.

Chapter 4: Introduction to Python for ML


Python has become the standard language for Machine Learning because it is easy to read,
has powerful libraries, and an enormous community. You don't need to be a Python expert —
you need to know enough to work with data and models.
Essential Libraries
Library What It Does
NumPy Fast mathematical operations on arrays and matrices. The
foundation of all numeric computing in Python.
Pandas Working with tabular data (like Excel spreadsheets) — loading,
cleaning, exploring datasets.
Matplotlib / Seaborn Creating charts, graphs, and visualisations.
scikit-learn Ready-to-use ML algorithms. The main library we use
throughout this course.
TensorFlow / Keras Building deep neural networks (covered in Unit IV).
PyTorch Another deep learning framework, preferred in research (also
Unit IV).

NumPy Basics — Arrays


import numpy as np

# Create an array (like a list, but much faster for math)


arr = [Link]([1, 2, 3, 4, 5])
print(arr) # [1 2 3 4 5]
print(arr * 2) # [2 4 6 8 10] — operates on ALL elements at once!
print([Link]()) # 3.0
print([Link]()) # 1.4142...

# 2D array (matrix)
matrix = [Link]([[1, 2, 3],
[4, 5, 6]])
print([Link]) # (2, 3) — 2 rows, 3 columns

Pandas Basics — Working with Data


import pandas as pd

# Load a dataset (CSV file)


df = pd.read_csv('[Link]')

# Explore the data


print([Link]()) # First 5 rows
print([Link]) # (rows, columns)
print([Link]) # Data type of each column
print([Link]()) # Statistics: mean, min, max, etc.
print([Link]().sum()) # Count missing values per column
# Select a column
ages = df['age']

# Filter rows
young = df[df['age'] < 30]

# Create a new column


df['age_squared'] = df['age'] ** 2

UNIT I RECAP

Machine Learning = letting computers learn patterns from data instead of


programming rules.
Supervised Learning = learning with labelled examples (has correct answers).
Unsupervised Learning = finding patterns in unlabelled data.
Reinforcement Learning = learning from rewards and penalties.
Overfitting = memorising training data (fails on new data).
Underfitting = not learning enough (fails on all data).
Key libraries: NumPy (math), Pandas (data), scikit-learn (ML algorithms).
UNIT II

Supervised Learning

Chapter 5: Regression — Predicting Numbers


Regression means predicting a NUMBER. Examples: predicting a student's marks, a house's
price, tomorrow's temperature.

5.1 Linear Regression


📏 ANALOGY: Drawing a Best-Fit Line

Imagine plotting hours studied vs exam score on a graph. You scatter-plot all your
data points. Then you draw a single straight line that comes as close as possible to
ALL the points. That line is Linear Regression. Given any new 'hours studied' value,
you just look at where that line is and read off the predicted score.

The equation of the line is:

THE LINEAR REGRESSION FORMULA

Predicted Value = (Weight × Input) + Bias


OR in math notation: y = mx + b
Where: m = slope (how much y changes for each unit of x)
b = bias / intercept (value of y when x = 0)
For MULTIPLE inputs: y = w1*x1 + w2*x2 + ... + wn*xn + b
The algorithm LEARNS the best values of w and b from your data.

How does it learn? It uses a method called Gradient Descent — it tries many values of w and
b, measures the error each time, and slowly adjusts in the direction that reduces the error. You
don't need to implement this yourself — scikit-learn handles it.

Linear Regression in Python


from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from [Link] import mean_absolute_error, mean_squared_error,
r2_score
import pandas as pd
import numpy as np

# Load data
df = pd.read_csv('house_prices.csv') # Example dataset
X = df[['size_sqft', 'num_bedrooms', 'age']] # Features (inputs)
y = df['price'] # Target (output we want to
predict)

# Split: 80% for training, 20% for testing


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

# Create and train the model


model = LinearRegression()
[Link](X_train, y_train) # This is where learning happens!

# Make predictions on the test set


y_pred = [Link](X_test)

# Evaluate
print("MAE:", mean_absolute_error(y_test, y_pred)) # Average error in
same units
print("MSE:", mean_squared_error(y_test, y_pred)) # Penalises large
errors more
print("R² Score:", r2_score(y_test, y_pred)) # 1.0 = perfect, 0
= useless

Evaluation Metrics for Regression


Metric What It Means
MAE (Mean Absolute Average of |actual - predicted|. Easy to interpret. 'On average,
Error) my predictions are off by X units.'
MSE (Mean Squared Average of (actual - predicted)². Penalises big mistakes more
Error) than small ones.
RMSE (Root Mean Square root of MSE. Same units as the target — easier to
Squared Error) interpret.
R² Score Fraction of variance explained. 1.0 = perfect model. 0 = no
better than predicting the average every time.

5.2 Decision Tree Regressor


🌳 ANALOGY: Twenty Questions Game

Think of the 20 Questions game. 'Is it an animal? Does it have fur? Is it bigger than a
cat?' By asking yes/no questions in sequence, you narrow down to the answer. A
Decision Tree does exactly this with your data. It asks a series of yes/no questions
about your features and arrives at a prediction.

from [Link] import DecisionTreeRegressor

model = DecisionTreeRegressor(max_depth=5, random_state=42)


# max_depth limits how deep the tree grows — prevents overfitting!
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("R²:", r2_score(y_test, y_pred))

5.3 Random Forest Regressor


🌲🌲🌲 ANALOGY: Asking 100 Experts Instead of One

Would you trust one doctor's opinion on a serious illness, or would you want a
second, third, fourth opinion? A Random Forest builds MANY decision trees (100 by
default), each trained on a slightly different random subset of your data. Each tree
makes a prediction, and the final answer is the average. This is called an 'ensemble'
method — it is almost always better than a single tree.

from [Link] import RandomForestRegressor

model = RandomForestRegressor(n_estimators=100, max_depth=10,


random_state=42)
# n_estimators = number of trees
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("R²:", r2_score(y_test, y_pred))

# Bonus: See which features matter most


import pandas as pd
importances = [Link](model.feature_importances_, index=[Link])
print(importances.sort_values(ascending=False))

Chapter 6: Classification — Predicting Categories


Classification means predicting a CATEGORY. Examples: spam or not spam, which disease,
which digit in a handwritten image.
6.1 Logistic Regression
Despite the name, this is a CLASSIFICATION algorithm, not regression! It predicts
probabilities of belonging to a class (0 or 1, spam or not spam).

HOW IT WORKS

1. It fits a linear equation to the data (like linear regression).


2. It passes the result through a 'sigmoid function' that squeezes any number into a
value between 0 and 1.
3. If the output > 0.5, it predicts class 1 (e.g., spam). Otherwise, class 0 (not spam).
The sigmoid function: Output = 1 / (1 + e^(-x)). All you need to know: it turns any
number into a probability.

from sklearn.linear_model import LogisticRegression


from [Link] import accuracy_score, classification_report

model = LogisticRegression(max_iter=1000)
[Link](X_train, y_train)
y_pred = [Link](X_test)

print("Accuracy:", accuracy_score(y_test, y_pred))


print(classification_report(y_test, y_pred)) # Full breakdown

6.2 k-Nearest Neighbours (k-NN)


ANALOGY: You Are Who Your Neighbours Are

Imagine you move to a new city and want to know which neighbourhood is safest.
You look at the 5 houses closest to you and check their crime history. If 4 out of 5
are safe, you conclude you are likely in a safe area. k-NN does exactly this: to
classify a new data point, it finds the k closest points in the training data and takes a
majority vote.

from [Link] import KNeighborsClassifier


from [Link] import StandardScaler

# IMPORTANT: k-NN is distance-based, so SCALE your features first!


scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)

model = KNeighborsClassifier(n_neighbors=5) # k=5


[Link](X_train_scaled, y_train)
y_pred = [Link](X_test_scaled)
print("Accuracy:", accuracy_score(y_test, y_pred))

KEY POINT

Always scale features before using k-NN, SVM, or any distance-based algorithm! If
one feature goes from 0–1,000,000 and another goes from 0–1, the first will
completely dominate the distance calculation.

6.3 Naïve Bayes


📧 ANALOGY: The World's Simplest Spam Filter

Suppose you have seen 1,000 emails. 200 were spam. Of those 200 spam emails,
180 contained the word 'FREE'. Of 800 non-spam emails, only 20 contained 'FREE'.
Now a new email arrives with the word 'FREE'. What is the probability it is spam?
Naïve Bayes uses this type of probability calculation (called Bayes' Theorem) for
each word/feature and multiplies them together. It calls features 'naïve' because it
assumes features are independent — which is often not true, but it still works
surprisingly well for text classification.

from sklearn.naive_bayes import GaussianNB

model = GaussianNB()
[Link](X_train, y_train)
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

6.4 Decision Trees for Classification


Same concept as the Decision Tree Regressor, but instead of predicting a number, it predicts
a class. At each leaf node, it predicts the majority class of training examples that end up there.

from [Link] import DecisionTreeClassifier

model = DecisionTreeClassifier(max_depth=5, random_state=42)


[Link](X_train, y_train)
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

6.5 Random Forest Classifier


The ensemble version of the decision tree — same idea as Random Forest Regressor, but for
classification. Usually one of the best out-of-the-box classifiers.
from [Link] import RandomForestClassifier

model = RandomForestClassifier(n_estimators=100, random_state=42)


[Link](X_train, y_train)
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))

6.6 Support Vector Machine (SVM)


✂️ ANALOGY: Drawing the Best Dividing Line

Imagine two groups of red and blue dots on a piece of paper. You want to draw a line
that separates them. There are infinitely many lines you could draw. SVM finds the
line (or in higher dimensions, a 'hyperplane') that has the MAXIMUM distance from
the closest dots of each group. Those closest dots are called 'support vectors'.
Maximising this distance (called the 'margin') makes the classifier more robust to
new data.

from [Link] import SVC


from [Link] import StandardScaler

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)

model = SVC(kernel='rbf', C=1.0)


# kernel='rbf' handles non-linear boundaries
# C = regularisation: small C = wider margin but some errors, large C =
strict fit
[Link](X_train_scaled, y_train)
y_pred = [Link](X_test_scaled)
print("Accuracy:", accuracy_score(y_test, y_pred))

Chapter 7: Evaluation Metrics for Classification


Accuracy alone is not enough! If 99% of emails are NOT spam, a model that always says 'not
spam' has 99% accuracy but is completely useless. We need better metrics.

The Confusion Matrix


UNDERSTANDING THE CONFUSION MATRIX

Imagine your model predicts spam vs not-spam. Four things can happen:
TRUE POSITIVE (TP): Model says spam. Actually IS spam. ✓ Correct!
TRUE NEGATIVE (TN): Model says not spam. Actually NOT spam. ✓ Correct!
FALSE POSITIVE (FP): Model says spam. Actually NOT spam. ✗ 'False Alarm'
FALSE NEGATIVE (FN): Model says not spam. Actually IS spam. ✗ 'Missed It!'

Precision, Recall, and F1


Metric Formula & Meaning
Precision TP / (TP + FP). Of all the things I predicted as Positive, how
many actually were? High precision = few false alarms.
Recall (Sensitivity) TP / (TP + FN). Of all actual Positives, how many did I catch?
High recall = few misses.
F1 Score 2 × (Precision × Recall) / (Precision + Recall). Balances both.
Use when you care about both.
ROC-AUC Area under the ROC Curve. Measures how well the model
separates classes across all thresholds. 0.5 = random, 1.0 =
perfect.

WHEN TO USE WHICH METRIC

SPAM DETECTION: Prioritise Precision. (Missing a spam email is okay; sending a


real email to spam is bad.)
CANCER DETECTION: Prioritise Recall. (Missing a cancer case is deadly; a false
alarm just means more tests.)
BALANCED PROBLEM: Use F1 Score or Accuracy.
OVERALL MODEL QUALITY: Use ROC-AUC.

Cross-Validation
🔄 ANALOGY: Multiple Test Exams Instead of One

Instead of evaluating your model on just one test set (which might be lucky or
unlucky), cross-validation splits the data into k parts (e.g., 5). It trains on 4 parts and
tests on 1 part, repeats this 5 times with each part being the test set once, then
averages the results. This gives a more reliable estimate of real-world performance.

from sklearn.model_selection import cross_val_score


model = RandomForestClassifier(n_estimators=100)
scores = cross_val_score(model, X, y, cv=5, scoring='accuracy')
# cv=5 means 5-fold cross-validation

print("Scores:", scores)
print("Mean Accuracy:", [Link]().round(4))
print("Std Dev:", [Link]().round(4))

Regularisation: Preventing Overfitting


Regularisation adds a penalty to the model for being too complex. The idea: if two models
explain the data equally well, prefer the simpler one.

Regularisation Type How It Works


L1 (Lasso) Adds penalty proportional to |weights|. Drives some weights to
exactly zero — effectively removes useless features. Good for
feature selection.
L2 (Ridge) Adds penalty proportional to weights². Shrinks all weights
towards zero but none become exactly zero. Good general-
purpose regularisation.
ElasticNet Combines L1 and L2. Best of both worlds.
from sklearn.linear_model import Ridge, Lasso, LogisticRegression

# Ridge Regression (L2)


ridge = Ridge(alpha=1.0) # alpha controls strength of regularisation

# Lasso Regression (L1)


lasso = Lasso(alpha=0.1)

# Logistic Regression with L2 (default) or L1


lr = LogisticRegression(penalty='l2', C=1.0)
# C = inverse of regularisation strength: smaller C = stronger
regularisation

UNIT II RECAP

Regression = predict a number. Classification = predict a category.


Linear Regression: fits a line through data. Use R², MAE, MSE to evaluate.
Decision Tree: asks yes/no questions. Prone to overfitting — use max_depth.
Random Forest: 100 trees, takes average/majority vote. Almost always better than a
single tree.
Logistic Regression: classifies using probabilities via sigmoid function.
k-NN: classify by majority vote of k nearest neighbours. ALWAYS scale features first.
SVM: finds maximum-margin boundary. ALWAYS scale features first.
Use Precision when false alarms are costly. Use Recall when missing cases is
costly.
Cross-validation gives more reliable evaluation than a single train/test split.
Regularisation (L1/L2) prevents overfitting.
UNIT III

Unsupervised Learning & Feature


Engineering

Chapter 8: Clustering Algorithms


Clustering finds natural groupings in data without any labels. The machine itself discovers the
structure.

8.1 k-Means Clustering


📍 ANALOGY: Finding the Centre of Groups of People

Imagine 100 people scattered in a park. You want to form 3 groups. Start by placing
3 flags randomly. Each person joins the group with the nearest flag. Then move each
flag to the centre of its group. Repeat until nobody changes groups. That is k-Means!

The Algorithm Step-by-Step:


1. Choose k (number of clusters). This is a hyperparameter YOU must set.
2. Randomly initialise k 'centroids' (cluster centres).
3. Assign each data point to the nearest centroid.
4. Move each centroid to the mean of its assigned points.
5. Repeat steps 3–4 until centroids stop moving.

from [Link] import KMeans


import [Link] as plt

# How do we choose k? Use the Elbow Method


inertia = []
K_range = range(1, 11)
for k in K_range:
km = KMeans(n_clusters=k, random_state=42)
[Link](X)
[Link](km.inertia_) # Inertia = sum of distances to nearest
centroid

# Plot and look for the 'elbow'


[Link](K_range, inertia, 'bo-')
[Link]('Number of Clusters k')
[Link]('Inertia')
[Link]('Elbow Method for Optimal k')
[Link]()

# Train the final model


km = KMeans(n_clusters=3, random_state=42)
labels = km.fit_predict(X)
print("Cluster labels:", labels) # Each point gets a cluster number

8.2 Hierarchical Clustering


🌳 ANALOGY: Building a Family Tree

Start with every person as their own group. Find the two most similar people and
merge them. Now find the two most similar groups and merge them. Keep merging
until everyone is in one group. The result is a 'dendrogram' — a tree that shows how
groups were formed. You can cut the tree at any height to get any number of
clusters.

from [Link] import AgglomerativeClustering


from [Link] import dendrogram, linkage
import [Link] as plt

# Plot the dendrogram first to decide where to cut


Z = linkage(X, method='ward') # 'ward' minimises within-cluster variance
[Link](figsize=(10, 5))
dendrogram(Z)
[Link]('Dendrogram — Cut where gap is largest')
[Link]()

# Apply the clustering


model = AgglomerativeClustering(n_clusters=3, linkage='ward')
labels = model.fit_predict(X)

8.3 DBSCAN
🌆 ANALOGY: Finding Neighbourhoods by Population Density

DBSCAN thinks about density. A 'core point' is a data point that has at least 'minPts'
neighbours within radius 'epsilon'. These core points form clusters. Points that are
reachable from a core point join its cluster. Points that are neither core points nor
reachable from any core point are 'noise' (outliers). This is why DBSCAN can find
clusters of ANY shape and automatically detects outliers.
from [Link] import DBSCAN

model = DBSCAN(eps=0.5, min_samples=5)


# eps = neighbourhood radius
# min_samples = minimum points to be a 'core point'
labels = model.fit_predict(X)

# -1 means the point is noise/outlier


print("Number of clusters:", len(set(labels)) - (1 if -1 in labels else 0))
print("Number of noise points:", list(labels).count(-1))

Algorithm How You Set # of Clusters Special Feature


k-Means You must set k beforehand Fast, works well on round/spherical
clusters
Hierarchical You cut the dendrogram Shows cluster hierarchy visually;
no k needed upfront
DBSCAN Automatic (based on density) Finds any shape; detects outliers
automatically

Chapter 9: Dimensionality Reduction


Sometimes your data has hundreds of features. This causes problems: models overfit,
computation is slow, and patterns are hard to visualise. Dimensionality reduction compresses
features while keeping the most important information.

9.1 PCA (Principal Component Analysis)


📸 ANALOGY: Taking a Photo of a 3D Object

Imagine a 3D sculpture. A photo is 2D, but it still captures most of the important
shape. PCA does the same: it finds the directions of maximum variance in your data
(called 'principal components') and projects everything onto a lower-dimensional
space. The first principal component captures the most variance, the second
captures the second most, and so on.

from [Link] import PCA


from [Link] import StandardScaler
import [Link] as plt

# Scale first — PCA is sensitive to scale


scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# How much variance does each component explain?
pca_full = PCA()
pca_full.fit(X_scaled)
explained_variance = pca_full.explained_variance_ratio_.cumsum()
# Plot to decide how many components to keep
[Link](explained_variance)
[Link]('Number of Components')
[Link]('Cumulative Explained Variance')
[Link]()

# Reduce to 2 components for visualisation


pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)

[Link](X_pca[:, 0], X_pca[:, 1], c=y, cmap='viridis')


[Link]('Principal Component 1')
[Link]('Principal Component 2')
[Link]('PCA — 2D Projection')
[Link]()

9.2 t-SNE (t-distributed Stochastic Neighbour Embedding)


PCA vs t-SNE

PCA is LINEAR — it finds straight-line directions of maximum variance. Good for


reducing dimensions while preserving global structure.
t-SNE is NON-LINEAR — it tries to preserve local neighbourhood structure.
EXCELLENT for visualising clusters. However, the positions are not interpretable
(the axes mean nothing), and it is slow on large datasets.
RULE OF THUMB: Use PCA for data preprocessing before ML. Use t-SNE for
visualising clusters.

from [Link] import TSNE

tsne = TSNE(n_components=2, perplexity=30, random_state=42, n_iter=1000)


X_tsne = tsne.fit_transform(X_scaled)

[Link](X_tsne[:, 0], X_tsne[:, 1], c=y, cmap='viridis')


[Link]('t-SNE Projection')
[Link]()

Chapter 10: Feature Engineering


'Garbage in, garbage out.' The quality of your features matters more than the choice of
algorithm. Feature Engineering is the art of transforming raw data into the best possible inputs
for your model.
10.1 Handling Missing Values
import pandas as pd
from [Link] import SimpleImputer

# See where data is missing


print([Link]().sum())

# Option 1: Drop rows with missing values (only if very few missing)
df_dropped = [Link]()

# Option 2: Fill with mean/median/mode (IMPUTATION)


imputer = SimpleImputer(strategy='median') # 'mean', 'median',
'most_frequent'
X_imputed = imputer.fit_transform(X)

10.2 Feature Scaling


WHY SCALE?

If 'Age' ranges from 18–80 and 'Income' ranges from 10,000–1,000,000, then Income
will dominate any distance calculation. Scaling brings all features to the same range.
StandardScaler: Mean=0, Std=1. Best for most ML algorithms.
MinMaxScaler: Scales to [0,1]. Good for neural networks.
RobustScaler: Uses median and IQR. Best when you have outliers.

from [Link] import StandardScaler, MinMaxScaler,


RobustScaler

# StandardScaler (most common)


scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train) # fit AND transform on
training data
X_test_scaled = [Link](X_test) # ONLY transform on test
data (no fit!)
# CRITICAL: Never fit the scaler on test data — that would 'leak'
information!

10.3 Encoding Categorical Variables


ML algorithms work with numbers, not text. We must convert categories to numbers.

import pandas as pd
from [Link] import LabelEncoder, OneHotEncoder

# Label Encoding — only for ORDINAL categories (e.g., Low < Medium < High)
le = LabelEncoder()
df['size_encoded'] = le.fit_transform(df['size']) #
['Small','Med','Large'] → [0,1,2]

# One-Hot Encoding — for NOMINAL categories (no order, e.g., colours,


cities)
# Creates a new binary column for each category
df_encoded = pd.get_dummies(df, columns=['city'])
# OR with scikit-learn:
ohe = OneHotEncoder(sparse_output=False, drop='first') # drop='first'
avoids redundancy
X_encoded = ohe.fit_transform(df[['city']])

10.4 Outlier Detection


import numpy as np
from scipy import stats

# Method 1: Z-Score method


# Points with |z-score| > 3 are outliers
z_scores = [Link](df['column'])
outliers = df[[Link](z_scores) > 3]

# Method 2: IQR method


Q1 = df['column'].quantile(0.25)
Q3 = df['column'].quantile(0.75)
IQR = Q3 - Q1
lower = Q1 - 1.5 * IQR
upper = Q3 + 1.5 * IQR
outliers = df[(df['column'] < lower) | (df['column'] > upper)]
df_clean = df[(df['column'] >= lower) & (df['column'] <= upper)]

10.5 Feature Selection


Method How It Works
Filter Methods Score features independently (e.g., correlation, chi-squared).
Fast but doesn't consider feature interactions.
Wrapper Methods Try different subsets of features and pick the best (e.g.,
Recursive Feature Elimination). Slower but better.
Embedded Methods Feature selection happens as part of model training (e.g.,
Lasso regression, Random Forest importances). Best of both
worlds.
from sklearn.feature_selection import SelectKBest, f_classif, RFE
from [Link] import RandomForestClassifier

# Filter: Select top k features by ANOVA F-test


selector = SelectKBest(score_func=f_classif, k=10)
X_selected = selector.fit_transform(X_train, y_train)

# Wrapper: Recursive Feature Elimination


rfe = RFE(estimator=RandomForestClassifier(), n_features_to_select=10)
[Link](X_train, y_train)
print("Selected features:", [Link][rfe.support_])

UNIT III RECAP

k-Means: assign points to nearest centroid; requires choosing k upfront.


Hierarchical: builds a dendrogram; no k needed upfront; cut the tree at desired
height.
DBSCAN: density-based; finds any shape; automatically detects outliers.
PCA: linear dimensionality reduction; preserves global variance.
t-SNE: non-linear; excellent for visualising clusters; axes are not interpretable.
Always handle missing values (imputation or dropping).
Always scale features for distance-based and neural network algorithms.
Use One-Hot Encoding for nominal categories, Label Encoding for ordinal.
Feature selection improves model performance and reduces overfitting.
UNIT IV

Neural Networks and Deep Learning

Chapter 11: Building Blocks of Neural Networks

11.1 The Perceptron — The Simplest Neural Network


🧠 ANALOGY: A Single Neuron Making a Decision

Your brain has ~86 billion neurons. Each neuron receives signals from other
neurons, adds them up, and if the total is strong enough, it fires a signal to the next
neurons. A Perceptron is the mathematical model of a single neuron: it takes several
inputs, multiplies each by a weight (importance), adds them up, adds a bias, and
passes the result through an activation function.

THE PERCEPTRON FORMULA

Output = Activation_Function( w1*x1 + w2*x2 + ... + wn*xn + bias )


Where: x1, x2...xn = your input features
w1, w2...wn = weights (learned from data)
bias = an extra offset term
Activation_Function = decides if/how strongly the neuron fires

11.2 Activation Functions


Without an activation function, neural networks would just be linear regression no matter how
many layers you add. Activation functions introduce non-linearity, allowing networks to learn
complex patterns.

Activation Function When & Why to Use


ReLU (Rectified Linear f(x) = max(0, x). Output is 0 for negatives, else x. MOST
Unit) commonly used in hidden layers. Fast to compute, doesn't
suffer from vanishing gradients.
Sigmoid f(x) = 1/(1+e^-x). Squashes to (0,1). Use in OUTPUT layer for
BINARY classification (spam/not spam).
Softmax Converts outputs to probabilities that sum to 1. Use in
OUTPUT layer for MULTI-CLASS classification.
Tanh Similar to sigmoid but range (-1, 1). Sometimes used in RNNs.
Leaky ReLU Like ReLU but allows small negative values. Fixes the 'dying
ReLU' problem.

11.3 MLP — Multi-Layer Perceptron


ARCHITECTURE

An MLP has three types of layers:


INPUT LAYER: One neuron per feature in your dataset.
HIDDEN LAYERS: One or more layers of neurons. These learn the patterns.
OUTPUT LAYER: One neuron per class (classification) or one neuron (regression).
Information flows FORWARD from input to output (this is called 'forward
propagation').
Errors flow BACKWARD to update weights (this is called 'backpropagation').

import tensorflow as tf
from tensorflow import keras
from [Link] import layers

# Build an MLP for binary classification


model = [Link]([
[Link](128, activation='relu', input_shape=(X_train.shape[1],)),
# Hidden layer 1
[Link](0.3), # Dropout: randomly turns off 30% of neurons —
prevents overfitting
[Link](64, activation='relu'), # Hidden layer 2
[Link](0.3),
[Link](1, activation='sigmoid') # Output layer for binary
classification
])

[Link](
optimizer='adam', # Adam is the most commonly used optimizer
loss='binary_crossentropy', # Loss function for binary classification
metrics=['accuracy']
)

[Link]() # Shows all layers and parameter counts

history = [Link](
X_train, y_train,
epochs=50, # How many times to go through the full training
data
batch_size=32, # Process 32 samples at a time
validation_split=0.2, # 20% of training data used for validation
verbose=1
)

11.4 Convolutional Neural Networks (CNN)


ANALOGY: How Your Eyes Process Images

When you look at a face, your eyes don't analyse every pixel independently. They
detect edges first, then shapes, then features like eyes and nose. CNNs work the
same way. 'Convolutional layers' act like filters that scan across an image, detecting
patterns. Early layers detect edges; middle layers detect shapes; deep layers detect
complex features like faces.

Key CNN Components:


• Convolutional Layer: Applies learnable filters across the image. Each filter detects a
specific pattern.
• Pooling Layer: Reduces image size (e.g., take the max value in each 2x2 region).
Makes the network faster and more robust to slight shifts.
• Flatten Layer: Converts the 2D feature maps to a 1D vector for the dense layers.
• Dense Layers: Regular MLP layers for final classification.

from [Link] import layers, models

# CNN for image classification (e.g., MNIST digits: 28x28 grayscale images)
model = [Link]([
layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
# 32 filters, 3x3 kernel size
layers.MaxPooling2D((2,2)), # Reduce from 26x26 to 13x13
layers.Conv2D(64, (3,3), activation='relu'),
layers.MaxPooling2D((2,2)),
[Link](), # Convert to 1D
[Link](128, activation='relu'),
[Link](10, activation='softmax') # 10 classes → 10 outputs
])

[Link](optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
[Link](X_train, y_train, epochs=10, validation_split=0.2)

11.5 Recurrent Neural Networks (RNN, LSTM, GRU)


📖 ANALOGY: Reading a Sentence Word by Word

When reading 'The cat sat on the mat', understanding 'mat' depends on all the
previous words. Regular neural networks process each input independently — they
have no memory. RNNs have a 'memory' that carries information from one step to
the next, making them perfect for sequences: text, time series, speech.

RNN vs LSTM vs GRU

SIMPLE RNN: Has short-term memory. Suffers from 'vanishing gradient' — forgets
long-term dependencies.
LSTM (Long Short-Term Memory): Has a 'cell state' that can carry information for
long sequences. Solves the vanishing gradient problem. Industry standard for
sequences.
GRU (Gated Recurrent Unit): A simplified LSTM. Fewer parameters, trains faster.
Often performs similarly to LSTM.
RULE OF THUMB: Start with LSTM. If too slow, try GRU.

from [Link] import layers, models

# LSTM for sequence classification (e.g., sentiment analysis)


model = [Link]([
[Link](vocab_size, 64, input_length=max_len), # Converts
words to vectors
[Link](128, return_sequences=True), # return_sequences=True for
stacking LSTMs
[Link](64),
[Link](1, activation='sigmoid') # Binary output
])

[Link](optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])

11.6 Transformers & Attention (Introduction)


Transformers are the architecture behind ChatGPT, BERT, and all modern Large Language
Models. They replaced RNNs for most NLP tasks.

THE KEY IDEA: ATTENTION

Instead of processing sequences one-by-one like RNNs, Transformers look at ALL


positions in the sequence simultaneously.
The 'Attention Mechanism' lets each word focus on the most relevant other words in
the sentence.
Example: In 'The animal didn't cross the street because it was too tired', 'it' should
attend to 'animal'. Attention learns this relationship.
Transformers are parallelisable (all positions processed at once) → much faster to
train than RNNs on GPUs.
11.7 Hyperparameter Tuning
Hyperparameter What It Controls
Learning Rate How big each update step is. Too high = overshoots optimal.
Too low = trains forever. Start with 0.001.
Batch Size How many samples processed before updating weights.
Smaller = noisier updates but sometimes better generalisation.
Number of Layers Depth of the network. More layers = more capacity but slower
and harder to train.
Number of Neurons Width of each layer. More neurons = more capacity but more
risk of overfitting.
Dropout Rate Fraction of neurons randomly dropped during training. Typical
values: 0.2–0.5.
Epochs How many full passes through training data. Use early
stopping to avoid overfitting.
from [Link] import EarlyStopping, ReduceLROnPlateau

# Early Stopping: stop training when validation loss stops improving


early_stop = EarlyStopping(monitor='val_loss', patience=5,
restore_best_weights=True)
# patience=5: stop after 5 epochs of no improvement

# Reduce learning rate when learning plateaus


reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3)

[Link](X_train, y_train, epochs=100, callbacks=[early_stop, reduce_lr],


validation_split=0.2)

UNIT IV RECAP

Perceptron = one neuron. Weights + bias + activation function.


Activation functions (ReLU, Sigmoid, Softmax) add non-linearity.
MLP = Input layer + Hidden layers + Output layer. Learns via backpropagation.
CNN = best for images. Convolutional layers detect spatial features.
RNN/LSTM/GRU = best for sequences (text, time series). LSTM handles long
dependencies.
Transformers = modern NLP architecture. Uses Attention to relate all positions at
once.
Dropout prevents overfitting. Early stopping prevents over-training.
Adam is the go-to optimizer. Start with learning rate 0.001.
UNIT V

Explainability, Ethics & ML Deployment

Chapter 12: Explainable AI (XAI)


Building a model that predicts correctly is only half the job. In the real world, people need to
UNDERSTAND why a model made a decision. A bank can't just say 'the AI rejected your loan'
— they need to explain why.

12.1 SHAP (SHapley Additive exPlanations)


💰 ANALOGY: Splitting Credit Among Team Members

Imagine 5 people worked on a project that earned 100,000 rupees. How do you fairly
split the money? You calculate how much each person contributed by checking:
'What would the outcome have been without this person?' SHAP uses the same idea
for ML features. For each prediction, it calculates how much each feature contributed
to the final output, positive or negative.

import shap

# SHAP for tree models (fastest)


explainer = [Link](trained_random_forest)
shap_values = explainer.shap_values(X_test)

# Summary plot: shows which features matter most GLOBALLY


shap.summary_plot(shap_values, X_test, feature_names=feature_names)

# Force plot: explains ONE specific prediction


shap.force_plot(explainer.expected_value, shap_values[0], X_test.iloc[0],
feature_names=feature_names)

12.2 LIME (Local Interpretable Model-agnostic Explanations)


HOW LIME WORKS

LIME explains ONE prediction at a time (local explanation).


It creates fake data points near the input we want to explain.
It trains a SIMPLE (interpretable) model on those nearby points.
The simple model approximates what the complex model does in that local region.
KEY: 'Model-agnostic' means LIME works with ANY model — black box or not.

from lime.lime_tabular import LimeTabularExplainer

explainer = LimeTabularExplainer(
X_train,
feature_names=feature_names,
class_names=['Class 0', 'Class 1'],
mode='classification'
)

# Explain one prediction


exp = explainer.explain_instance(X_test[0], model.predict_proba,
num_features=10)
exp.show_in_notebook() # or exp.as_pyplot_figure()

12.3 Grad-CAM (for CNN Image Models)


Grad-CAM creates a heatmap that highlights WHICH REGIONS of an image caused the
model's decision. Essential for understanding and debugging image classifiers.

HOW GRAD-CAM WORKS

It looks at the gradients (slopes) of the prediction with respect to the last
convolutional layer.
Regions that caused large gradients are highlighted in red/orange.
Regions that had little effect are shown in blue/green.
This shows you 'what part of the image did the model look at to make this
prediction?'

Chapter 13: Ethical and Fair ML


Machine Learning models can and do reflect human biases present in the training data. This is
not just an ethical issue — it has real consequences for real people.

Type of Bias Example


Historical Bias Training a loan-approval model on historical data where
women were denied loans at higher rates → model continues
discriminating.
Representation Bias Training a face recognition model on mostly light-skinned
faces → poor accuracy for dark-skinned faces.
Measurement Bias Using 'number of arrests' as a proxy for 'criminality' — but
arrest rates depend on policing patterns, not just behaviour.
Feedback Loop Recommender systems show content users engage with →
users engage with more extreme content → recommendation
becomes more extreme.

Fairness Metrics
• Demographic Parity: Model should predict positive at equal rates across groups.
• Equal Opportunity: True positive rate should be equal across groups.
• Calibration: Predicted probabilities should match actual rates for all groups.

ETHICAL CHECKLIST FOR ANY ML PROJECT

✓ Who collected the data? Are any groups under-represented?


✓ Are your features proxies for protected attributes (race, gender, religion)?
✓ Who will be affected by this model's decisions?
✓ What happens if the model is wrong? (A missed cancer vs a mis-classified spam
email are very different.)
✓ Is there a human in the loop for high-stakes decisions?
✓ Can affected people appeal or contest the model's decision?

Chapter 14: ML Model Deployment


A model sitting in a Jupyter notebook helps no one. Deployment means making your model
available so real users can get predictions from it.

Step 1: Save the Model


import joblib

# Save the model to disk


[Link](model, 'my_model.pkl')
[Link](scaler, 'my_scaler.pkl') # Save the scaler too!

# Load it back
loaded_model = [Link]('my_model.pkl')
loaded_scaler = [Link]('my_scaler.pkl')
Step 2: Create an API with Flask
from flask import Flask, request, jsonify
import joblib
import numpy as np

app = Flask(__name__)
model = [Link]('my_model.pkl')
scaler = [Link]('my_scaler.pkl')

@[Link]('/predict', methods=['POST'])
def predict():
data = [Link] # Get JSON input from user
features = [Link](data['features']).reshape(1, -1) # Reshape for
model
features_scaled = [Link](features)
prediction = [Link](features_scaled)[0]
probability = model.predict_proba(features_scaled)[0].max()
return jsonify({'prediction': int(prediction), 'probability':
float(probability)})

if __name__ == '__main__':
[Link](debug=True, port=5000)

Step 3: Test the API


import requests

# Test your API


response = [Link](
'[Link]
json={'features': [5.1, 3.5, 1.4, 0.2]}
)
print([Link]())
# Output: {'prediction': 0, 'probability': 0.97}

DEPLOYMENT OPTIONS

LOCAL: Run Flask on your own machine. Good for testing only.
CLOUD (Heroku / AWS / GCP / Azure): Deploy to the internet. Anyone can access.
DOCKER: Package your model and all its dependencies into a container. Runs
identically anywhere.
SERVERLESS (AWS Lambda): Pay only when the API is called. Great for low-traffic
models.

UNIT V RECAP
SHAP: explains which features contribute to each prediction. Game theory approach.
LIME: local explanations for any model. Trains a simple model nearby the point.
Grad-CAM: highlights which image regions a CNN focused on.
ML models can encode and amplify human biases — always audit for fairness.
Deployment pipeline: Train → Evaluate → Save → Create API → Test → Deploy to
cloud.
Always save the scaler along with the model!
UNIT VI

Generative AI & Emerging Trends

Chapter 15: Generative Models


So far we have been building models that CLASSIFY or PREDICT. Generative models are
different — they can CREATE new data that looks like the training data: new images, new text,
new music.

15.1 Autoencoders
ANALOGY: Compress and Reconstruct

Imagine compressing a 10MB photo to 100KB for sending, then decompressing it on


the other end. An Autoencoder learns to compress data into a small 'bottleneck'
(called the latent space) and then reconstruct it. The bottleneck forces the network to
learn the most important features. Use cases: denoising images, anomaly detection,
feature learning.

from [Link] import layers, models

# Encoder
encoder_input = [Link](shape=(784,)) # For MNIST: 28x28=784
encoded = [Link](128, activation='relu')(encoder_input)
encoded = [Link](32, activation='relu')(encoded) # 32D bottleneck

# Decoder
decoded = [Link](128, activation='relu')(encoded)
decoded = [Link](784, activation='sigmoid')(decoded) # Reconstruct
original

autoencoder = [Link](encoder_input, decoded)


[Link](optimizer='adam', loss='mse')
[Link](X_train, X_train, epochs=50, validation_split=0.2) #
Target IS the input!

15.2 VAE (Variational Autoencoder)


FROM AUTOENCODER TO VAE

A regular Autoencoder maps each input to a POINT in latent space.


A VAE maps each input to a DISTRIBUTION (mean and variance) in latent space.
This means you can SAMPLE from the latent space to generate NEW examples.
The VAE ensures the latent space is smooth and continuous — interpolating
between two points generates meaningful outputs.
Use case: Generate new handwritten digits, new faces, new molecules for drug
discovery.

15.3 GANs (Generative Adversarial Networks)


🎨 vs 🔍 ANALOGY: The Forger and the Detective

Imagine a master art forger trying to create fake paintings, and a detective trying to
catch fakes. The forger gets better at faking, and the detective gets better at spotting
fakes. They push each other to improve. Eventually, the forger is so good that the
detective cannot tell real from fake. A GAN has two networks: the GENERATOR
(forger) creates fake data, and the DISCRIMINATOR (detective) tries to tell real from
fake. They train together in this adversarial game.

# GAN architecture (conceptual code)


import tensorflow as tf
from [Link] import layers

# Generator: takes random noise, outputs a fake image


def build_generator(latent_dim=100):
model = [Link]([
[Link](256, activation='relu', input_shape=(latent_dim,)),
[Link](),
[Link](512, activation='relu'),
[Link](),
[Link](784, activation='tanh') # Output: 28x28 image
])
return model

# Discriminator: takes image, outputs probability it is real


def build_discriminator():
model = [Link]([
[Link](512, activation='relu', input_shape=(784,)),
[Link](256, activation='relu'),
[Link](1, activation='sigmoid') # Real or Fake
])
return model
15.4 Diffusion Models
THE BIG IDEA

Diffusion models are behind Stable Diffusion, DALL-E, and Midjourney.


FORWARD PROCESS: Gradually add Gaussian noise to an image over many steps
until it becomes pure noise.
REVERSE PROCESS: Train a neural network to UNDO the noise, one step at a
time.
GENERATION: Start from pure random noise, apply the denoising network
repeatedly → real image emerges.
This is currently the state-of-the-art for image generation, beating GANs on image
quality and diversity.

Chapter 16: Large Language Models (LLMs)


📱 ANALOGY: Autocomplete on Steroids

Your phone's keyboard predicts the next word as you type. LLMs are trained on
hundreds of billions of words and predict not just the next word but can generate
coherent, knowledgeable paragraphs. They are Transformers scaled to enormous
sizes — GPT-4 has ~1.8 trillion parameters.

Key LLMs you should know about:


• GPT-4 / ChatGPT (OpenAI): General purpose. Best for conversation and code
generation.
• Claude (Anthropic): Strong reasoning, very safe. Long context window.
• Gemini (Google): Strong at multimodal tasks (text + images + video).
• LLaMA (Meta): Open source. Run locally on your own hardware.
• BERT: Trained to understand text (not generate it). Great for classification tasks.

Prompting Techniques
Technique Example
Zero-shot Just ask: 'Translate this to French: ...'
Few-shot Give 2-3 examples first, then ask for the same on new input.
Chain-of-Thought Add 'Let's think step by step.' Makes LLMs reason better.
Role Prompting 'You are an expert data scientist. Explain k-means clustering
to a beginner.'
RAG (Retrieval Retrieve relevant documents first, add to prompt. LLM uses
Augmented Generation) them as context.

Chapter 17: Reinforcement Learning (Conceptual)


We introduced RL in Unit I. Here we go deeper into the key concepts.

Term Meaning
Agent The learner/decision-maker (e.g., a robot, a game AI)
Environment Everything the agent interacts with (the game, the real world)
State The agent's current situation (position, health points, etc.)
Action What the agent can do (move left, jump, buy stock)
Reward Feedback signal: +1 for good action, -1 for bad, 0 for neutral
Policy The strategy the agent follows: given a state, which action to
take?
Q-Learning Learns Q-values: the expected future reward for each (state,
action) pair
Deep Q-Network (DQN) Uses a neural network to approximate Q-values. Used by
DeepMind to beat Atari games.

Chapter 18: AutoML & Edge AI

AutoML — Automating the ML Pipeline


AutoML automates the process of selecting the best model, preprocessing steps, and
hyperparameters. It democratises ML — you don't need deep expertise to get a good model.

# Using auto-sklearn (example)


import [Link]

cls = [Link](
time_left_for_this_task=120, # 2 minutes to search
per_run_time_limit=30
)
[Link](X_train, y_train)
y_pred = [Link](X_test)

# OR use Google AutoML, AWS AutoPilot, or [Link]


Edge AI & TinyML
WHAT IS EDGE AI?

Traditional ML: Data is sent to the cloud → model runs on a server → response sent
back.
Edge AI: The model runs DIRECTLY on the device (phone, sensor, microcontroller)
where data is collected.
WHY? Faster responses (no network delay), works offline, privacy (data never
leaves device), lower cost.
TinyML: Running ML on extremely small, low-power hardware (e.g., Arduino,
Raspberry Pi, sensors).
Examples: Voice recognition on AirPods, face unlock on your phone, anomaly
detection on factory sensors.

Chapter 19: Industry Case Studies


Let us see how real companies use ML:

Company / Domain How They Use ML


Netflix / Spotify Collaborative filtering + deep learning for personalised
recommendations. Model learns your taste from your
listening/watching history.
Google Translate Neural Machine Translation using Transformers. Trained on
billions of sentence pairs.
Tesla / Waymo CNN for object detection, RL for driving policy. Cameras +
LiDAR feed real-time data to the model.
Healthcare (PathAI) CNN trained on pathology slides to detect cancer. Matches or
exceeds expert human performance.
Banks (fraud detection) Ensemble models (XGBoost + neural nets) flag unusual
transactions in real-time.
Agriculture Drones with CNNs detect crop disease from aerial images.
Farmers can treat affected areas only.
Amazon Forecasting demand with time-series models (LSTMs +
Temporal Fusion Transformers). Optimises warehouse
stocking.

UNIT VI RECAP

Autoencoders: compress then reconstruct. Used for denoising and feature learning.
VAE: generates new samples by learning a distribution in latent space.
GANs: Generator vs Discriminator — trains to generate realistic fake data.
Diffusion Models: add noise then learn to denoise. Powers Stable Diffusion and
DALL-E.
LLMs: Transformers scaled up. GPT, Claude, Gemini, LLaMA.
Prompting: zero-shot, few-shot, chain-of-thought, RAG.
AutoML automates model selection and hyperparameter tuning.
Edge AI / TinyML: run models on-device for speed, privacy, and offline use.
PRACTICAL EXPERIMENTS
Complete Code for All Lab Practicals

Practical 1: EDA on a Dataset


import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

# Load the Iris dataset as an example


from [Link] import load_iris
iris = load_iris()
df = [Link]([Link], columns=iris.feature_names)
df['target'] = [Link]

# a) First 5 rows
print([Link]())

# b) Basic statistics
print("Mean:
", [Link](numeric_only=True))
print("Median:
", [Link](numeric_only=True))
print("Std Dev:
", [Link](numeric_only=True))

# c) EDA
print("Shape:", [Link])
print("Data types:
", [Link])
print("Missing values:
", [Link]().sum())

# Visualisation: Pairplot
[Link](df, hue='target')
[Link]('Iris Dataset Pairplot', y=1.02)
[Link]()

# Histogram for a numeric column


df['sepal length (cm)'].hist(bins=20, color='steelblue')
[Link]('Distribution of Sepal Length')
[Link]('Sepal Length (cm)')
[Link]('Frequency')
[Link]()
Practical 2: Regression Model
from [Link] import fetch_california_housing
from sklearn.linear_model import LinearRegression
from [Link] import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from [Link] import mean_absolute_error, mean_squared_error,
r2_score
import numpy as np

data = fetch_california_housing()
X, y = [Link], [Link]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,


random_state=42)

for name, model in [("Linear Regression", LinearRegression()),


("Decision Tree", DecisionTreeRegressor(max_depth=5,
random_state=42))]:
[Link](X_train, y_train)
y_pred = [Link](X_test)
print(f"=== {name} ===")
print(f"MAE: {mean_absolute_error(y_test, y_pred):.4f}")
print(f"MSE: {mean_squared_error(y_test, y_pred):.4f}")
print(f"R²: {r2_score(y_test, y_pred):.4f}")
print()

Practical 3: Classification Model


from [Link] import load_breast_cancer
from [Link] import RandomForestClassifier
from [Link] import StandardScaler
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score, classification_report,
confusion_matrix
import seaborn as sns
import [Link] as plt

data = load_breast_cancer()
X, y = [Link], [Link]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)

model = RandomForestClassifier(n_estimators=100, random_state=42)


[Link](X_train_scaled, y_train)
y_pred = [Link](X_test_scaled)
print("Accuracy:", accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred,
target_names=data.target_names))

cm = confusion_matrix(y_test, y_pred)
[Link](cm, annot=True, fmt='d', xticklabels=data.target_names,
yticklabels=data.target_names)
[Link]('Confusion Matrix')
[Link]()

Practical 5: k-Means & Hierarchical Clustering


from [Link] import make_blobs
from [Link] import KMeans, AgglomerativeClustering
from [Link] import silhouette_score
from [Link] import dendrogram, linkage
import [Link] as plt

X, _ = make_blobs(n_samples=300, centers=4, random_state=42)

# k-Means
km = KMeans(n_clusters=4, random_state=42)
km_labels = km.fit_predict(X)
print("k-Means Silhouette Score:", silhouette_score(X, km_labels))

[Link](X[:, 0], X[:, 1], c=km_labels, cmap='viridis', marker='o')


[Link](km.cluster_centers_[:, 0], km.cluster_centers_[:, 1],
marker='X', s=300, c='red')
[Link]('k-Means Clustering')
[Link]()

# Hierarchical
Z = linkage(X, method='ward')
[Link](figsize=(10, 5))
dendrogram(Z, truncate_mode='level', p=5)
[Link]('Hierarchical Clustering Dendrogram')
[Link]()

hier = AgglomerativeClustering(n_clusters=4)
hier_labels = hier.fit_predict(X)
print("Hierarchical Silhouette Score:", silhouette_score(X, hier_labels))

Practical 7: MLP with Keras


from [Link] import load_breast_cancer
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
import tensorflow as tf
from [Link] import layers, models
import [Link] as plt

data = load_breast_cancer()
X, y = [Link], [Link]

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y,
test_size=0.2, random_state=42)

model = [Link]([
[Link](64, activation='relu', input_shape=(X_train.shape[1],)),
[Link](0.3),
[Link](32, activation='relu'),
[Link](0.3),
[Link](1, activation='sigmoid')
])

[Link](optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
history = [Link](X_train, y_train, epochs=50, batch_size=16,
validation_split=0.2, verbose=0)

# Plot training curves


fig, (ax1, ax2) = [Link](1, 2, figsize=(12, 4))
[Link]([Link]['loss'], label='Train Loss')
[Link]([Link]['val_loss'], label='Val Loss')
ax1.set_title('Loss over Epochs')
[Link]()

[Link]([Link]['accuracy'], label='Train Accuracy')


[Link]([Link]['val_accuracy'], label='Val Accuracy')
ax2.set_title('Accuracy over Epochs')
[Link]()
[Link]()

loss, acc = [Link](X_test, y_test, verbose=0)


print(f"Test Accuracy: {acc:.4f}")

Practical 10: Flask API for Model Deployment


# [Link] — Save this file and run: python [Link]

from flask import Flask, request, jsonify


import joblib
import numpy as np
from [Link] import RandomForestClassifier
from [Link] import load_breast_cancer
from [Link] import StandardScaler
from sklearn.model_selection import train_test_split
# Train a model (in production, just load a saved one)
data = load_breast_cancer()
X, y = [Link], [Link]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y,
test_size=0.2, random_state=42)
model = RandomForestClassifier(n_estimators=100, random_state=42)
[Link](X_train, y_train)

[Link](model, '[Link]')
[Link](scaler, '[Link]')

app = Flask(__name__)

@[Link]('/predict', methods=['POST'])
def predict():
try:
data_in = [Link]
features = [Link](data_in['features']).reshape(1, -1)
features_scaled = [Link](features)
pred = int([Link](features_scaled)[0])
prob = float(model.predict_proba(features_scaled)[0].max())
class_name = 'Malignant' if pred == 0 else 'Benign'
return jsonify({'prediction': pred, 'class': class_name,
'confidence': round(prob, 4)})
except Exception as e:
return jsonify({'error': str(e)}), 400

@[Link]('/health', methods=['GET'])
def health():
return jsonify({'status': 'ok'})

if __name__ == '__main__':
[Link](debug=True, port=5000)

# Test with: curl -X POST [Link]


# -H "Content-Type: application/json"
# -d '{"features": [17.99, 10.38, 122.8, 1001.0, 0.1184, ...]}'
EXAM QUICK REFERENCE

All Algorithms at a Glance


Algorithm Type When to Use
Linear Supervised (Regression) Predict a number when
Regression relationship is linear
Decision Tree Supervised (Both) Interpretable model; visualisable
decisions
Random Forest Supervised (Both) Almost always better than a single
tree; great default
Logistic Supervised (Classification) Binary classification; probabilities
Regression needed
k-NN Supervised (Both) Simple baseline; works well on
small datasets
Naïve Bayes Supervised (Classification) Text classification; very fast
SVM Supervised (Classification) High-dimensional data; clear
margin of separation
k-Means Unsupervised (Clustering) When you know how many
clusters you want
Hierarchical Unsupervised (Clustering) When you want to see cluster
hierarchy
DBSCAN Unsupervised (Clustering) Arbitrary shape clusters; outlier
detection
PCA Unsupervised (Dim. Reduction) Preprocessing; reduce features;
speed up training
t-SNE Unsupervised (Dim. Reduction) Visualising high-dimensional data
in 2D
MLP Deep Learning General tabular data with complex
patterns
CNN Deep Learning Images, video, spatial data
LSTM/GRU Deep Learning Sequences: text, time series, audio
Transformer Deep Learning NLP, LLMs, anything requiring
long-range dependencies
Key Formulas
REGRESSION METRICS

MAE = (1/n) × Σ |y_actual - y_predicted|


MSE = (1/n) × Σ (y_actual - y_predicted)²
R² = 1 - (SS_residuals / SS_total) [1.0 = perfect]

CLASSIFICATION METRICS

Accuracy = (TP + TN) / (TP + TN + FP + FN)


Precision = TP / (TP + FP) [How many predicted positives are actually positive]
Recall = TP / (TP + FN) [How many actual positives did we catch]
F1 = 2 × (Precision × Recall) / (Precision + Recall)

The ML Workflow — Every Time


6. Define the Problem: What are you predicting? What type of ML? What metric matters?
7. Collect & Explore Data: Load, display, check shape, missing values, distributions.
8. Preprocess: Handle missing values, encode categoricals, scale features.
9. Feature Engineering: Create new features, select best features.
10. Split Data: 80% train, 20% test. Or use cross-validation.
11. Choose & Train Model: Start simple (Logistic Regression, Random Forest).
12. Evaluate: Use appropriate metrics. Check for overfitting.
13. Improve: Tune hyperparameters, try different algorithms, add more data.
14. Deploy: Save model, create API, test, deploy to cloud.
15. Monitor: Track model performance in production. Retrain when it degrades.

Common Interview Questions


Question Key Points in Your Answer
What is overfitting? Model too complex, memorises training data, fails on new
data. Fix: more data, simpler model, regularisation, dropout.
Precision vs Recall? Precision = few false alarms. Recall = few misses. Use F1
when both matter. Use Recall for medical diagnosis.
Why scale features? Distance-based algorithms (k-NN, SVM, k-Means) are affected
by feature magnitude. Scaling puts all features on equal
footing.
Random Forest vs Random Forest = many trees + averaging = lower variance +
Decision Tree? better generalisation. Decision Tree = one tree = interpretable
but overfits easily.
What is the bias-variance Simple model → high bias, low variance (underfits). Complex
tradeoff? model → low bias, high variance (overfits). Goal: find the
sweet spot.
CNN vs RNN? CNN = spatial data (images). RNN/LSTM = sequential data
(text, time series). Transformers can handle both.
What is attention in A mechanism that lets each position in a sequence focus on
Transformers? relevant other positions, enabling learning of long-range
dependencies.

You now have everything you need.


Good luck! 🚀

You might also like