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 — Supervised and Unsupervised Learning

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

Supervised and Unsupervised Learning

Machine Learning (ML) is the branch of AI in which systems learn patterns from data rather than being explicitly programmed with rules. In data analytics, ML is what powers predictive analytics (supervised) and descriptive analytics (unsupervised).

Supervised Learning

The algorithm learns from labelled training data — every input example carries the correct output. The goal is a function f(X) → Y that generalises to unseen data.

Training data:
   X (features/inputs)              Y (label/target)
   ────────────────────             ────────────────
   hours=5, attendance=80%    ->    Pass
   hours=2, attendance=45%    ->    Fail
   hours=8, attendance=92%    ->    Pass

Learn f  ->  then predict for a NEW student with hours=6, attendance=75%
TypeTarget variableQuestionExamples
ClassificationCategorical (discrete classes)"Which class?"Spam/not-spam, pass/fail, disease/no-disease, digit 0–9
RegressionContinuous (numeric)"How much?"House price, temperature, sales forecast, marks

Unsupervised Learning

The algorithm receives only inputs, no labels, and must discover structure on its own.

TypeGoalExamples
ClusteringGroup similar recordsCustomer segmentation, document grouping, image compression
Association rule miningFind items that co-occurMarket basket analysis ("bread → butter")
Dimensionality reductionCompress features while retaining informationPCA before modelling, visualization in 2-D
Anomaly detectionIdentify unusual recordsFraud detection, machine-fault detection

Reinforcement Learning (Brief)

An agent interacts with an environment, taking actions and receiving rewards or penalties, learning a policy that maximises cumulative reward. Used in game playing (AlphaGo), robotics, dynamic pricing, and recommendation sequencing. It needs neither labels nor a fixed dataset — it needs an environment to experiment in.

Comparison — The Standard Exam Table

BasisSupervised LearningUnsupervised Learning
Input dataLabelled (X and Y both given)Unlabelled (only X)
GoalPredict the output for new inputsDiscover hidden structure
FeedbackDirect — the correct answer is knownNone
Analytics typePredictiveDescriptive
Main tasksClassification, regressionClustering, association, dimensionality reduction
AlgorithmsNaïve Bayes, KNN, linear regression, decision tree, SVM, random forestK-Means, hierarchical clustering, DBSCAN, Apriori, PCA
EvaluationObjective — accuracy, RMSE, F1 vs known truthSubjective — silhouette score, domain interpretation
Data preparation costHigh (labelling is expensive/manual)Low (no labelling needed)
Computational complexityGenerally simplerOften harder
Number of classesKnown in advanceUnknown; must be discovered/chosen
ExamplePredict whether a loan will defaultSegment customers into behavioural groups

Semi-Supervised and Self-Supervised Learning

  • Semi-supervised — a small labelled set plus a large unlabelled set. Common in practice, because labelling is the bottleneck (e.g. 1,000 labelled medical images plus 100,000 unlabelled ones).
  • Self-supervised — labels generated automatically from the data's own structure (e.g. predicting a masked word). This is how modern language models are pre-trained.

The Supervised Learning Workflow

The cardinal rule: never evaluate a model on data it was trained on. Training accuracy is always optimistic; only performance on held-out data predicts real-world behaviour.

Overfitting and Underfitting

UnderfittingGood FitOverfitting
Model complexityToo simpleBalancedToo complex
Training errorHighLowVery low
Test errorHighLowHigh
BiasHighBalancedLow
VarianceLowBalancedHigh
CauseToo few features, over-regularisedToo many parameters, too little data, noise memorised
FixAdd features, use a more complex modelMore data, regularisation, simpler model, cross-validation, early stopping

The bias-variance trade-off:

   Total Error = Bias² + Variance + Irreducible Error

   Bias     = error from wrong assumptions (model too simple)
   Variance = error from sensitivity to the particular training sample
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_squared_error

np.random.seed(42)
X = np.sort(np.random.uniform(0, 10, 30)).reshape(-1, 1)
y = 2 * X.ravel() + 5 + np.random.normal(0, 3, 30)

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

for degree in [1, 3, 15]:
    model = make_pipeline(PolynomialFeatures(degree), LinearRegression())
    model.fit(X_train, y_train)
    train_rmse = np.sqrt(mean_squared_error(y_train, model.predict(X_train)))
    test_rmse  = np.sqrt(mean_squared_error(y_test,  model.predict(X_test)))
    print(f"Degree {degree:2d}: train RMSE = {train_rmse:6.3f}, test RMSE = {test_rmse:8.3f}")
# Degree  1: train RMSE =  2.708, test RMSE =    2.906   <- good fit
# Degree  3: train RMSE =  2.605, test RMSE =    3.297
# Degree 15: train RMSE =  1.183, test RMSE =  108.402   <- OVERFITTING: memorised the noise

Train-Test Split and Cross-Validation

from sklearn.model_selection import KFold, cross_val_score

# Simple split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)
print(f"Train: {len(X_train)}, Test: {len(X_test)}")

# k-fold cross-validation — every observation is used for testing exactly once
model = LinearRegression()
scores = cross_val_score(model, X, y, cv=5, scoring="r2")
print("Fold R² scores:", scores.round(3))
print(f"Mean R² = {scores.mean():.3f} (+/- {scores.std():.3f})")

Why cross-validation beats a single split: one random split can be lucky or unlucky. Averaging over k folds gives a far more stable estimate, and uses every record for both training and testing.

VariantDescription
k-foldStandard; k = 5 or 10
Stratified k-foldPreserves class proportions in each fold — essential for imbalanced classification
Leave-One-Out (LOOCV)k = n; maximum data use, very expensive
Time-series splitTrain only on the past, test on the future — never shuffle time-ordered data

The next lessons dive into each task type: classification, then regression, then the specific algorithms named in the syllabus.