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

AAM Midterm Notes

This document serves as a study guide for a midterm exam on Analytics and AI for Managers, detailing the exam format and key topics to focus on, including machine learning models, metrics, and data preparation techniques. It outlines the types of analytics, data types, imputation methods, feature scaling, and the fundamentals of machine learning, including supervised and unsupervised learning. The document emphasizes the importance of data quality and preparation in machine learning projects.

Uploaded by

prajnakalpa
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)
4 views48 pages

AAM Midterm Notes

This document serves as a study guide for a midterm exam on Analytics and AI for Managers, detailing the exam format and key topics to focus on, including machine learning models, metrics, and data preparation techniques. It outlines the types of analytics, data types, imputation methods, feature scaling, and the fundamentals of machine learning, including supervised and unsupervised learning. The document emphasizes the importance of data quality and preparation in machine learning projects.

Uploaded by

prajnakalpa
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

Table of Contents

Application of Analytics & AI for Managers — Complete Midterm


Notes
MDI Gurgaon | PGDM Term 1 | Chapters 2–6 (up to K-Means & KNN)

0. HOW TO USE THIS DOCUMENT


Your exam pattern (4 questions, no theory, no Orange, fully application-based):

Q Type What they want Where to study


Q1 Case-based (2–3 Read a business §1, §2, §9 (Model
sub-parts) situation → name Selection
the right ML model Framework)
→ justify why
Q2 Metric calculation Confusion matrix → §6
Accuracy, Precision,
Recall, F1
Q3 Application Interpret §4, §5, §7, §8
(Classification + coefficients, predict
Regression) values, calculate
errors, read a tree
Q4 Recommender Support / §11
System Confidence / Lift, or
similarity-based
recommendation

Excluded (do not waste time): Hyperparameter Tuning, Factors for Poor ML
Performance, White Box vs Black Box, Explainability Remedies, SVM (all forms), all Python-
code steps, Decision Tree with Python.
Study order if short on time: §6 (metrics) → §9 (model selection) → §11 (recommender)
→ §4/§5 → §10 (clustering) → §12 (solved numericals) → §14 (cheatsheet).

1. THE FOUNDATION — Analytics, AI, ML


1.1 The nesting doll
ARTIFICIAL INTELLIGENCE (machines doing "intelligent" things)
└── MACHINE LEARNING (machines learn patterns from data, not
rules)
└── DEEP LEARNING (ML using multi-layer neural networks)
└── GENERATIVE AI (deep learning that creates new
content)

Definition of ML (Langley & Simon): the study of computational methods for improving
performance by mechanising the acquisition of knowledge from experience.
Manager’s version: ML observes a volume of recorded historical data, finds meaningful
patterns in it, and uses those patterns to react to new, unseen data in the future.
Key distinction from traditional programming:

Traditional Programming Machine Learning


Human writes the rules Machine discovers the rules
Input: Data + Rules → Output: Answers Input: Data + Answers → Output: Rules
“If income > 10L AND CIBIL > 750 → Feed 50,000 past loans + outcomes →
approve loan” model learns the cut-offs itself

1.2 The Four Types of Analytics (a classic case-framing question)


Type Question answered Technique Business example
Descriptive What happened? Dashboards, “Sales fell 12% in Q3
mean/median, pivot in the West zone”
tables
Diagnostic Why did it happen? Drill-down, “Because the top 2
correlation, root- distributors
cause churned”
Predictive What may happen? Regression, “Distributor X has
classification, ML 78% churn
probability next
quarter”
Prescriptive What should we do? Optimisation, “Offer X a 4% credit-
simulation, period extension;
recommender expected retention
+31%”
Exam tip: If a case says “the CMO wants to know which customers will leave next
month” → Predictive. If it says “and what offer to give each” → Prescriptive. The
verb tense in the question gives it away.

1.3 Garbage-In, Garbage-Out (GIGO)


No algorithm can rescue bad data. The model’s ceiling is set by data quality, not by
algorithm sophistication. This is why Chapter 2 (data preparation) typically consumes
60–80% of a real project’s time.
2. CHAPTER 2 — DATA PREPARATION & TRANSFORMATION
2.1 Data Types (decides everything downstream)
Can you do
Category Sub-type Description Example maths?
Qualitative / Nominal Named Gender, City, ✘ (only
Categorical categories, no Brand count/mode)
order
Ordinal Ordered Satisfaction Rank/median
categories, (Low/Med/Hig only
unequal gaps h), Ratings 1–5
Quantitative / Interval Ordered, equal Temperature +, − only
Numeric gaps, no true °C, IQ
zero
Ratio Equal gaps, Revenue, Age, +, −, ×, ÷
true zero exists Weight,
Distance

Why it matters: - Target is categorical → Classification problem - Target is continuous →


Regression problem - Predictors that are nominal → must be encoded (one-hot / dummy) -
Predictors on different scales → must be scaled before distance-based algorithms (KNN,
K-Means)

2.2 Data Imputation (handling missing values)


Why not just delete? Deleting rows loses information and can bias the sample (if
missingness is not random). Deleting a column loses a possibly-important predictor.

Approach How When to use Risk


Listwise deletion Drop rows with any Missing < 5% and Sample-size loss,
NA random bias
Mean imputation Fill with column Numeric, roughly Shrinks variance;
mean symmetric, few gaps distorted by outliers
Median imputation Fill with column Numeric, skewed Shrinks variance
median data or outliers
present
Mode imputation Fill with most Categorical Over-represents
frequent variables majority class
Constant / Fill with 0 or a new When missingness Can create fake
“Unknown” label itself is meaningful category
Model-based Predict the missing Missing > 10%, Computationally
(KNN / regression value from other variables correlated heavier
imputation) columns
Remove attribute Drop the whole > 40–50% missing Loses a predictor
Approach How When to use Risk
column
Rule of thumb for the exam: Income has outliers and 8% missing → median.
“Preferred Store” (categorical) missing → mode. “Blood pressure” missing but
correlated with age & BMI → model-based/KNN imputation.
Orange: the Impute widget (Preprocess) — options: Average/Most frequent, Model-based
(kNN/Random Forest), Remove rows, Remove columns, Fixed value.

2.3 Feature Scaling


Why: Distance-based and gradient-based algorithms treat a variable with a bigger numeric
range as “more important.” Income (₹3,00,000) vs Age (35) — Euclidean distance would be
almost 100% driven by income.
Scaling is MANDATORY for: KNN, K-Means, Hierarchical Clustering, SVM, PCA, Neural
Networks. Scaling is NOT needed for: Decision Trees, Random Forest (they split on
thresholds, scale-invariant); plain OLS Linear Regression (coefficients just rescale, though
it helps interpretation).

(a) Min–Max Normalisation → squeezes into [0, 1]


X − Xmin
X n o r m=
Xma x − Xmin

Use when: you need a bounded range; data is not Gaussian. Weakness: extremely sensitive
to outliers.

(b) Z-score Standardisation → mean 0, SD 1


X− μ
Z=
σ
Use when: data is roughly normal; outliers present; algorithm assumes centred data. Output
range: unbounded.
Worked example. Ages: 20, 30, 40, 50, 60. (μ = 40, σ = 14.14 population)

Min-Max =
X (X−20)/(60−20) Z = (X−40)/14.14
20 0.00 −1.41
30 0.25 −0.71
40 0.50 0.00
50 0.75 +0.71
60 1.00 +1.41
2.4 One-Hot Encoding (converting categories to numbers)
A nominal variable City = {Delhi, Mumbai, Chennai} cannot be fed as 1/2/3 — that falsely
implies Chennai (3) > Delhi (1) and that Mumbai is the average of the two.
One-hot: create one binary (0/1) column per category.

City Delhi Mumbai Chennai


Delhi 1 0 0
Mumbai 0 1 0
Chennai 0 0 1

Dummy-variable trap: with k categories you only need k−1 columns in a regression (drop
one as the reference/base category). Keeping all k creates perfect multicollinearity. The
dropped category is absorbed into the intercept.
Other encoding approaches: - Label / Ordinal encoding — Low=1, Med=2, High=3.
Valid only for genuinely ordinal variables. - Binary encoding / Frequency encoding /
Target encoding — used when a nominal variable has very high cardinality (e.g., 5,000
pincodes) and one-hot would explode the feature count.

2.5 Outliers
Definition: an observation that lies abnormally far from the rest of the data.
Types: Point (single odd value), Contextual (odd only in context — ₹50,000 spend is
normal for a wedding, odd on a Tuesday), Collective (a group of points odd together).

Traditional detection methods


1. Box Plot / IQR Method - Q1 = 25th percentile, Q3 = 75th percentile - IQR = Q3 − Q1 -
Lower fence = Q1 − 1.5 × IQR - Upper fence = Q3 + 1.5 × IQR - Anything outside the
fences = outlier.
2. Z-score Method - Compute Z = (X − μ)/σ - |Z| > 3 (sometimes 2.5) → outlier. Works only
for roughly normal data.

ML-based detection methods


Method Idea
Isolation Forest Randomly split the data; outliers get
isolated in fewer splits (shorter path
length)
Local Outlier Factor (LOF) Compares a point’s local density to its
neighbours’ density; low relative density =
outlier
One-Class SVM Learns a boundary around “normal” data;
anything outside = outlier
Method Idea
DBSCAN Density clustering; points in no cluster are
labelled noise

Treatment: delete (only if a genuine data error) · cap/winsorise at the fence value ·
transform (log) · treat separately (in fraud detection the outlier is the signal — never
delete!).
Case-question trap: “A bank finds 0.3% of transactions have unusually high
values.” Do NOT say “remove the outliers.” Say: these outliers are the business
object of interest — build an anomaly-detection model.
Solved IQR numerical. Data: 12, 14, 15, 15, 16, 18, 19, 20, 22, 60 - n = 10. Q1 = value at
25th percentile ≈ 15, Q3 ≈ 20 (using the median-of-halves method: lower half
12,14,15,15,16 → median 15; upper half 18,19,20,22,60 → median 20) - IQR = 20 − 15 = 5 -
Lower fence = 15 − 1.5(5) = 7.5 ; Upper fence = 20 + 1.5(5) = 27.5 - 60 > 27.5 → 60 is an
outlier.

3. CHAPTER 3 — FUNDAMENTALS OF MACHINE LEARNING


3.1 Types of ML
MACHINE LEARNING
┌───────────────┼────────────────┬──────────────────┐
SUPERVISED UNSUPERVISED SEMI-SUPERVISED REINFORCEMENT
(labelled) (unlabelled) (few labels) (reward signal)
│ │
┌────┴────┐ ┌─────┴──────┐
Classification Clustering Association
Regression (K-Means, Rules
Hierarchical) (Apriori)
Dimensionality Reduction (PCA)

Supervised learning: the algorithm is given historical data that contains the outcome (the
target / label / dependent variable). It learns the mapping from inputs → output, then
predicts for new data. - Target categorical (Yes/No, Spam/Ham, Churn/Stay, Jaguar/Ford)
→ Classification - Target continuous (revenue, satisfaction score, BMI, TRP rating) →
Regression
Unsupervised learning: no target attribute. The goal is to explore the data and find
intrinsic structure — natural groupings (clustering) or co-occurrence patterns (association
rules).
Reinforcement learning: an agent takes actions in an environment and learns from
rewards/penalties (dynamic pricing, robotics, game playing).
3.2 Labelled vs Unlabelled Data
• Labelled: each row carries the answer. “These 500 employees were rated Good or
Poor.” → you can train a classifier to predict the rating for a new employee.
• Unlabelled: no answer column. “Here are 500 employees and their attributes.” → the
algorithm must group them itself; you then interpret and name the groups.
This single distinction is the #1 discriminator in the case question. Ask:
“Does the dataset described contain the answer column?” If yes → supervised. If no
→ unsupervised.

3.3 Key Concepts / Vocabulary


