Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3 — Classification

Lesson 25 of 46 in the free Introduction to Data Analytics notes on Siksha Sarovar, written by Rohit Jangra.

Classification

Classification is the supervised learning task of predicting a categorical class label for a new observation, using a model learned from labelled training data.

Types of Classification

TypeNumber of classesExample
Binary2Spam/Not-spam, Pass/Fail, Fraud/Legitimate
Multi-class3+, one label per recordDigit 0–9, species setosa/versicolor/virginica
Multi-labelMultiple labels per recordA news article tagged both "politics" and "economy"
ImbalancedClasses very unequally representedFraud (0.1%) vs legitimate (99.9%)

Major Classification Algorithms

AlgorithmCore ideaStrengthsWeaknesses
Naïve BayesBayes' theorem with a feature-independence assumptionExtremely fast, great for text, works with small dataIndependence assumption rarely true
K-Nearest NeighbourMajority vote of the k closest training pointsNo training phase, simple, non-linear boundariesSlow at prediction, needs scaling, curse of dimensionality
Decision TreeRecursive splits maximising purityHighly interpretable, handles mixed data typesOverfits easily
Random ForestEnsemble of many trees, majority voteHigh accuracy, robust to overfittingLess interpretable, slower
Logistic RegressionSigmoid of a linear combination → probabilityInterpretable coefficients, probabilistic outputOnly linear decision boundaries
SVMMaximum-margin separating hyperplaneEffective in high dimensionsSlow on large data, hard to tune
Neural NetworksLayers of weighted non-linear transformsLearns very complex patternsNeeds lots of data, a black box

Evaluating a Classifier — The Confusion Matrix

                       PREDICTED
                    Positive    Negative
                 ┌───────────┬───────────┐
        Positive │    TP     │    FN     │   <- actual positives
 ACTUAL          │(hit)      │(miss,     │
                 │           │ Type II)  │
                 ├───────────┼───────────┤
        Negative │    FP     │    TN     │   <- actual negatives
                 │(false     │(correct   │
                 │ alarm,    │ rejection)│
                 │ Type I)   │           │
                 └───────────┴───────────┘
TermMeaning
TP (True Positive)Predicted positive, actually positive ✓
TN (True Negative)Predicted negative, actually negative ✓
FP (False Positive)Predicted positive, actually negative ✗ (Type I error)
FN (False Negative)Predicted negative, actually positive ✗ (Type II error)

Evaluation Metrics

                       TP + TN
   Accuracy   = ───────────────────────      overall correctness
                 TP + TN + FP + FN

                     TP
   Precision  = ───────────      of everything predicted positive, how much was right?
                 TP + FP

                     TP
   Recall     = ───────────      of all actual positives, how many did we catch?
   (Sensitivity) TP + FN

                     TN
   Specificity = ───────────     of all actual negatives, how many did we correctly reject?
                  TN + FP

                  2 × Precision × Recall
   F1-Score   = ──────────────────────────    harmonic mean of precision and recall
                   Precision + Recall

Worked Example — Fraud Detection

Out of 1,000 transactions, 50 are fraudulent. A model predicts 40 as fraud, of which 30 are genuinely fraudulent.

TP = 30                        (fraud correctly caught)
FP = 40 − 30 = 10              (legitimate wrongly flagged)
FN = 50 − 30 = 20              (fraud missed)
TN = 1000 − 30 − 10 − 20 = 940

Accuracy    = (30 + 940) / 1000        = 0.970  ->  97%
Precision   = 30 / (30 + 10) = 30/40   = 0.750  ->  75%
Recall      = 30 / (30 + 20) = 30/50   = 0.600  ->  60%
Specificity = 940 / (940 + 10)         = 0.989  ->  98.9%
F1          = 2(0.75 × 0.60)/(0.75 + 0.60) = 0.90/1.35 = 0.667

Interpretation: 97% accuracy sounds excellent, but the model misses 40% of all fraud (recall = 0.60). This is the accuracy paradox — on imbalanced data, always predicting "not fraud" would score 95% accuracy while catching nothing.

Precision vs Recall — Which Matters More?

ScenarioPrioritiseReason
Cancer screeningRecallMissing a real case (FN) is catastrophic; a false alarm just means another test
Spam filterPrecisionMarking an important email as spam (FP) is worse than letting a spam through
Fraud detectionRecall (usually)Missed fraud costs money; false alarms cost a phone call
Search resultsPrecisionUsers only look at the top results
Balanced needF1-scoreHarmonic mean penalises a poor value on either side

