Machine Learning Fundamentals for TU BCA: Supervised vs Unsupervised vs Reinforcement Learning with Python

Author: Bhuban Subedi | Subject: Machine Learning / Artificial Intelligence (CACS354) | Semester: Sixth / Seventh Semester


In modern computer science, Artificial Intelligence (AI) and Machine Learning (ML) represent the fastest-growing frontier. Rather than hand-crafting explicit rule-based algorithms for every scenario, Machine Learning algorithms learn patterns and statistical relationships directly from historical training data.

In the Tribhuvan University (TU) BCA Machine Learning / AI elective courses and final-year capstone projects, questions comparing Supervised vs Unsupervised vs Reinforcement Learning, explaining the Bias-Variance Tradeoff, and evaluating classifiers carry significant weightage.

In this guide, we will break down the taxonomy of machine learning, explore core algorithms, understand model evaluation metrics, and implement practical Python examples using Scikit-Learn.


1. The AI, ML, and Deep Learning Hierarchy

+-------------------------------------------------------------------------------+
|                       ARTIFICIAL INTELLIGENCE (AI)                            |
|       Broad field of creating machines that simulate human intelligence       |
|                                                                               |
|       +---------------------------------------------------------------+       |
|       |                   MACHINE LEARNING (ML)                       |       |
|       |    Algorithms that learn statistical patterns from data       |       |
|       |                                                               |       |
|       |       +-----------------------------------------------+       |       |
|       |       |               DEEP LEARNING (DL)              |       |       |
|       |       |  Multi-layered Artificial Neural Networks     |       |       |
|       |       +-----------------------------------------------+       |       |
|       +---------------------------------------------------------------+       |
+-------------------------------------------------------------------------------+

2. The Three Primary Paradigms of Machine Learning

+------------------------------------+------------------------------------+------------------------------------+
| Supervised Learning                | Unsupervised Learning              | Reinforcement Learning (RL)        |
+------------------------------------+------------------------------------+------------------------------------+
| **Labeled Data:** Model is trained | **Unlabeled Data:** Model discovers| **Trial and Error:** Agent learns  |
| on input features $(X)$ paired with| hidden structures and groupings    | optimal policy by taking actions in|
| correct target labels $(y)$.       | without external labels.           | an environment to maximize rewards.|
+------------------------------------+------------------------------------+------------------------------------+
| Tasks: **Regression** (continuous) | Tasks: **Clustering** &            | Tasks: Robotics, Game AI (Chess,   |
| and **Classification** (discrete). | **Dimensionality Reduction**.      | AlphaGo), Autonomous driving.      |
+------------------------------------+------------------------------------+------------------------------------+
| Examples: Linear Regression,       | Examples: K-Means Clustering,      | Examples: Q-Learning, Deep Q-      |
| Logistic Regression, Random Forest.| PCA, Hierarchical Clustering.      | Networks (DQN), Policy Gradient.   |
+------------------------------------+------------------------------------+------------------------------------+

3. Supervised Learning: Regression vs. Classification

   REGRESSION: Predicting Numbers               CLASSIFICATION: Predicting Categories
          y (House Price)                               y (Spam or Not Spam)
          ^                                             ^
          |        / (Trendline)                        |      * * (Class A: Spam)
          |    *  / *                                   |     *  *
          |   *  /                                      |  ----------------- (Decision Boundary)
          |  *  / *                                     |      o o
          +--------------------> x (Size in sq ft)       +-----o---o---------> x (Email word count)
  1. Linear Regression: Models linear relationship between independent variable $X$ and dependent continuous variable $y$:
    $$\mathbf{y = \beta_0 + \beta_1 x + \epsilon}$$
  2. Logistic Regression: Models probability of a binary categorical outcome using the Sigmoid activation function:
    $$\mathbf{P(y=1|x) = \sigma(z) = \frac{1}{1 + e^{-z}}}$$

4. Unsupervised Learning: K-Means Clustering

K-Means partitions $n$ unlabeled data observations into $k$ distinct clusters:
1. Initialize $k$ cluster centroids randomly.
2. Assign each data point to its nearest centroid using Euclidean distance:
$$d(p, q) = \sqrt{\sum_{i=1}^{n} (q_i – p_i)^2}$$
3. Recompute centroids as the mean of all points assigned to that cluster.
4. Repeat until centroids converge and cluster assignments stabilize.


5. Model Overfitting, Underfitting & Bias-Variance Tradeoff

+------------------------------------+------------------------------------+------------------------------------+
| Underfitting (High Bias)           | Optimal Balanced Fit               | Overfitting (High Variance)        |
+------------------------------------+------------------------------------+------------------------------------+
| Model is too simple to capture     | Model captures underlying patterns | Model memorizes noise and outliers |
| underlying data trends.            | and generalizes well to unseen data| in training data; fails on test set|
| (High train error, high test error)| (Low train error, low test error). | (Zero train error, high test error)|
+------------------------------------+------------------------------------+------------------------------------+

6. Practical Python Implementation (Scikit-Learn)

Here is a complete Python script demonstrating data loading, training a Random Forest Classifier, and evaluating metrics:

# Import core machine learning libraries
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

# 1. Load benchmark dataset
iris = load_iris()
X = iris.data  # Features: Sepal Length, Sepal Width, Petal Length, Petal Width
y = iris.target # Target classes: Setosa, Versicolour, Virginica

# 2. Split dataset into 80% Training and 20% Testing sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y
)

# 3. Instantiate and train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# 4. Predict on unseen test data
y_pred = model.predict(X_test)

# 5. Evaluate Performance
acc = accuracy_score(y_test, y_pred)
print(f"=== Model Performance ===")
print(f"Classification Accuracy: {acc * 100:.2f}%\n")
print("Detailed Classification Report:")
print(classification_report(y_test, y_pred, target_names=iris.target_names))

Frequently Asked Questions (FAQ)

Q1: What is the purpose of Confusion Matrix in Classification?

A Confusion Matrix summarizes prediction results by comparing Actual vs Predicted labels across 4 quadrants: True Positives (TP), True Negatives (TN), False Positives (FP – Type I Error), and False Negatives (FN – Type II Error).

Q2: What is Cross-Validation ($k$-Fold CV)?

$k$-Fold Cross-Validation splits the dataset into $k$ equal subsets. The model is trained on $k-1$ folds and evaluated on the remaining fold, repeating $k$ times to ensure robust, unbiased performance estimation.

LEAVE A REPLY

Please enter your comment!
Please enter your name here