Term Meaning
Instance / Record / Observation One row
Feature / Attribute / Independent One input column
Variable (IV) / Predictor
Target / Label / Dependent Variable The column being predicted
(DV)
Feature Engineering Creating better inputs — deriving Age from
DOB, ratios, log transforms, interaction
terms
Feature Selection Keeping only the useful predictors (via
Rank widget scores)
Parameter Learned from the data during training
(regression coefficients, split points)
Hyperparameter Set before training by the analyst (k in KNN,
k in K-Means, tree depth)
Model The learned mapping from features →
target
Training The process of fitting the model to data
Inference / Scoring Applying the trained model to new data

3.4 Data Partitioning (Model Training)


Why partition? If you test a model on the same data it learned from, it will look brilliant
and be useless in production. You need unseen data to get an honest estimate of
performance.

Set Typical share Purpose


Training 60–70% The model learns the
relationship between IVs
and DV
Validation / Holdout 15–20% Tune the model and check
how well it predicts on data
Set Typical share Purpose
it hasn’t learned from
Testing 15–20% Final, untouched check of
real-world accuracy

Common splits: 70:15:15, 60:20:20, or 70:20:10 — the analyst chooses.

3.5 K-Fold Cross-Validation


Idea: split the data into k equal folds. Train on k−1 folds, test on the remaining one. Repeat
k times so that every fold serves once as the test set. Average the k scores.
K = 5:
Iter 1: [TEST][train][train][train][train] → Score₁
Iter 2: [train][TEST][train][train][train] → Score₂
Iter 3: [train][train][TEST][train][train] → Score₃
Iter 4: [train][train][train][TEST][train] → Score₄
Iter 5: [train][train][train][train][TEST] → Score₅
Final performance = (Score₁+...+Score₅)/5

Advantages: every observation is used for both training and testing; far more stable than
one lucky/unlucky split; better use of limited data. Cost: k× the computation. Special case:
Leave-One-Out CV (LOOCV) where k = n. Stratified k-fold: preserves the class ratio in each
fold — essential for imbalanced data.

3.6 Overfitting vs Underfitting (the bias–variance trade-off)


Underfitting Good fit Overfitting
Model complexity Too simple Just right Too complex
Training accuracy Low High Very high (≈100%)
Test accuracy Low High Low ← the giveaway
Error type High bias Balanced High variance
Analogy Student who didn’t Student who Student who
study understood memorised the
concepts answer key
Fixes Add features, more — More data, simplify
complex model, model, prune tree,
train longer cross-validation,
regularisation
Exam signal: “The model scored 98% on training data but 61% on test data.” →
Overfitting. Prescribe: simplify/prune, gather more data, use k-fold CV, apply
regularisation.

3.7 Feature Importance — Rank Widget


Before modelling, score each predictor’s usefulness against the target and drop the weak
ones. Common scoring measures shown in Orange’s Rank widget:
Score Works for Meaning
Information Gain Classification Reduction in entropy
achieved by that feature
Gain Ratio Classification Information Gain
normalised by the feature’s
own entropy — corrects the
bias toward many-valued
attributes
Gini decrease Classification Reduction in Gini impurity
χ² (Chi-square) Categorical Statistical dependence
between feature and target
ReliefF Both Distinguishing power
between near neighbours of
different classes
ANOVA / Univariate Regression Variance in target explained
regression by that feature

Managerial value: fewer features → faster, cheaper, simpler-to-explain models, less


overfitting, and it tells the business which levers actually matter.

4. CHAPTER 4A — LINEAR REGRESSION (Supervised · Regression · White Box)


4.1 What it is
A statistical/ML technique to assess the impact of one or more independent variables on
one continuous dependent variable, and to predict that DV for new data.
Simple Linear Regression:
Y = β0 + β 1 X +ε

Multiple / Multivariate Linear Regression:


Y = β0 + β 1 X 1 + β 2 X 2 +…+ β n X n+ ε

• β₀ (intercept): predicted Y when all X = 0


• βᵢ (slope/coefficient): change in Y for a one-unit increase in Xᵢ, holding all other
variables constant — this “holding constant” phrase is what examiners look for
• ε: error/residual term (what the model can’t explain)
Estimation method: Ordinary Least Squares (OLS) — choose the β’s that minimise the
Sum of Squared Errors, Σ(Yᵢ − Ŷᵢ)².
Formulas for simple regression:
∑ ( X i − X́ )( Y i − Ý ) sY
β 1= =r ⋅ β =Ý − β 1 X́
∑ ( X i − X́ )
2
sX 0

4.2 The Four Assumptions (memorise — direct exam item)


Assumption What it means How to check If violated
1. Linearity / Relationship Scatter plot, Q-Q Transform variables
Normal between X and Y is plot, histogram of (log, sqrt), add
distribution of linear; residuals are residuals polynomial terms
errors normally
distributed
2. Mutual Observations (and Durbin–Watson Use time-series
exclusivity / their errors) are statistic (≈2 is good) models
Independence of independent of each
errors other — no
autocorrelation
3. Constant variance of Residual vs Fitted Log-transform Y,
Homoscedasticity residuals across all plot; Breusch–Pagan weighted least
levels of X (residual test squares
plot shows a
uniform band, not a
cone)
4. No Independent Correlation matrix; Drop one variable,
multicollinearity variables are not VIF > 5 (or 10) = combine them, use
strongly correlated problem PCA/ridge
with each other

Why multicollinearity is dangerous (managerially): the model’s overall prediction may


still be fine, but individual coefficients become unstable and un-interpretable — you can no
longer tell the CMO which lever to pull. E.g., TV spend and total ad spend in the same model.
Heteroscedasticity = the opposite of homoscedasticity (cone-shaped residual plot).
Common when Y has a large range, e.g. predicting house price where expensive houses
have much larger errors.

4.3 Interpreting the output


Suppose an FMCG firm models monthly Sales (₹ lakh):
^
S a l e s=12.5+ 3.2 ( T V S p e n d ) +1.8 ( D i g i t a l S p e n d ) −0.9 ( C o m p e t it o r P r o m o )

Element Interpretation
Intercept 12.5 With zero TV spend, zero digital spend and
no competitor promo, baseline sales =
₹12.5 lakh
β_TV = 3.2 Each additional ₹1 lakh of TV spend
Element Interpretation
increases sales by ₹3.2 lakh, holding
digital spend and competitor promo
constant
β_Digital = 1.8 Each ₹1 lakh of digital raises sales by ₹1.8
lakh, ceteris paribus
β_Comp = −0.9 Each competitor promo event reduces
sales by ₹0.9 lakh
Managerial takeaway ₹1 lakh moved from digital to TV yields +
₹1.4 lakh net — but only within the
observed data range

p-value: if p < 0.05, the coefficient is statistically significant (the variable genuinely affects
Y). If p > 0.05, that predictor is not adding proven value.
Never extrapolate. If the training data has TV spend between ₹2–20 lakh, the
model says nothing reliable about ₹200 lakh.

4.4 Evaluation Metrics for Regression ★★★ (high-probability numerical)


Let Yᵢ = actual, Ŷᵢ = predicted, Ȳ = mean of actuals, n = number of observations.

Metric Formula Units Interpretation


MAE — Mean 1 Same as Y Average size of
∑ ∥ Y i − Y^ i ∥
Absolute Error n error. Robust to
outliers. Easiest to
explain to business.
MSE — Mean 1 2 Y² Penalises large
∑ ( Y i − Y^ i )
Squared Error n errors heavily. Not
directly
interpretable
(squared units).


RMSE — Root Mean 1 Same as Y Same units as Y, but
∑ ( Y i − Y^ i )
2
Squared Error n still punishes big
misses. RMSE ≥
MAE always.
R² — Coefficient of S Sre s ∑ ( Y i − Y^ i0–1
)
2
% of variance in Y
Determination 1− =1− 2 explained by the
S St o t ∑ ( Y − Ý )
i model
Adjusted R²
[
1 − ( 1 − R 2)
n −1
n −k − 1 ) 0–1 R² penalised for the
number of
predictors k. Use
this to compare
models with
different numbers
Metric Formula Units Interpretation
of variables.
MAPE 100
n
∑ ‖ )
Y i − Y^ i
Yi
% Scale-free → lets you
compare across
products/models
sMAPE 100 ∥ Y i − Y^ i ∥ % Symmetric version;
∑ bounded, doesn’t
n ( ∥ Y i ∥+∥ Y^ i ∥ ) /2 explode when Yᵢ → 0

MAPE’s asymmetric-penalty problem (explicitly taught in your deck — likely a short


question): - Actual = 100, Predicted = 0 → error = |100−0|/100 = 100%. Because
predictions can’t go below zero, under-forecast error is capped at 100%. - Actual = 100,
Predicted = 300 → error = |100−300|/100 = 200%. Over-forecasting is unbounded. - ⇒
MAPE penalises over-forecasting more heavily than under-forecasting, so a model
optimised on MAPE will be biased toward under-forecasting. Also undefined when any
actual = 0. - sMAPE fixes this because the denominator averages actual and predicted, so
it cannot explode as Yᵢ → 0 and it treats over/under errors more symmetrically.
RMSE vs MAE — which to use? - Choose RMSE when large errors are disproportionately
costly (stock-out of a life-saving drug, aircraft part demand). - Choose MAE when all errors
cost proportionally and you have outliers you don’t want to dominate the metric. - RMSE
>> MAE signals a few very large errors (outliers) in the predictions.
R² interpretation: R² = 0.82 → 82% of the variation in sales is explained by the predictors in
this model; 18% is due to factors not captured. R² never decreases when you add a
variable, even a useless one — hence Adjusted R².

5. CHAPTER 4B — LOGISTIC REGRESSION (Supervised · Classification · White Box)


5.1 Why not linear regression for a Yes/No target?
If Y ∈ {0,1}, a straight line produces predictions below 0 and above 1 — meaningless as
probabilities. It also violates homoscedasticity and normality of errors. Logistic
regression solves this by predicting the probability of the event, bounded between 0
and 1.

5.2 The mathematics


Sigmoid / logistic function:
1
P ( Y =1 )= − ( β 0+ β 1 X1 +…+ βn X n )
1+e
This produces the characteristic S-shaped curve flattening at 0 and 1.
Odds and the Logit (log-odds) — the linear part:
O d d s=
P
1−P
ln (
P
1− P )
=β 0 + β 1 X 1+ …+ β n X n

Interpreting coefficients — the critical bit: - β is the change in log-odds per unit
increase in X — not directly meaningful to a manager. - e^β = Odds Ratio (OR) — this is
meaningful. - OR > 1 → the variable increases the odds of the event - OR = 1 → no effect -
OR < 1 → the variable decreases the odds - % change in odds = (e^β − 1) × 100
Example. Employee attrition model, β for OverTime = Yes is 0.92. e^0.92 = 2.51 →
employees doing overtime have 2.51× the odds of attrition compared to those who
don’t, holding everything else constant — i.e., +151% odds.
If β for MonthlyIncome (per ₹10,000) = −0.45 → e^(−0.45) = 0.638 → each additional
₹10,000 of income reduces the odds of attrition by (1 − 0.638) = 36.2%.

5.3 The Cut-off / Threshold


The model outputs a probability. To convert to a class you apply a threshold (default 0.5).
Managers must move the threshold based on the cost of errors:

Situation Better threshold Why


Cancer screening / fraud Lower (e.g., 0.3) Missing a true case (FN) is
detection catastrophic → maximise
Recall
Sending an expensive Higher (e.g., 0.7) Wasting spend on non-
premium offer buyers (FP) is costly →
maximise Precision

Lowering the threshold → more predicted positives → Recall ↑, Precision ↓. This trade-off is
exactly what the ROC curve visualises.

5.4 Worked logistic numerical (very exam-likely)


A bank models loan default:
logit ( P ) =−3.5+0.8 ( Num_Prev_Defaults ) +0.02 ( Debt_to_Income_% ) − 0.004 ( CIBIL− 600 )

Applicant: 1 previous default, DTI = 45%, CIBIL = 700.