There is always a trade-off: lowering the decision threshold catches more positives (↑ recall) but flags more negatives wrongly (↓ precision).

ROC Curve and AUC

ROC curve: plot True Positive Rate (Recall) against False Positive Rate (1 − Specificity)
           at every possible classification threshold.

AUC (Area Under Curve):
   1.0  = perfect classifier
   0.9+ = excellent
   0.8  = good
   0.7  = fair
   0.5  = no better than random guessing (the diagonal line)

Python — A Complete Classification Workflow

import pandas as pd
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import (confusion_matrix, accuracy_score, precision_score,
                             recall_score, f1_score, classification_report, roc_auc_score)

data = load_breast_cancer()
X, y = data.data, data.target      # 0 = malignant, 1 = benign

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y   # stratify keeps class balance
)

scaler = StandardScaler()
X_train_s = scaler.fit_transform(X_train)      # fit ONLY on training data
X_test_s = scaler.transform(X_test)            # transform test with the same scaler

model = LogisticRegression(max_iter=5000)
model.fit(X_train_s, y_train)
y_pred = model.predict(X_test_s)
y_proba = model.predict_proba(X_test_s)[:, 1]

print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))
# [[51  2]
#  [ 1 89]]

print(f"\nAccuracy : {accuracy_score(y_test, y_pred):.4f}")
print(f"Precision: {precision_score(y_test, y_pred):.4f}")
print(f"Recall   : {recall_score(y_test, y_pred):.4f}")
print(f"F1-Score : {f1_score(y_test, y_pred):.4f}")
print(f"ROC-AUC  : {roc_auc_score(y_test, y_proba):.4f}")

print("\n", classification_report(y_test, y_pred, target_names=data.target_names))
# Visualising the confusion matrix and ROC curve
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import roc_curve

fig, axes = plt.subplots(1, 2, figsize=(13, 5))

cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", ax=axes[0],
            xticklabels=data.target_names, yticklabels=data.target_names)
axes[0].set_xlabel("Predicted"); axes[0].set_ylabel("Actual")
axes[0].set_title("Confusion Matrix")

fpr, tpr, thresholds = roc_curve(y_test, y_proba)
axes[1].plot(fpr, tpr, color="#168B99", linewidth=2,
             label=f"AUC = {roc_auc_score(y_test, y_proba):.3f}")
axes[1].plot([0, 1], [0, 1], "k--", label="Random (AUC = 0.5)")
axes[1].set_xlabel("False Positive Rate"); axes[1].set_ylabel("True Positive Rate")
axes[1].set_title("ROC Curve"); axes[1].legend()

plt.tight_layout(); plt.show()
# Comparing several classifiers on the same data
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier

models = {
    "Naive Bayes":        GaussianNB(),
    "KNN (k=5)":          KNeighborsClassifier(n_neighbors=5),
    "Decision Tree":      DecisionTreeClassifier(random_state=42),
    "Random Forest":      RandomForestClassifier(n_estimators=100, random_state=42),
    "Logistic Regression": LogisticRegression(max_iter=5000),
}

results = []
for name, m in models.items():
    m.fit(X_train_s, y_train)
    pred = m.predict(X_test_s)
    results.append({
        "Model": name,
        "Accuracy": round(accuracy_score(y_test, pred), 4),
        "Precision": round(precision_score(y_test, pred), 4),
        "Recall": round(recall_score(y_test, pred), 4),
        "F1": round(f1_score(y_test, pred), 4),
    })

print(pd.DataFrame(results).sort_values("F1", ascending=False).to_string(index=False))

Handling Imbalanced Classes

TechniqueDescription
Stratified samplingPreserve class ratios in train/test splits and CV folds
Oversampling / SMOTEDuplicate or synthesise minority-class examples
UndersamplingRandomly drop majority-class examples
Class weightsclass_weight="balanced" penalises minority errors more heavily
Threshold tuningLower the decision threshold below 0.5 to boost recall
Right metricsUse F1, precision-recall AUC, or Cohen's kappa — never plain accuracy

The next lessons cover the two specific classifiers in this syllabus — Naïve Bayes and KNN — but first, the other supervised task: regression.