Step 1 — Compute the logit (z): z = −3.5 + 0.8(1) + 0.02(45) − 0.004(100) z = −3.5 + 0.8 +
0.9 − 0.4 = −2.2
Step 2 — Convert to probability: P = 1 / (1 + e^(2.2)) = 1 / (1 + 9.025) = 1/10.025 =
0.0998 ≈ 9.98%
Step 3 — Classify: at a 0.5 cut-off, 0.0998 < 0.5 → predict NO DEFAULT → approve the
loan.
Step 4 — Interpret a coefficient: e^0.8 = 2.226 → each additional previous default
multiplies the odds of default by 2.23× (+123%), all else equal.

0.1108 ✓)
Step 5 — Odds check: Odds = P/(1−P) = 0.0998/0.9002 = 0.1109. (Cross-check: e^(−2.2) =

5.5 Logistic vs Linear Regression


Linear Regression Logistic Regression
Target Continuous Categorical (usually binary)
Output A number A probability (0–1) → then a
class
Function Straight line S-shaped sigmoid
Estimation Ordinary Least Squares Maximum Likelihood
Estimation
Evaluated by R², RMSE, MAE, MAPE Accuracy, Precision, Recall,
F1, AUC
Assumption of Required Not required
homoscedasticity

6. CLASSIFICATION EVALUATION METRICS ★★★★★ (guaranteed Question 2)


6.1 The Confusion Matrix
Always define which class is “Positive” first — it is the event of interest (churn, fraud,
disease, default, response).

Predicted:
Predicted: Positive Negative Row total
Actual: Positive TP (True Positive) ✔ FN (False Negative) Actual Positives
✘ Type II error
Actual: Negative FP (False Positive) ✘ TN (True Negative) Actual Negatives
Type I error ✔

Plain English: - TP — it was churn, we said churn. Correct catch. - TN — it wasn’t churn,
we said not churn. Correct pass. - FP — False alarm. It wasn’t churn, we said churn. (Type I
error) - FN — Miss. It was churn, we said not churn. (Type II error)
Memory hook: the second word is what the model predicted; “True/False” says whether
the model was right. “False Positive” = model predicted Positive, and it was False.

6.2 The Formulas


Metric Formula Question it answers
Accuracy T P+T N Of everything, what fraction
T P+T N + F P+ F N did we get right?
Metric Formula Question it answers
Precision (Positive TP Of everything we flagged
Predictive Value) T P+ F P positive, how much really
was? (cost of false alarms)
Recall / Sensitivity / True TP Of all the actual positives,
Positive Rate T P+ F N how many did we catch?
(cost of misses)
Specificity / True Negative TN Of all actual negatives, how
Rate T N +F P many did we correctly
clear?
F1 Score P r e c i s i o n × R e c al l Harmonic mean — the

P r e c i s i o n+ R e c a ll single balanced score
Error Rate / F P+ F N How often are we wrong?
= 1 − Accuracy
Misclassification T otal
FPR (False Positive Rate) FP x-axis of ROC curve
= 1 − Specificity
F P+T N

Why the harmonic mean for F1? It punishes imbalance. Precision 1.0 and Recall 0.0 gives
an arithmetic mean of 0.5 but an F1 of 0 — correctly telling you the model is useless.

6.3 The Accuracy Paradox (a classic case-question trap)


A bank’s fraud dataset: 9,900 legitimate, 100 fraudulent transactions. A lazy model predicts
“not fraud” for everything. - Accuracy = 9,900/10,000 = 99% — looks fantastic. - Recall =
0/100 = 0% — it catches zero fraud. Utterly worthless.
Rule: for imbalanced data, never judge on Accuracy. Use Precision, Recall,
F1, and AUC.
Handling imbalance: oversample the minority (SMOTE), undersample the majority, use
class weights, or change the decision threshold.

6.4 Precision vs Recall — which does the business want?


Business scenario Which error hurts more Optimise
Cancer / disease screening FN — sending a sick patient Recall
home
Fraud detection FN — letting fraud through Recall (but too many FPs
annoy genuine customers)
Predictive maintenance on FN — an undetected failure Recall
aircraft
Spam filter FP — a job offer sent to Precision
spam
Expensive marketing offer / FP — wasted spend on Precision
retention discount someone who’d never leave
Business scenario Which error hurts more Optimise
Legal e-discovery, resume FP — wasted senior- Precision
shortlisting for a costly partner time
interview loop
Balanced costs / Both F1
imbalanced classes

Standard exam sentence to write: “Since the cost of a False Negative (an undetected
fraudulent transaction, ~₹X loss) far exceeds the cost of a False Positive (a customer-service
call to verify), the bank should optimise for Recall, accepting a lower Precision, and should
lower the classification threshold below 0.5.”

6.5 FULLY SOLVED CONFUSION MATRIX NUMERICAL


Question. A telecom company builds a churn model on 1,000 test customers. Positive class
= “Churn”. Results:

Predicted No-
Predicted Churn Churn Total
Actual Churn 120 80 200
Actual No- 60 740 800
Churn
Total 180 820 1000

So: TP = 120, FN = 80, FP = 60, TN = 740.


Accuracy = (TP+TN)/Total = (120+740)/1000 = 860/1000 = 0.86 → 86%
Precision = TP/(TP+FP) = 120/(120+60) = 120/180 = 0.667 → 66.7% → Of the 180
customers we flagged as likely to churn, only 67% actually churned; 33% of our retention
budget was wasted.
Recall (Sensitivity) = TP/(TP+FN) = 120/(120+80) = 120/200 = 0.60 → 60% → We
caught only 60% of the customers who actually left; 80 churners walked out unnoticed.
Specificity = TN/(TN+FP) = 740/(740+60) = 740/800 = 0.925 → 92.5%
F1 Score = 2 × (0.667 × 0.60)/(0.667 + 0.60) = 2 × (0.4002/1.267) = 2 × 0.3159 = 0.632 →
63.2%
Error Rate = (60+80)/1000 = 14%
Managerial verdict: Accuracy of 86% is misleading because 80% of the base doesn’t churn
— predicting “no churn” for everyone would already give 80%. The model adds only 6
percentage points. With Recall at 60% the company is missing 40% of its churners.
Recommendation: lower the probability threshold (say to 0.35) to raise Recall, accepting
more false positives, since a retention offer costs ₹500 while losing a customer costs
₹8,000 in lifetime value.
6.6 ROC Curve and AUC
• ROC (Receiver Operating Characteristic) curve plots TPR (Recall) on the y-axis
against FPR (1 − Specificity) on the x-axis, as the classification threshold is swept
from 1 to 0.
• AUC (Area Under the Curve) summarises it in one number.
AUC Meaning
1.0 Perfect classifier
0.9–1.0 Excellent
0.8–0.9 Good
0.7–0.8 Fair
0.5 No better than a coin toss (the
diagonal line)
< 0.5 Worse than random (predictions
are inverted)

Interpretation: AUC = 0.85 means that if you pick one random positive and one random
negative case, there is an 85% chance the model assigns a higher score to the positive one.
Key advantage: AUC is threshold-independent and works well on imbalanced data — so
it’s the best single number for comparing two models.
Lift chart / Gain chart (marketing use): shows how much better than random targeting
your model is when you contact only the top x% of the scored list. “Contacting the top 20%
ranked by the model captures 55% of all responders → lift of 2.75×.”

7. CHAPTER 5 — DECISION TREE (Supervised · Classification & Regression · White


Box)
7.1 Structure
[Root Node: Income > ₹8L?]
/ \
YES NO
| |
[Age > 35?] [Leaf: Will Not Buy]
/ \
YES NO
| |
[Leaf: Buy] [Leaf: Won't Buy]

• Root node — the whole dataset; the first, most informative split
• Internal / decision node — a test on one attribute
• Branch — the outcome of the test
• Leaf / terminal node — the final prediction (a class, or a numeric mean)
• Depth — the longest root-to-leaf path
• Pruning — cutting back branches to prevent overfitting
Biggest managerial advantage: it is a white-box model. You can print the tree and hand
it to a branch manager as a set of IF-THEN rules. No other high-accuracy model explains
itself this well. This is why it dominates regulated domains (credit approval, insurance
underwriting) where you must justify a decision to a customer or regulator.

7.2 How the tree decides where to split


At every node the algorithm evaluates every possible split on every attribute and picks the
one that makes the resulting child nodes as pure as possible (ideally, all one class).

(a) Entropy & Information Gain (ID3 / C4.5)


Entropy = measure of disorder/impurity, ranging 0 (pure) to 1 (perfectly mixed, for 2
classes):
c
E n t r o p y ( S )=− ∑ p i log 2 pi
i=1

• 50/50 split → Entropy = −0.5log₂0.5 − 0.5log₂0.5 = 1.0 (maximum disorder)


• 100/0 split → Entropy = 0 (perfectly pure)
Information Gain = reduction in entropy after the split:

|S v )
I G ( S , A )=E n t r o p y ( S ) − ∑ E n t r o p y (Sv )
v∈A |S )
The attribute with the highest Information Gain becomes the split.
Gain Ratio = IG ÷ SplitInfo(A). It corrects Information Gain’s bias toward attributes with
many distinct values (e.g., “Customer ID” would have perfect IG but zero predictive power).

(b) Gini Impurity (CART — Orange’s default)


c
G in i ( S )=1 − ∑ p2i
i=1

• 50/50 → 1 − (0.25 + 0.25) = 0.5 (maximum for 2 classes)


• 100/0 → 1 − 1 = 0 (pure)
Gini vs Entropy: they almost always pick the same split. Gini is computationally cheaper
(no logarithms), so it’s the default in most tools. Entropy’s max is 1; Gini’s max is 0.5 (for
binary).

(c) For Regression Trees: Variance / MSE Reduction


When the target is continuous, the tree splits to minimise within-node variance (SSE).
The prediction at a leaf is the mean of the training observations in that leaf.
7.3 SOLVED ENTROPY / INFORMATION GAIN NUMERICAL ★
Dataset: 14 customers. Target = “Purchased” (9 Yes, 5 No). Candidate attribute = Income
Level {High, Medium, Low}.

Income Total Yes No


High 5 2 3
Medium 4 4 0
Low 5 3 2

Step 1 — Parent entropy: p(Yes) = 9/14 = 0.643, p(No) = 5/14 = 0.357 E(S) =
−0.643·log₂(0.643) − 0.357·log₂(0.357) = −0.643(−0.637) − 0.357(−1.486) = 0.410 + 0.530
= 0.940
Step 2 — Entropy of each child: - High (2Y, 3N): −(2/5)log₂(0.4) − (3/5)log₂(0.6) =
0.4(1.322) + 0.6(0.737) = 0.529 + 0.442 = 0.971 - Medium (4Y, 0N): pure → 0.000 - Low
(3Y, 2N): same shape as High → 0.971
Step 3 — Weighted child entropy: = (5/14)(0.971) + (4/14)(0.000) + (5/14)(0.971) =
0.347 + 0 + 0.347 = 0.694
Step 4 — Information Gain: IG = 0.940 − 0.694 = 0.246
Step 5 — Gini for the same split (bonus): Parent Gini = 1 − (0.643² + 0.357²) = 1 − (0.413
+ 0.127) = 0.459 High: 1 − (0.4² + 0.6²) = 1 − 0.52 = 0.48 · Medium: 1 − 1 = 0 · Low: 0.48
Weighted = (5/14)(0.48) + (4/14)(0) + (5/14)(0.48) = 0.343 Gini decrease = 0.459 −
0.343 = 0.116
Conclusion: If another attribute (say “Student”) gives IG = 0.151, then Income Level
(0.246) wins and becomes the split at this node.
Handy log₂ values for the exam: log₂(0.1)=−3.322 · log₂(0.2)=−2.322 · log₂(0.25)=−2 ·
log₂(1/3)=−1.585 · log₂(0.4)=−1.322 · log₂(0.5)=−1 · log₂(0.6)=−0.737 · log₂(2/3)=−0.585 ·
log₂(0.75)=−0.415 · log₂(0.8)=−0.322 · log₂(0.9)=−0.152 (Convert any log: log₂(x) =
ln(x)/0.693 = log₁₀(x)/0.3010)

7.4 Hyperparameters (Orange’s Tree widget)


Hyperparameter What it does Effect
Max depth Caps how many levels deep Lower = simpler, less
the tree grows overfitting
Min instances in leaves A leaf must contain at least Prevents leaves built on 1–2
n records noisy rows
Do not split subsets Minimum node size to Pre-pruning
smaller than attempt a split
Limit majority to Stop splitting when a node Stops chasing marginal
is x% pure purity
Binary trees only Forces two-way splits Simpler, deeper trees
Hyperparameter What it does Effect

Pre-pruning (early stopping) = set these limits before growing. Post-pruning = grow the
full tree, then cut back branches that don’t improve validation performance.

7.5 Regression Tree Application (Supply-Chain Disruption example)


A logistics firm predicts Delay (in days) from Supplier Rating, Distance, Port Congestion
Index, Weather Score. - Splits are chosen to minimise variance in delay within each node. -
Root split might be Port Congestion > 7.5? - A leaf containing 40 shipments with
delays averaging 6.2 days → predicted delay for any new shipment landing in that leaf
= 6.2 days - Managerially, the path to a leaf is the root cause narrative: “High port
congestion + supplier rating below 3 + monsoon months → an average 6.2-day delay.”

7.6 Advantages & Disadvantages


✅ Advantages ❌ Disadvantages
Highly interpretable — IF-THEN rules Prone to overfitting if not pruned
No feature scaling needed Unstable — a small data change can
produce a very different tree
Handles numeric + categorical together Greedy: picks locally best split, not globally
optimal
Handles non-linear relationships Biased toward attributes with many levels
(fix: Gain Ratio)
Missing values handled reasonably Can create biased trees on imbalanced
classes
Implicit feature selection Poor at smooth linear relationships
(creates a staircase)

Ensembles fix instability: Random Forest (many trees on bootstrapped samples +


random feature subsets, majority vote) and Gradient Boosting — accuracy up,
interpretability down.

8. K-NEAREST NEIGHBOURS (KNN) — Supervised · Lazy Learner


8.1 The idea
“You are like your neighbours.” To classify a new point, find the k closest points in the
training data and take a majority vote of their classes. For regression, take the average of
their values.
Called a “lazy learner” because it does no training at all — it just stores the dataset. All
the computation happens at prediction time, which makes it slow to predict on large
datasets. It is also non-parametric (assumes no functional form) and instance-based.
8.2 Algorithm
1. Choose k and a distance metric.
2. Scale/normalise all features (non-negotiable — see §2.3).
3. Compute the distance from the new point to every training point.
4. Sort and take the k nearest.
5. Classification → majority class among the k. Regression → mean of the k.
6. (Optional) Distance-weighted voting — closer neighbours get more weight (1/d²),
which breaks ties and reduces sensitivity to k.

8.3 Choosing k
k too small (k=1) k too large (k=n)
Very sensitive to noise/outliers Over-smoothed, ignores local structure
Overfitting — high variance, low bias Underfitting — high bias, low variance
Jagged decision boundary Boundary collapses toward the majority
class

Rules of thumb: start with k ≈ √n; use an odd k for binary classification to avoid ties;
select k by cross-validation (plot error vs k and pick the minimum).

8.4 SOLVED KNN NUMERICAL ★


Question. A telecom firm wants to predict whether a customer will upgrade to a premium
plan. Training data (already scaled 0–1 for illustration; raw values shown):

Monthly Spend
Cust (₹) Tenure (months) Upgrade?
A 800 24 Yes
B 600 12 No
C 900 30 Yes
D 500 8 No
E 750 20 Yes
F 550 15 No

New customer X: Spend = ₹700, Tenure = 18 months. Use k = 3, Euclidean distance.


Step 1 — Min-Max scale both features (Spend range 500–900; Tenure range 8–30):

Spend’ = Tenure’ =
Cust (S−500)/400 (T−8)/22 Class
A 0.750 0.727 Yes
B 0.250 0.182 No
C 1.000 1.000 Yes
D 0.000 0.000 No
Spend’ = Tenure’ =
Cust (S−500)/400 (T−8)/22 Class
E 0.625 0.545 Yes
F 0.125 0.318 No
X 0.500 0.455 ?

Step 2 — Euclidean distances d = √[(Δspend)² + (Δtenure)²]

Cust Δspend Δtenure d² d Rank


A 0.250 0.272 0.0625+0. 0.369 2
0740=0.1
365
B −0.250 −0.273 0.0625+0. 0.370 3
0745=0.1
370
C 0.500 0.545 0.2500+0. 0.740 6
2970=0.5
470
D −0.500 −0.455 0.2500+0. 0.676 5
2070=0.4
570
E 0.125 0.090 0.0156+0. 0.154 1
0081=0.0
237
F −0.375 −0.137 0.1406+0. 0.399 4
0188=0.1
594

Step 3 — Take k = 3 nearest: E (0.154, Yes), A (0.369, Yes), B (0.370, No)


Step 4 — Majority vote: Yes = 2, No = 1 → Predict: X WILL UPGRADE. Estimated
probability = 2/3 = 0.667
Step 5 — Sanity check on k: With k = 1 the answer is also Yes (E). With k = 5 we’d add F
(No) and D (No) → Yes = 2, No = 3 → prediction flips to No. This demonstrates why k must
be tuned by cross-validation rather than guessed.
Step 6 — What if we hadn’t scaled? Spend ranges over 400 units while tenure ranges
over 22. Unscaled, d(X,A) = √(100² + 6²) = 100.2 and d(X,B) = √(100² + 6²) = 100.2 — the
distances would be 99.8% driven by spend, and tenure would effectively be ignored.

8.5 Distance Metrics (also used in clustering)


Metric Formula Use when
Euclidean (L2) √∑(x − y )
i i
2
Continuous data, “straight-
line” distance. Default.
Metric Formula Use when
Manhattan (L1, City- ∑ ∥ xi − yi ∥ Grid-like paths; high-
block) dimensional data; more
robust to outliers
Minkowski 1/p
(∑ ∥ x i − y i ∥p ) Generalisation: p=1 →
Manhattan, p=2 →
Euclidean
Cosine similarity A⋅B Text/documents,
∥ A ∥∥ B ∥ recommender systems —
measures angle, ignores
magnitude
Hamming distance Count of positions that Categorical / binary
differ attributes
Jaccard Index ∥A∩B∥ Sets, binary
∥A∪B∥ presence/absence (market
baskets)

Simple Matching Coefficient (from your deck): for 5 categorical attributes where 2 match
→ similarity = 2/5, dissimilarity = 3/5. Hamming distance = 3.
Cosine similarity ranges from −1 to 1 (0 to 1 for non-negative data); 1 = identical
direction. Cosine distance = 1 − cosine similarity.

8.6 KNN Pros & Cons


✅ ❌
Extremely simple, no training phase Slow at prediction — O(n) distance calcs
per query
Naturally handles multi-class Curse of dimensionality — distances
become meaningless in high dimensions
Non-linear boundaries with no Must scale features
assumptions
Adapts instantly to new data Sensitive to outliers and irrelevant features
Works for classification and regression Memory-hungry (stores the whole
dataset); struggles with imbalanced classes

9. ★ THE MODEL SELECTION FRAMEWORK (your Question 1 answering


template)
9.1 The 5-Question Decision Tree
Q1. Is there a labelled target/outcome column in the data? - NO → Unsupervised → go
to Q5 - YES → Supervised → go to Q2
Q2. Is the target continuous or categorical? - Continuous (₹, %, days, score) →
Regression → Q3 - Categorical (Yes/No, class labels) → Classification → Q4
Q3. Regression — which one? | If the case says… | Use | |—|—| | Relationship looks linear;
need to quantify impact of each driver; need coefficients to present to leadership | Linear /
Multiple Regression | | Relationship is non-linear, has interactions, mixed data types, and
needs interpretable rules | Regression Tree | | Small dataset, purely local patterns, no
assumptions | KNN Regression |
Q4. Classification — which one? | If the case says… | Use | |—|—| | Binary outcome; need
probability of the event; need odds-ratio interpretation for regulators | Logistic
Regression | | Need explainable IF-THEN rules for frontline staff; non-linear; mixed
variable types | Decision Tree | | Similarity-based, “customers like this one”; small clean
dataset; no training time available | KNN | | Highest accuracy needed, interpretability
secondary | Random Forest / Boosting (mention as an extension) |
Q5. Unsupervised — which one? | If the case says… | Use | |—|—| | Group
customers/stores/products into segments; you know roughly how many segments you
want | K-Means Clustering | | Want to see the nesting structure, don’t know k, small
dataset | Hierarchical Clustering (dendrogram) | | “Which products are bought
together?”, cross-sell, product bundling, store layout | Association Rule Mining (Apriori)
| | “Recommend items to a user” | Recommender System (collaborative / content-based) |
| Too many variables, want to compress them | PCA |

9.2 The Answer Template (write it in this order — earns full marks)
1. Business problem restated: “The firm wants to identify which of its 2 million
subscribers are likely to cancel in the next 30 days.” 2. ML problem type: “This is a
supervised classification problem, because historical data contains a labelled
outcome (churned / retained) and the target is categorical/binary.” 3. Model
chosen + WHY: “I recommend Logistic Regression as the primary model because
(a) it outputs a churn probability for each subscriber, allowing the retention team
to rank and prioritise the top decile; (b) its coefficients convert to odds ratios, so
management can see exactly which drivers (e.g., data-usage decline, complaint
count) move churn and by how much; (c) it is fast and stable on a 2-million-row
dataset.” 4. Target & key features: “DV = Churn (1/0). IVs = tenure, ARPU, monthly
data usage trend, number of complaints, plan type (one-hot encoded), payment
delays.” 5. Data prep needed: “Impute missing ARPU with the median (skewed);
one-hot encode plan type; treat outliers in usage; check multicollinearity between
ARPU and data usage.” 6. Evaluation metric + justification: “Because churners
are only ~8% of the base, accuracy would be misleading. Evaluate on Recall and
AUC. Given a retention offer costs ₹400 versus ₹9,000 lifetime value lost, prioritise
Recall and lower the threshold to ~0.35.” 7. Validation: “70:15:15 split with 5-fold
stratified cross-validation.” 8. Business action: “Score the base weekly; route the
top 10% to a proactive retention call; A/B test offers; monitor for model drift
quarterly.” 9. Risk/limitation: “Correlation ≠ causation; watch for fairness issues if
the model uses proxies for protected attributes; retrain as behaviour shifts.”
9.3 Quick problem-type recognition table
Business problem in the
case Type Recommended model
Property valuation / house Supervised Regression Linear Regression /
price Regression Tree
Predicting sales revenue Supervised Regression Multiple Linear Regression
from ad spend
Forecasting digital ad spend Supervised Regression Multiple Linear Regression
ROI
Credit scoring / to give a Supervised Classification Logistic Regression /
loan or not Decision Tree
Fraud detection Supervised Classification Logistic/Tree + focus on
(imbalanced) Recall; or anomaly
detection if unlabelled
Customer churn prediction Supervised Classification Logistic Regression
Employee attrition Supervised Classification Logistic Regression /
prediction Decision Tree
Shortlisting resumes Supervised Classification Decision Tree
(explainability + fairness
audit)
Root cause of supply-chain Supervised Regression Regression Tree
disruption
Predicting machine failure Supervised Regression Regression Tree
days
Customer segmentation / Unsupervised K-Means
personas
Store/city site planning by Unsupervised K-Means / Hierarchical
similarity
Market basket / product Unsupervised Association Rules
bundling
Cross-sell “add-on at Unsupervised Association Rules /
checkout” Recommender
Detecting unusual Unsupervised Anomaly detection
transactions with no labels (Isolation Forest, LOF,
DBSCAN)
News article grouping Unsupervised Clustering
(Google News)
10. CHAPTER 6 — UNSUPERVISED LEARNING: CLUSTER ANALYSIS
10.1 Supervised vs Unsupervised (recap in your deck’s exact words)
• Supervised learning: discover patterns that relate data attributes to a target
(class) attribute; use those patterns to predict the target for future instances.
Learning by examples.
• Unsupervised learning: the data has no target attribute; explore the data to find
intrinsic structures. Learning by observation.

10.2 What is a cluster?


A cluster is a collection of data objects that are similar to one another within the same
group and dissimilar to objects in other groups.
Quality of clustering: - High intra-cluster similarity (cohesion — tight within) - Low
inter-cluster similarity (separation — far apart)
Quality depends on: the similarity/distance measure used, the implementation, and its
ability to uncover hidden patterns.
Typical uses: as a stand-alone tool to get insight into data distribution, or as a pre-
processing step for other algorithms.

10.3 Business applications (memorise 5–6)


Application What clustering does
Market segmentation Group customers by RFM / demographics /
behaviour → tailored 4Ps per segment
Credit-card limit extension Cluster cardholders by spend and
repayment; extend limits only to the safe,
high-value cluster
Targeted e-mail campaigns Different creative per cluster instead of one
blast
Social network / sentiment analysis Group users by opinion or interaction
pattern
City planning Group houses/areas by type, value,
geography for zoning
News clustering (Google News) Group articles about the same story from
different outlets
Computer vision / object recognition Group visually similar regions
Medical imaging Image detection, classification,
segmentation in radiology & pathology
Anomaly detection Points that belong to no cluster = faulty
equipment, human error, security breach
Customer personas Build buyer-persona profiles → align
Application What clustering does
product messaging
Recommendation engines Discover trends in past purchase behaviour
→ cross-sell

10.4 Major clustering approaches


Approach Idea Typical methods
Partitioning Construct various partitions k-means, k-medoids
of n objects into k clusters,
evaluate by a criterion
(minimise sum of squared
errors)
Hierarchical Create a hierarchical Agglomerative, Divisive
decomposition of the data;
recursive
partitioning/merging
(Density-based) Group by dense regions; DBSCAN
leftovers are noise

Finding the global optimum would require exhaustively enumerating all partitions —
computationally impossible. So k-means and k-medoids are heuristic methods.

10.5 K-MEANS CLUSTERING ★★★


Definition: an algorithm to cluster n objects based on attributes into k clusters, where k <
n, such that intra-cluster similarity is high and inter-cluster similarity is low. Each cluster is
represented by its centroid (the mean point).

The algorithm (4 steps, as per the deck)


1. Partition objects into k non-empty subsets (or randomly place k initial centroids).
2. Compute seed points as the centroids of the current clusters (centroid = mean of
all points in the cluster).
3. Assign each object to the cluster with the nearest seed point.
4. Go back to step 2; stop when the assignments no longer change (convergence).
Objective function it minimises — Within-Cluster Sum of Squares (WCSS / SSE /
Inertia):
k
W C S S=∑ ∑ ∥ x − μ j ∥2
j=1 x ∈C j

The three hyperparameters (explicit deck slide)


1. Initial values of clusters (initialisation — random vs k-means++)
2. Distance measure (Euclidean, Manhattan, …)
3. Number of clusters, k ← the most important
Issues / Limitations
• ✅ Simple to compute, converges easily, good time and space performance
• ❌ May produce empty clusters
• ❌ Outliers unduly affect the centroids (the mean is not robust — k-medoids/PAM
fixes this by using an actual data point as the centre)
• ❌ You must pre-define k
• ❌ Random initialisation → may converge to a local optimum, so results vary
between runs (fix: run multiple times with different seeds, or use k-means++)
• ❌ Assumes spherical, similar-sized clusters; fails on elongated/irregular shapes
• ❌ Requires scaling; only works on numeric data (categorical → k-modes)

10.6 Choosing k

(a) Elbow Method


Run K-Means for a range of k values. For each, compute the average distance of each
point to its centroid (WCSS/SSE) and plot it against k. Pick the k where the curve falls
suddenly and then flattens — the “elbow.”
WCSS
│●
│ ●
│ ●
│ ● ← ELBOW (k=3): sharp drop ends here
│ ●───●───●───●
└───────────────────────── k
1 2 3 4 5 6 7

Limitation (deck slide): with 2-D data it’s easy to see the elbow (k ≤ 4). Ambiguity arises
when the number of clusters is larger — the curve becomes smooth with no obvious
elbow. For higher-dimensional data, the Silhouette method is the better alternative.

(b) Silhouette Method ★


For each point i: - a(i) = average distance from i to all other points in its own cluster
(cohesion) - b(i) = average distance from i to all points in the nearest neighbouring
cluster (separation)
b (i ) −a ( i )
s ( i )=
max {a ( i ) , b ( i ) }
The Silhouette Score for a solution = the average s(i) across all points.

s(i) value Meaning


+1 Clusters are well apart and clearly
distinguished; the point is deep inside the
right cluster
0 Clusters are indifferent — the point sits on
s(i) value Meaning
the boundary; distance between clusters is
not significant
−1 The point has been assigned to the wrong
cluster; high inter-cluster overlap

Pick the k whose average Silhouette Score is closest to 1. - Positive score → clusters
distinct, low chance of inter-cluster overlap - Negative score → data points wrongly
grouped into non-representative clusters, high overlap
Added advantages over Elbow: it validates consistency within clusters, works in high
dimensions, and can identify outliers within a cluster (points with low/negative s).
A silhouette plot shows a horizontal bar per point, grouped by cluster. Good solution: all
clusters have wide, similarly-sized blocks extending well past the average line. Bad
solution: clusters of very unequal width, blocks falling short of the average line, or bars
extending into negative territory.

10.7 SOLVED K-MEANS NUMERICAL ★★


Question. Cluster these 6 customers into k = 2 using K-Means with Euclidean distance.
Initial centroids: C1 = A(1,1) and C2 = F(8,8).
Points: A(1,1), B(2,1), C(4,3), D(5,4), E(8,7), F(8,8)
ITERATION 1 — Assignment step

Pt d to C1(1,1) d to C2(8,8) Assign


A(1,1) √0 = 0.00 √(49+49)=9.90 C1
B(2,1) √(1+0)=1.00 √(36+49)=9.22 C1
C(4,3) √(9+4)=3.61 √(16+25)=6.40 C1
D(5,4) √(16+9)=5.00 √(9+16)=5.00 C1 (tie → assign
to C1)
E(8,7) √(49+36)=9.22 √(0+1)=1.00 C2
F(8,8) √(49+49)=9.90 0.00 C2

Cluster 1 = {A, B, C, D}; Cluster 2 = {E, F}


Update centroids: - C1 = ((1+2+4+5)/4, (1+1+3+4)/4) = (12/4, 9/4) = (3.00, 2.25) - C2 =
((8+8)/2, (7+8)/2) = (8.00, 7.50)
ITERATION 2 — Reassignment

d to C1(3.00, d to C2(8.00,
Pt 2.25) 7.50) Assign
A(1,1) √(4+1.5625)=2. √(49+42.25)=9. C1
36 55
d to C1(3.00, d to C2(8.00,
Pt 2.25) 7.50) Assign
B(2,1) √(1+1.5625)=1. √(36+42.25)=8. C1
60 85
C(4,3) √(1+0.5625)=1. √(16+20.25)=6. C1
25 02
D(5,4) √(4+3.0625)=2. √(9+12.25)=4.6 C1
66 1
E(8,7) √(25+22.5625)= √(0+0.25)=0.50 C2
6.90
F(8,8) √(25+33.0625)= √(0+0.25)=0.50 C2
7.62

Assignments unchanged → ALGORITHM HAS CONVERGED.


Final answer: - Cluster 1 = {A, B, C, D}, centroid (3.00, 2.25) — “low-value / low-
engagement” segment - Cluster 2 = {E, F}, centroid (8.00, 7.50) — “high-value / high-
engagement” segment
Bonus — compute WCSS: C1: (2.36² + 1.60² + 1.25² + 2.66²) = 5.57+2.56+1.56+7.08 =
16.77 C2: (0.50² + 0.50²) = 0.50 WCSS = 17.27
Bonus — a silhouette calculation for point C(4,3): - a(C) = avg distance to A, B, D within
cluster = [d(C,A)=√(9+4)=3.61, d(C,B)=√(4+4)=2.83, d(C,D)=√(1+1)=1.41] → a =
(3.61+2.83+1.41)/3 = 2.62 - b(C) = avg distance to E, F = [√(16+16)=5.66, √(16+25)=6.40]
→ b = 6.03 - s(C) = (6.03 − 2.62)/max(2.62, 6.03) = 3.41/6.03 = +0.566 → reasonably well-
clustered.

10.8 HIERARCHICAL CLUSTERING


Core idea: does not require a representative (centroid) for each cluster. It assumes
clusters are hierarchically structured — every cluster is made up of smaller clusters. The
result is a tree (dendrogram) whose leaves are individual data points and whose inner
nodes represent the collection of all points in the subtree.

Two types
Agglomerative (bottom-up) — the
common one Divisive (top-down)
Every observation starts as its own cluster All observations start in one cluster
Repeatedly merge the two closest clusters Repeatedly split the cluster
Stop when everything is in one big cluster Stop when each observation is its own
cluster

Agglomerative algorithm (3 steps, as in the deck): 1. Start with each data point in a
single cluster. 2. Find the two data points/clusters with the shortest distance (using an
appropriate distance measure) and merge them. 3. Repeat step 2 until all points have
merged into one cluster.

Linkage criteria (how to measure distance between clusters)


Linkage Definition Behaviour
Single link (MIN) Distance between the two Can handle non-elliptical
closest points of the two shapes; suffers from
clusters chaining (long straggly
clusters); sensitive to noise
Complete link (MAX) Distance between the two Produces compact, roughly
farthest points equal-diameter clusters;
breaks large clusters;
sensitive to outliers
Average link Average of all pairwise Compromise between single
distances between the two and complete; less sensitive
clusters to noise
Centroid Distance between the two Can produce inversions in
cluster centroids the dendrogram
Ward’s method Merges the two clusters Tends to produce balanced,
that cause the smallest compact, similarly-sized
increase in total within- clusters. Most popular
cluster variance (SSE) default.

Reading a dendrogram
• The y-axis = distance (dissimilarity) at which clusters merged. A tall vertical line =
a merge between very dissimilar groups.
• To get k clusters, draw a horizontal line across the dendrogram; the number of
vertical lines it crosses = the number of clusters.
• Best cut: where the line can be drawn through the longest uninterrupted vertical
gap — that’s where you can move the furthest without merging anything, indicating
well-separated groups.

SOLVED HIERARCHICAL CLUSTERING NUMERICAL ★


Distance matrix for 5 stores:

P Q R S T
P 0 2 6 10 9
Q 2 0 5 9 8
R 6 5 0 4 5
S 10 9 4 0 3
T 9 8 5 3 0

Use SINGLE LINKAGE (minimum distance).


Step 1: Smallest distance in the matrix = d(P,Q) = 2 → merge → {PQ} Recompute with MIN:
d(PQ,R) = min(6,5) = 5 · d(PQ,S) = min(10,9) = 9 · d(PQ,T) = min(9,8) = 8

PQ R S T
PQ 0 5 9 8
R 5 0 4 5
S 9 4 0 3
T 8 5 3 0

Step 2: Smallest = d(S,T) = 3 → merge → {ST} d(PQ,ST) = min(9,8) = 8 · d(R,ST) = min(4,5)


=4

PQ R ST
PQ 0 5 8
R 5 0 4
ST 8 4 0

Step 3: Smallest = d(R,ST) = 4 → merge → {RST} d(PQ,RST) = min(5, 8) = 5


Step 4: Merge {PQ} and {RST} at distance 5 → single cluster {PQRST}
Merge sequence: (P,Q)@2 → (S,T)@3 → (R,ST)@4 → (PQ, RST)@5
Dendrogram:
Height
5 ┤ ┌─────────────────┐
4 ┤ │ ┌─────┴──┐
3 ┤ │ │ ┌──┴──┐
2 ┤ ┌─┴─┐ │ │ │
0 ┤ P Q R S T

Two-cluster solution: cut between height 4 and 5 → {P, Q} and {R, S, T}. Three-cluster
solution: cut between 3 and 4 → {P,Q}, {R}, {S,T}.

10.9 K-Means vs Hierarchical — comparison table


Hierarchical
K-Means (Agglomerative)
Need to specify k upfront? Yes No — decide after seeing
the dendrogram
Output Flat set of k clusters A full tree/hierarchy
Complexity ~O(n·k·i) — fast, scales to ~O(n³) or O(n² log n) —
large data slow, small data only
Reproducibility Varies with random Deterministic
initialisation
Can a point change cluster Yes, each iteration No — merges are
Hierarchical
K-Means (Agglomerative)
later? irreversible (greedy)
Handles outliers Poorly (drags centroids) Better (they merge last,
visible in dendrogram)
Cluster shapes Spherical, similar size Depends on linkage — more
flexible
Best for Large customer bases, Small datasets, exploring
known/approximate k structure, taxonomy
building

11. ★★ ASSOCIATION RULE MINING & RECOMMENDER SYSTEMS (guaranteed


Question 4)
11.1 What it is
Association Rule Mining is an unsupervised algorithm for identifying objects/items
that are related to each other. It is the go-to tool for retailers and e-commerce analysts
for detecting product bundles and customer service preferences, and for building
recommendations.
Rule notation: {Antecedent} → {Consequent}, i.e. IF the customer buys X, THEN they
are likely to buy Y. Example: {Bread, Butter} → {Jam}
Only the left side is the “if” — the arrow does not mean causation. It means co-occurrence.

11.2 THE THREE METRICS (hyperparameters) ★★★


Let N = total number of transactions.

Metric Formula Range What it tells you


Support Transactions containing 0
(Atoand
1 B) How frequent /
N popular is this
combination? Filters
out rare,
commercially
irrelevant rules.
Confidence Su p por t ( A ∪B ) 0 to 1 How reliable is the
=P ( B ∣ A )
Su p p ort ( A) rule? Of the people
who bought A, what
% also bought B?
Lift C o n f i d e n c e ( A → B ) 0 to ∞ S u p p o r t ( A ∪ BHow ) much better
=
S u p p o r t (B ) S u p p o r t ( A ) × S u p pthan
o r t ( Bchance?
)
Controls for the
baseline popularity
Metric Formula Range What it tells you
of B.

Support of a single item A = (transactions containing A)/N.

Interpreting Lift — the money metric


Lift Interpretation Business action
=1 A and B are independent No action; the association is
— probability of the an illusion of popularity
antecedent and consequent
occurring is unrelated. The
two items are not related.
>1 Positive association — the Bundle them, cross-sell,
probability is high that an place together,
antecedent item results in recommend
the consequent item
<1 Negative association — Do not bundle; these are
the antecedent item can be competing/substitute
a substitute/replacement products. Possibly promote
for the consequent item separately.

Why Lift matters more than Confidence — the deck’s core lesson. Confidence alone can
be fooled by a very popular consequent. If 80% of everyone buys milk, then a rule
{Batteries} → {Milk} with 70% confidence actually shows batteries buyers buy milk less
than average. Lift = 0.70/0.80 = 0.875 < 1 → a negative association that confidence
alone would have missed.
Other useful metrics (know the names): - Conviction = (1 − Support(B))/(1 −
Confidence(A→B)) — how much the rule would be wrong if A and B were independent -
Leverage = Support(A∪B) − Support(A)×Support(B) — the absolute (not ratio) lift over
independence

11.3 The Apriori Algorithm


The Apriori Principle: “If an itemset is frequent, then all of its subsets must also be
frequent.” Equivalently (the contrapositive, which is what makes it efficient): if an itemset
is infrequent, all of its supersets are infrequent too — so they can be pruned without
checking.
Steps: 1. Set a minimum support threshold (e.g., 0.02) and a minimum confidence
threshold (e.g., 0.5). 2. Find all frequent 1-itemsets (single items meeting min support). 3.
Generate candidate 2-itemsets from them, count support, prune those below min support.
4. Repeat for 3-itemsets, 4-itemsets… until no new frequent itemsets emerge. 5. From each
frequent itemset, generate rules meeting min confidence. 6. Rank the surviving rules by
Lift and act on the top ones.
Managerial tuning: min support too high → you only find the boring obvious rules. Too
low → thousands of rules, computationally explosive, many spurious.

11.4 SOLVED ASSOCIATION RULE NUMERICAL ★★★


Question. A supermarket recorded 10 transactions:

T# Items
1 Bread, Milk
2 Bread, Diaper, Beer, Eggs
3 Milk, Diaper, Beer, Cola
4 Bread, Milk, Diaper, Beer
5 Bread, Milk, Diaper, Cola
6 Bread, Milk
7 Milk, Diaper
8 Bread, Diaper, Beer
9 Bread, Milk, Diaper
10 Bread, Milk, Cola

Step 1 — Individual item counts and support (N = 10):

Item Transactions Count Support


Bread 1,2,4,5,6,8,9,10 8 0.8
Milk 1,3,4,5,6,7,9,10 8 0.8
Diaper 2,3,4,5,7,8,9 7 0.7
Beer 2,3,4,8 4 0.4
Cola 3,5,10 3 0.3
Eggs 2 1 0.1

Step 2 — Evaluate the rule {Diaper} → {Beer} - Transactions with both Diaper and Beer:
2, 3, 4, 8 → count = 4 - Support(Diaper ∪ Beer) = 4/10 = 0.40 - Confidence =
Support(Diaper∪Beer)/Support(Diaper) = 0.40/0.70 = 0.571 → 57.1% - Lift =
Confidence/Support(Beer) = 0.571/0.40 = 1.43
Interpretation: The combination appears in 40% of all baskets. Of customers who buy
diapers, 57.1% also buy beer. Lift = 1.43 > 1 → a positive association: diaper buyers are
43% more likely to buy beer than the average shopper. ✅ Action: place beer near the
diaper aisle; bundle a diaper+beer promotion; recommend beer at checkout when
diapers are in the cart.
Step 3 — Evaluate the reverse rule {Beer} → {Diaper} - Support = 0.40 (same —
support is symmetric) - Confidence = 0.40/0.40 = 1.00 → 100% (every beer buyer
bought diapers!) - Lift = 1.00/0.70 = 1.43 (same — lift is also symmetric)
Key learning: Support and Lift are symmetric; CONFIDENCE IS NOT. {Beer}→{Diaper}
at 100% confidence is a much stronger operational rule than {Diaper}→{Beer} at 57%. So
the merchandising action should be: when a customer picks up beer, prompt diapers — not
the other way round.
Step 4 — A trap rule: {Cola} → {Bread} - Both: T5, T10 → count = 2 → Support = 0.20 -
Confidence = 0.20/0.30 = 0.667 → 66.7% — looks strong! - Lift = 0.667/0.80 = 0.833 <
1 → ❌ Negative association. Bread is bought by 80% of everyone anyway; cola buyers buy
it less often. Confidence alone would have misled us. Do not bundle.
Step 5 — A 2-item antecedent: {Bread, Milk} → {Diaper} - Bread & Milk together:
T1,4,5,6,9,10 → 6 → Support(Bread∪Milk) = 0.60 - All three: T4, T5, T9 → 3 → Support =
0.30 - Confidence = 0.30/0.60 = 0.50 → 50% - Lift = 0.50/0.70 = 0.714 < 1 → negative
association.
Step 6 — Deck’s worked example type: if a rule “applies to 60% of all customer
transactions because 60% of people buy Coffee,” that 60% is the support of the
antecedent — it tells you the rule’s reach, not its reliability.

11.5 RECOMMENDER SYSTEMS


A recommender system predicts what a user will like and surfaces it. Three families:

(a) Content-Based Filtering


“Recommend items similar to what this user liked before.” - Build a feature profile of
each item (genre, brand, price band, keywords) and a profile of the user’s tastes. -
Compute similarity between the user profile and unseen items — usually cosine
similarity. - ✅ Works from day one for a new item; no other users needed; explainable
(“because you watched sci-fi”). - ❌ Over-specialisation / filter bubble — never surprises
the user; needs rich item metadata.

(b) Collaborative Filtering


“Recommend what similar users liked.” Uses the user–item rating matrix only; needs no
item metadata.

User-based CF Item-based CF
Find users similar to you; recommend what Find items similar to those you liked
they liked (similar = rated similarly by the same
people)
“Customers like you also bought…” “Customers who bought this also bought…”
User tastes shift often → less stable Item–item relations are stable → Amazon’s
approach, scales better
• ✅ Discovers serendipitous recommendations; no metadata required.
• ❌ Cold-start problem (new user or new item has no ratings), sparsity (most users
rate very few items), popularity bias.
(c) Hybrid
Combines both (Netflix). Content-based handles cold start; collaborative takes over once
behavioural data accumulates. Association rules add the “frequently bought together” strip.

Association Rules vs Collaborative Filtering


Association Rules Collaborative Filtering
Works on transactions/baskets Works on user–item ratings
Output: IF-THEN rules, same for everyone Output: personalised ranked list per user
in that basket state
Great for in-basket cross-sell at checkout Great for homepage personalisation

11.6 SOLVED RECOMMENDER NUMERICAL ★★


Question. User–item rating matrix (1–5; blank = not rated):

Movie A Movie B Movie C Movie D


User 1 5 3 4 4
User 2 3 1 2 3
User 3 4 3 4 3
Target U 4 3 3 ?

Recommend: should we suggest Movie D to Target U? Use user-based collaborative


filtering with cosine similarity on the commonly-rated items (A, B, C).
Vectors on {A, B, C}: U = (4,3,3) · U1 = (5,3,4) · U2 = (3,1,2) · U3 = (4,3,4)
Cosine similarity = (A·B)/(‖A‖·‖B‖)
sim(U, U1): - Dot = 4(5) + 3(3) + 3(4) = 20 + 9 + 12 = 41 - ‖U‖ = √(16+9+9) = √34 = 5.831 ;
‖U1‖ = √(25+9+16) = √50 = 7.071 - sim = 41/(5.831 × 7.071) = 41/41.23 = 0.9944
sim(U, U2): - Dot = 4(3) + 3(1) + 3(2) = 12 + 3 + 6 = 21 - ‖U2‖ = √(9+1+4) = √14 = 3.742 -
sim = 21/(5.831 × 3.742) = 21/21.82 = 0.9624
sim(U, U3): - Dot = 4(4) + 3(3) + 3(4) = 16 + 9 + 12 = 37 - ‖U3‖ = √(16+9+16) = √41 =
6.403 - sim = 37/(5.831 × 6.403) = 37/37.33 = 0.9911
Ranking of neighbours: U1 (0.9944) > U3 (0.9911) > U2 (0.9624)
Predicted rating for Movie D (weighted average using all three neighbours):
∑ s i m ( U , U i ) ×r U , D
r^ U , D = i

∑ s im ( U , U i )

= [0.9944(4) + 0.9624(3) + 0.9911(3)] / (0.9944 + 0.9624 + 0.9911) = [3.978 + 2.887 +


2.973] / 2.9479 = 9.838 / 2.9479 = 3.34
Decision: predicted rating 3.34 out of 5. If the platform’s recommendation threshold is
3.5, do not push Movie D to the top of U’s feed; if the threshold is 3.0, recommend it but
rank it below stronger candidates.
Note on cosine similarity: all three values are above 0.96 because cosine
measures the angle and ignores magnitude — User 2 rates everything low but in
the same pattern, so cosine calls them similar. If you want to penalise that, use
Pearson correlation (mean-centred cosine) or adjusted cosine, which subtract
each user’s average rating first. That’s exactly why Pearson is often preferred for
rating data.

12. ADDITIONAL SOLVED NUMERICALS (mixed practice)


N1. Linear Regression — build the line from scratch
Data: Advertising spend X (₹ lakh) and Sales Y (₹ lakh):

X 2 4 6 8 10
Y 12 18 26 30 39

Step 1: X̄ = 30/5 = 6 ; Ȳ = 125/5 = 25

(X−X̄ )
X Y X−X̄ Y−Ȳ (Y−Ȳ) (X−X̄ )²
2 12 −4 −13 52 16
4 18 −2 −7 14 4
6 26 0 1 0 0
8 30 2 5 10 4
10 39 4 14 56 16
Σ 132 40

Step 2: β₁ = 132/40 = 3.30 ; β₀ = 25 − 3.30(6) = 25 − 19.8 = 5.20


Model: Ŷ = 5.20 + 3.30X Interpretation: each additional ₹1 lakh of ad spend increases sales
by ₹3.30 lakh; baseline sales with zero advertising = ₹5.20 lakh.
Step 3 — Predictions & errors:

Ŷ=
X Y 5.2+3.3X e = Y−Ŷ |e| e² (Y−Ȳ)²
2 12 11.8 +0.2 0.2 0.04 169
4 18 18.4 −0.4 0.4 0.16 49
6 26 25.0 +1.0 1.0 1.00 1
8 30 31.6 −1.6 1.6 2.56 25
Ŷ=
X Y 5.2+3.3X e = Y−Ŷ |e| e² (Y−Ȳ)²
10 39 38.2 +0.8 0.8 0.64 196
4.0 4.40 440

Step 4 — Metrics: - MAE = 4.0/5 = 0.80 - MSE = 4.40/5 = 0.88 - RMSE = √0.88 = 0.938 -
R² = 1 − (SSres/SStot) = 1 − (4.40/440) = 1 − 0.01 = 0.99 → 99% of the variation in sales is
explained by ad spend. - Adjusted R² (n=5, k=1) = 1 − [(1−0.99)(4/3)] = 1 − 0.01333 =
0.9867 - MAPE = (1/5)×[0.2/12 + 0.4/18 + 1.0/26 + 1.6/30 + 0.8/39]×100 =
(1/5)×[0.01667+0.02222+0.03846+0.05333+0.02051]×100 = (0.15119/5)×100 = 3.02%
Step 5 — Predict for X = 12: Ŷ = 5.2 + 3.3(12) = ₹44.8 lakh (Caveat: X=12 is outside the
training range 2–10, so this is extrapolation — flag it.)

N2. Comparing two regression models


Model A Model B
Predictors (k) 3 8
R² 0.82 0.85
n 100 100

Adj R²(A) = 1 − [(1−0.82)(99/96)] = 1 − (0.18 × 1.03125) = 1 − 0.1856 = 0.8144 Adj R²(B) =


1 − [(1−0.85)(99/91)] = 1 − (0.15 × 1.0879) = 1 − 0.1632 = 0.8368
Verdict: Model B still wins on Adjusted R² (0.837 vs 0.814), so the 5 extra predictors earn
their keep. But B is harder to explain, more prone to multicollinearity, and more expensive
to maintain — check VIFs and the p-values of the 5 extras before adopting it.

N3. Multi-class confusion matrix


A model classifies loan applications into Low / Medium / High risk:

Actual ↓ /
Predicted → Low Medium High Total
Low 50 10 5 65
Medium 8 40 12 60
High 2 5 68 75
Total 60 55 85 200

Overall Accuracy = (50+40+68)/200 = 158/200 = 79%


For class “High” (one-vs-rest): - TP = 68 ; FP = 5 + 12 = 17 ; FN = 2 + 5 = 7 ; TN = 200 − 68
− 17 − 7 = 108 - Precision = 68/(68+17) = 68/85 = 0.800 - Recall = 68/(68+7) = 68/75 =
0.907 - F1 = 2(0.800×0.907)/(0.800+0.907) = 2(0.7256/1.707) = 0.850
For class “Medium”: - TP = 40 ; FP = 10 + 5 = 15 ; FN = 8 + 12 = 20 - Precision = 40/55 =
0.727 ; Recall = 40/60 = 0.667 ; F1 = 2(0.727×0.667)/(1.394) = 0.696
Insight: “Medium” is the weakest class — the model confuses Medium with High (12
cases). Since misclassifying a High-risk applicant as Medium means approving a bad loan,
focus on lifting Recall for High risk; it’s already 0.907, but the 7 missed High-risk cases are
the expensive ones.

N4. Threshold shifting


A churn model produces these probabilities for 10 customers (actual churn in brackets):
0.91(Y), 0.85(Y), 0.72(N), 0.66(Y), 0.55(N), 0.48(Y), 0.40(N), 0.33(N), 0.21(N), 0.10(N)
Actual: 4 churners (Y), 6 non-churners (N).
At threshold 0.5 — predicted churn: 0.91, 0.85, 0.72, 0.66, 0.55 (5 customers) - TP = 0.91,
0.85, 0.66 → 3 ; FP = 0.72, 0.55 → 2 ; FN = 0.48 → 1 ; TN = 4 - Accuracy = 7/10 = 70% ;
Precision = 3/5 = 0.60 ; Recall = 3/4 = 0.75 ; F1 = 2(0.6×0.75)/1.35 = 0.667
At threshold 0.45 — predicted churn: top 6 (down to 0.48) - TP = 4 ; FP = 2 ; FN = 0 ; TN =
4 - Accuracy = 8/10 = 80% ; Precision = 4/6 = 0.667 ; Recall = 4/4 = 1.00 ; F1 =
2(0.667×1)/1.667 = 0.80
Conclusion: Lowering the threshold from 0.50 to 0.45 caught all four churners at the cost
of the same two false alarms. If a retention offer costs ₹500 and a lost customer costs
₹8,000, the 0.45 threshold is clearly superior: it saves ₹8,000 for ₹1,000 of wasted offers.

N5. Imputation + scaling combined


Column: Monthly Income (₹’000): 25, 30, ?, 28, 32, 150, 27, ?, 29 - Non-missing: 25, 30, 28,
32, 150, 27, 29 → Mean = 321/7 = 45.86 ; sorted: 25,27,28,29,30,32,150 → Median = 29 -

✓) - Therefore impute with the MEDIAN (29), not the mean (45.86) — the mean has
150 is an outlier (IQR: Q1=27, Q3=32, IQR=5, upper fence = 32+7.5 = 39.5 → 150 > 39.5

been dragged up by a single extreme value and would inject a false ₹45,860 into two
records. - Then scale. Min-Max on the imputed series (min 25, max 150): a ₹29k record
maps to (29−25)/125 = 0.032 — everything is crushed into the bottom of the range by that
one outlier. Better: cap the outlier at 39.5 first, then scale, or use Z-score / robust
scaling.

13. QUESTION BANK WITH MODEL ANSWERS


Case Question 1 (Classification)
“MediCare Diagnostics processes 40,000 blood-test panels monthly. Historical
records contain 200,000 panels with the eventual confirmed diagnosis (Disease / No
Disease). Only 3% are positive. Management wants an ML system to flag likely-
positive panels for immediate senior-pathologist review. Which model, and how
should it be evaluated?”
Answer. 1. Type: Supervised classification — the historical data carries a labelled binary
outcome. 2. Model: Logistic Regression as the primary model. It outputs a probability of
disease (letting pathologists triage by risk rather than accepting a hard Yes/No), its odds
ratios are directly interpretable to a clinical review board, and it is stable on 200,000 rows.
A Decision Tree is a strong complement because it produces clinician-readable IF-THEN
rules; but on its own it overfits and gives coarser probability estimates. 3. Data prep:
median-impute skewed biomarker values; scale features; check multicollinearity between
correlated biomarkers (VIF); one-hot encode categorical panel types. 4. Class imbalance:
at 3% positive, a model predicting “No Disease” always scores 97% accuracy and 0%
recall. So: apply SMOTE/class weights, and do not evaluate on accuracy. 5. Metric:
Recall (Sensitivity) is paramount — a False Negative means a diseased patient is sent
home untreated, a potentially fatal cost. A False Positive merely triggers a second review
(some pathologist time). Report Recall, Precision, F1 and AUC; target Recall ≥ 0.95. 6.
Threshold: lower it well below 0.5 (e.g., 0.15) to maximise Recall, accepting more flagged
panels. 7. Validation: stratified 5-fold cross-validation to preserve the 3% ratio in every
fold. 8. Caveat: the model assists, never replaces, the pathologist; monitor for drift and for
demographic bias.

Case Question 2 (Regression)


“UrbanNest, a real-estate portal, wants to auto-generate a price estimate for any
listed property using area (sq ft), bedrooms, age, distance to metro, and locality.
What model, and how do you judge it?”
Answer. 1. Type: Supervised regression — target (price in ₹) is continuous. 2. Model:
Multiple Linear Regression first, because the coefficients directly answer the business
question “what is a square foot worth here, and what does metro proximity add?” — a
number the sales team can quote. Add a Regression Tree as a comparator to capture non-
linearities (price per sq ft behaves very differently in a premium locality) and interactions.
3. Prep: one-hot encode Locality (k−1 dummies to avoid the dummy trap); check
multicollinearity between area and bedrooms (expect high VIF — consider dropping
bedrooms or using rooms-per-sqft); log-transform price if residuals show
heteroscedasticity (prices fan out at the top end). 4. Assumptions to test: linearity,
independence, homoscedasticity, no multicollinearity. 5. Metrics: RMSE (in ₹, punishes big
misses on expensive homes — costly for credibility), MAE (average rupee error, easiest to
communicate), R²/Adjusted R² (% of price variation explained), and MAPE (comparable
across price bands and cities). 6. Business use: publish the estimate as a range (± 1
RMSE), never a point number; flag listings priced outside the range as potential
mispricings.

Case Question 3 (Unsupervised)


“BrewBox, a coffee subscription startup with 80,000 subscribers, has no idea who its
customers are. It has order frequency, average order value, roast preferences, and
browsing time. It wants distinct personas for its marketing team.”
Answer. 1. Type: Unsupervised — there is no target/label column; the company is
exploring, not predicting. 2. Model: K-Means Clustering. With 80,000 rows it is fast and
scalable, and it produces a clean set of flat segments with interpretable centroids the
marketing team can profile. Hierarchical clustering is unsuitable at this scale (O(n²)–
O(n³)) though it could be run on a 2,000-row sample first to suggest k via the dendrogram.
3. Prep — critical: feature scaling is mandatory. Order value (₹200–₹4,000) would
otherwise completely dominate order frequency (1–8) in the Euclidean distance. Use Z-
score standardisation. One-hot encode roast preference. Handle outliers (a few whale
customers will drag centroids). 4. Choosing k: run k = 2…10; plot WCSS for the Elbow
Method and compute Silhouette Scores. Prefer the Silhouette result since the data is
multi-dimensional and the elbow will likely be ambiguous. Pick the k with average
silhouette closest to 1 — and cross-check that the resulting segments are commercially
actionable and distinguishable, not just statistically tidy. 5. Interpretation: profile each
centroid and name it — e.g. “Weekly Dark-Roast Loyalists (high frequency, mid AOV)”,
“Occasional Premium Explorers (low frequency, high AOV, high browse time)”, “Lapsing Light
Users.” 6. Action: differentiated pricing, creative and cadence per persona; add association
rule mining on their order baskets to design bundles within each segment. 7. Caveat: K-
Means results vary with random initialisation → run multiple seeds / k-means++; re-cluster
every 6 months as behaviour drifts.

Short-Answer Bank
Q. Why can’t we use Accuracy for a fraud model? Because fraud is rare (say 0.5%). A
model that predicts “not fraud” for everything achieves 99.5% accuracy while catching zero
fraud. Use Recall (catch rate), Precision (investigator time wasted), F1 and AUC instead.
Q. Difference between a parameter and a hyperparameter? A parameter is learned
from the data during training (regression coefficients, tree split thresholds, cluster
centroids). A hyperparameter is set by the analyst before training (k in KNN, k in K-Means,
tree max-depth, min support in Apriori).
Q. Why is scaling needed for KNN and K-Means but not for a Decision Tree? KNN and
K-Means compute distances; a variable with a larger numeric range dominates the
distance and effectively silences the others. A Decision Tree splits on a threshold within
one variable at a time (Income > 8L?) — rescaling just rescales the threshold, so the
tree is unchanged.
Q. What does an R² of 0.45 mean, and is it bad? 45% of the variance in the dependent
variable is explained by the model. Whether that’s bad depends on domain: 0.45 is poor for
engineering measurement but respectable in consumer-behaviour or social-science
modelling where human choice is inherently noisy. Judge it against a baseline model and
against RMSE in business units.
Q. Model gives 97% training accuracy, 64% test accuracy. Diagnose and fix.
Overfitting (high variance) — the model memorised noise. Fixes: prune the tree / reduce
depth, reduce features, gather more training data, use k-fold cross-validation, apply
regularisation, or move to an ensemble.
Q. A rule has 90% confidence but lift of 0.85. Act on it? No. The high confidence is an
artefact of the consequent being extremely popular overall. Lift < 1 signals a negative
association — buyers of the antecedent are less likely than average to buy the consequent,
suggesting the two are substitutes. Do not bundle them.
Q. Elbow method vs Silhouette — which and why? Elbow plots WCSS against k and looks
for the sharp bend; it is quick but becomes ambiguous when the true number of clusters
is large or the data is high-dimensional. Silhouette computes, for each point, how much
closer it is to its own cluster than to the nearest other cluster; it gives a scored,
comparable value in [−1, 1], works in high dimensions, and additionally flags outliers.
Prefer Silhouette; use both if time permits.
Q. Why is Type I error acceptable in cancer screening but not in spam filtering? In
screening, a Type I error (False Positive) means an extra confirmatory test — inconvenient
and mildly costly. A Type II error (False Negative) means an undiagnosed cancer —
catastrophic. In spam filtering the ranking flips: a False Positive sends a legitimate job offer
to the junk folder, while a False Negative is just one more spam email in the inbox.
Q. What is the cold-start problem? A recommender system cannot generate
recommendations for a new user (no rating history) or a new item (no ratings received).
Mitigations: content-based filtering using item metadata, popularity-based defaults,
onboarding preference surveys, or demographic bootstrapping — then switch to
collaborative filtering as data accumulates.
Q. State the Apriori principle and why it matters. If an itemset is frequent, all its subsets
are frequent; equivalently, if an itemset is infrequent, every superset of it is also
infrequent. This lets the algorithm prune vast branches of the candidate space without
counting them, making market-basket analysis tractable on millions of transactions.
Q. Correlation vs causation in a regression output. A significant positive coefficient
shows that X and Y move together after controlling for the other variables in the model
— not that X causes Y. Omitted variables, reverse causality, or a common driver can all
produce it. To claim causation you need an experiment (A/B test) or a causal-inference
design.

14. ⚡ THE CHEATSHEET — LAST 30 MINUTES BEFORE THE EXAM


14.1 ALL FORMULAS ON ONE PAGE
Classification
Accuracy = (TP + TN) / (TP + TN + FP + FN)
Precision = TP / (TP + FP) ← of what we FLAGGED
Recall/Sens = TP / (TP + FN) ← of what ACTUALLY was
Specificity = TN / (TN + FP)
FPR = FP / (FP + TN) = 1 − Specificity
F1 = 2 × (P × R) / (P + R)
Error Rate = (FP + FN) / Total = 1 − Accuracy

Regression
MAE = (1/n) Σ|Yᵢ − Ŷᵢ|
MSE = (1/n) Σ(Yᵢ − Ŷᵢ)²
RMSE = √MSE
R² = 1 − [Σ(Yᵢ − Ŷᵢ)² / Σ(Yᵢ − Ȳ)²]
Adj R²= 1 − [(1 − R²)(n − 1)/(n − k − 1)]
MAPE = (100/n) Σ |(Yᵢ − Ŷᵢ)/Yᵢ|
sMAPE = (100/n) Σ |Yᵢ − Ŷᵢ| / [(|Yᵢ| + |Ŷᵢ|)/2]
β₁ = Σ(X−X̄)(Y−Ȳ) / Σ(X−X̄)² β₀ = Ȳ − β₁X̄

Logistic
P(Y=1) = 1 / (1 + e^(−z)) where z = β₀ + β₁X₁ + ... + βₙXₙ
Odds = P / (1 − P) = e^z
Logit = ln(P/(1−P)) = z
Odds Ratio = e^β % change in odds = (e^β − 1) × 100

Decision Tree
Entropy(S) = −Σ pᵢ log₂(pᵢ) [0 = pure, 1 = 50/50 binary]
Gini(S) = 1 − Σ pᵢ² [0 = pure, 0.5 = 50/50
binary]
Info Gain = Entropy(parent) − Σ (|Sᵥ|/|S|) × Entropy(Sᵥ)
Gain Ratio = Info Gain / SplitInfo
→ Pick the attribute with the HIGHEST Information Gain / Gini decrease

Distance
Euclidean = √Σ(xᵢ − yᵢ)²
Manhattan = Σ|xᵢ − yᵢ|
Minkowski = (Σ|xᵢ − yᵢ|^p)^(1/p) p=1 Manhattan, p=2 Euclidean
Cosine sim = (A·B) / (‖A‖ × ‖B‖)
Jaccard = |A ∩ B| / |A ∪ B|
Hamming = count of differing positions

Scaling
Min-Max: X' = (X − Xmin)/(Xmax − Xmin) → [0,1]
Z-score: Z = (X − μ)/σ → mean 0, SD 1

Outliers
IQR = Q3 − Q1
Lower fence = Q1 − 1.5 × IQR
Upper fence = Q3 + 1.5 × IQR
Z-score rule: |Z| > 3

Clustering
WCSS/SSE = Σⱼ Σ_{x∈Cⱼ} ‖x − μⱼ‖²
Silhouette s(i) = [b(i) − a(i)] / max{a(i), b(i)}
a(i) = avg distance to own cluster (cohesion)
b(i) = avg distance to nearest other cluster (separation)
Range −1 to +1; want close to +1

Association Rules
Support(A→B) = count(A ∩ B) / N
Confidence(A→B) = Support(A ∩ B) / Support(A) = P(B|A)
Lift(A→B) = Confidence(A→B) / Support(B)
= Support(A∩B) / [Support(A) × Support(B)]
Lift > 1 positive · = 1 independent · < 1 negative (substitutes)
★ Support & Lift are SYMMETRIC. Confidence is NOT.

Recommender
Predicted rating = Σ sim(u,v) × r(v,i) / Σ sim(u,v)

14.2 THE 60-SECOND DECISION TABLE


Signal in the case Answer
Data has an outcome column Supervised
No outcome column Unsupervised
Predicting a number (₹, days, %) Regression
Predicting a category (Yes/No, class) Classification
Need probability + odds ratios + regulator Logistic Regression
explanation
Need IF-THEN rules for frontline staff Decision Tree
Need to quantify impact of each driver on a Linear Regression
number
“Customers similar to this one” KNN
“Segment my customers” K-Means
“Don’t know how many groups; show me Hierarchical
the structure”
“What’s bought together?” / bundling / Association Rules
store layout
“What should we recommend to this user?” Recommender (Collaborative/Content)
Rare event (fraud, disease, churn) Don’t use Accuracy → Recall, F1, AUC
Train 98%, Test 60% Overfitting → prune, more data, CV,
regularise
Distance-based algorithm SCALE FIRST

14.3 THINGS EXAMINERS LOVE (say these)


1. “Holding all other variables constant” — every time you interpret a regression
coefficient.
2. “Accuracy is misleading here because the classes are imbalanced.”
3. “Since a False Negative costs X and a False Positive costs Y, we optimise for
[Recall/Precision] and shift the threshold to Z.”
4. “Lift > 1 confirms a genuine positive association; confidence alone would have
been fooled by the consequent’s baseline popularity.”
5. “Feature scaling is mandatory here because the algorithm is distance-based.”
6. “e^β = 2.51 → odds are 2.51 times higher, i.e. a 151% increase in odds.”
7. “Correlation is not causation — a controlled A/B test is needed to confirm the
lever.”
8. “We cannot extrapolate beyond the observed range of the training data.”
9. “Adjusted R² is the right comparator because the models have different
numbers of predictors.”
10. “K-Means may converge to a local optimum due to random initialisation, so we
run multiple seeds / k-means++.”
11. “The model should assist, not replace, the human decision-maker; and we
must audit it for bias on protected attributes.”

14.4 CLASSIC TRAPS


Trap The right move
Judging an imbalanced classifier on Use Recall / F1 / AUC
Accuracy
Bundling on high Confidence alone Check Lift > 1 first
Encoding a nominal variable as 1, 2, 3 Use one-hot (k−1 dummies)
Mean-imputing a skewed/outlier-heavy Use median
column
Forgetting to scale before KNN/K-Means Scale — the result is otherwise
meaningless
Removing “outliers” in a fraud case The outliers are the target
Comparing models on R² when k differs Use Adjusted R²
Predicting outside the training range Flag it as extrapolation
Saying “the model shows X causes Y” Say “X is associated with Y, controlling
for…”
Using linear regression for a 0/1 target Use logistic
Assuming Confidence(A→B) = It isn’t. Support and Lift are symmetric;
Confidence(B→A) confidence is not.
Tuning the model on the test set That’s leakage — tune on validation, test
once

14.5 KEY NUMBERS TO REMEMBER


Quantity Value
Typical split 70 : 15 : 15 (or 60:20:20, 70:20:10)
Common k in cross-validation 5 or 10
Multicollinearity red flag VIF > 5 (strictly > 10)
Outlier fence multiplier 1.5 × IQR
Z-score outlier cut-off |Z| > 3
Quantity Value
Default classification threshold 0.5
AUC of a random model 0.5
Good AUC > 0.8
Significance level p < 0.05
Max entropy (binary) 1.0
Max Gini (binary) 0.5
Silhouette range −1 to +1 (want → +1)
Lift of independence 1.0
KNN starting k √n, odd for binary
Rule of thumb: time spent on data 60–80% of the project
prep

14.6 ONE-LINE DEFINITIONS (rapid recall)


• Machine Learning — computational methods that improve performance by
acquiring knowledge from experience (historical data) rather than explicit rules.
• Supervised learning — learning a mapping from features to a known target label.
• Unsupervised learning — finding intrinsic structure in data with no target
attribute.
• Overfitting — the model memorises training noise; high train accuracy, low test
accuracy.
• Cross-validation — rotating train/test folds so every observation is tested once.
• Feature engineering — creating better input variables from raw data.
• Homoscedasticity — constant variance of residuals across the range of X.
• Multicollinearity — predictors correlated with each other, destabilising
coefficients.
• Entropy — a measure of disorder/impurity in a node.
• Information Gain — reduction in entropy achieved by a split.
• Pruning — cutting back a tree’s branches to reduce overfitting.
• Lazy learner — a model with no training phase (KNN); all work happens at
prediction time.
• Centroid — the mean point of a cluster.
• Dendrogram — the tree diagram produced by hierarchical clustering.
• Linkage — the rule for measuring distance between two clusters.
• Support — how frequently an itemset appears across all transactions.
• Confidence — the conditional probability of the consequent given the antecedent.
• Lift — how much more likely the consequent is, given the antecedent, versus
baseline.
• Cold start — a recommender’s inability to serve a brand-new user or item.
• Type I error — False Positive (false alarm). Type II error — False Negative (a
miss).

14.7 FINAL EXAM-DAY CHECKLIST


☐ Before any confusion-matrix question, write down which class is Positive. ☐ Label
TP / FP / FN / TN in the margin before computing anything. ☐ Show the formula, then
substitute, then the answer — partial credit lives in the substitution line. ☐ Round to 3
decimals; convert to % where it aids interpretation. ☐ Every numerical answer needs one
sentence of business interpretation. “Recall = 0.60 means we are missing 40% of actual
churners.” ☐ For “which model?” — always state (a) supervised/unsupervised, (b)
regression/classification, (c) the model, (d) three reasons why, (e) the evaluation
metric and why. ☐ For association rules, compute all three metrics, not just the one
asked, then judge on Lift. ☐ In K-Means, keep a running table of assignments per iteration
and stop when they repeat. ☐ Mention data prep (scaling, encoding, imputation) in every
case answer — it is free marks. ☐ Close every case answer with a limitation or ethical
caveat.

End of notes. Chapters 2–6 (through K-Means and KNN), aligned to the four-question
application-oriented exam pattern.

You might also like