Artificial Intelligence (AI)
Module 6
Machine Learning
Introduction to Machine Learning (ML)
Definition: Machine Learning is a subfield of Artificial Intelligence that enables systems to
automatically learn and improve from experience without being explicitly programmed.
Key Idea: Instead of writing explicit rules, we provide data → the machine learns patterns →
makes predictions or decisions.
Arthur Samuel (1959): “Machine Learning is the field of study that gives computers the
ability to learn without being explicitly programmed.”
Example:
• Spam filter learns from labeled emails (spam / not spam).
• Medical diagnosis model learns from patient data and predicts diseases.
• Image recognition model learns to identify faces or objects.
Core Components of an ML System:
1. Data: Input examples or observations (features).
2. Model: Mathematical function or algorithm that maps inputs → outputs.
3. Learning Algorithm: Adjusts model parameters to reduce prediction error.
4. Loss Function: Measures the difference between predicted and actual values.
5. Evaluation Metric: Accuracy, precision, recall, etc., to test performance.
Types of Learning: Machine Learning can be categorized based on the type of feedback
available from data.
Type Input Data Output/Goal Example
Supervised Labeled data (input → Learn mapping from Predict house price,
Learning output known) input to output classify emails
Customer
Unsupervised Discover hidden
Unlabeled data segmentation,
Learning structure/patterns
clustering
Reinforcement Agent interacts with Learn actions that Game playing, robot
Learning environment maximize rewards navigation
A. Supervised Learning
Definition: Algorithm learns from labeled examples, where each training sample has an input
and its correct output (label).
Mathematical Form: Given dataset 𝐷 = {(𝑥1 , 𝑦1 ), (𝑥2 , 𝑦2 ), … , (𝑥𝑛 , 𝑦𝑛 )}
The model learns a mapping function: 𝑓(𝑥) ≈ 𝑦
Examples:
• Spam detection (email → spam/not spam)
• Handwritten digit recognition (image → digit 0-9)
• Disease classification (features → disease label)
Types of Supervised Tasks:
• Classification: Output is discrete (e.g., Yes/No, categories).
• Regression: Output is continuous (e.g., temperature, price).
B. Unsupervised Learning
Definition: Algorithm works with unlabeled data: it discovers structure, relationships, or
clusters among data points.
Goal: Find hidden patterns or reduce data dimensions.
Examples:
• Market segmentation (group customers by behavior)
• Document/topic clustering
• Dimensionality reduction (PCA)
Common Techniques:
• Clustering Algorithms: k-Means, Hierarchical, DBSCAN
• Dimensionality Reduction: PCA, t-SNE
C. Reinforcement Learning (RL)
Definition: A learning paradigm where an agent learns by interacting with an environment to
achieve a goal through trial and error.
Elements:
• Agent: Learner or decision-maker.
• Environment: External system the agent interacts with.
• State (s): Current situation of the agent.
• Action (a): Choice the agent makes.
• Reward (r): Feedback received from environment.
• Policy (π): Strategy mapping states → actions.
Objective: Maximize the cumulative reward over time.
Example:
• AlphaGo learning to play Go via rewards for winning.
• A robot learning to walk — positive reward for staying upright.
Learning Loop: Agent → takes Action (aₜ) → Environment → gives Reward (rₜ) & next State
(sₜ₊₁) → Agent updates policy.
Classification Model and Learning Process: Classification is one of the most common
tasks in supervised learning.
General Steps in Machine Learning
Step Description
1. Data Collection Gather raw data from sensors, databases, or files.
2. Data Preprocessing Handle missing values, normalize, encode categories.
3. Feature
Choose relevant attributes that influence output.
Extraction/Selection
Divide data into Training (≈70–80%) and Testing (≈20–
4. Splitting Dataset
30%).
5. Model Training Feed training data to the ML algorithm to learn patterns.
6. Model Testing Evaluate performance using unseen test data.
7. Performance Metrics Compute accuracy, precision, recall, F1-score, etc.
Classification Terminology
• Feature Vector (x): Input attributes (e.g., age, salary).
• Label (y): Output class (e.g., "Approved"/"Rejected").
• Decision Boundary: The line/surface that separates classes.
• Confusion Matrix:
Actual\Predicted Positive Negative
Positive TP FN
Negative FP TN
Common Classification Algorithms
A. k-Nearest Neighbors (kNN)
Concept: A simple instance-based learning algorithm that classifies a data point based on the
majority class among its 𝑘 nearest neighbors.
Algorithm Steps:
1. Choose number of neighbors 𝑘.
2. Compute distance (usually Euclidean) between test point and all training points.
3. Select the 𝑘 nearest neighbors.
4. Assign the class label that is most frequent among those neighbors.
Distance Formula (Euclidean):
𝑁
𝑑(𝑥𝑖 , 𝑥𝑗 ) = √ ∑(𝑥𝑖𝑛 − 𝑥𝑗𝑛 )2
𝑛=1
Advantages:
• Simple and easy to implement.
• No training phase (lazy learning).
Disadvantages:
• Slow for large datasets.
• Sensitive to irrelevant features and scale.
Example: Classify an unknown fruit as “apple” or “orange” based on its color, weight, and
texture. For instance, kNN checks which known fruits are closest in feature space.
B. Decision Tree Classifier
Concept: A tree-like model where each internal node tests a feature, each branch represents
an outcome, and each leaf node represents a class label.
Key Idea: Split data recursively based on features that provide the highest information gain
(i.e., best separate the classes).
Metrics:
• Entropy: 𝐸𝑛𝑡𝑟𝑜𝑝𝑦(𝑆) = − ∑𝑐𝑖=1 𝑝𝑖 log2 𝑝𝑖
|𝑆𝑣 |
• Information Gain: 𝐼𝐺(𝑆, 𝐴) = 𝐸𝑛𝑡𝑟𝑜𝑝𝑦(𝑆) − ∑𝑣∈𝑉𝑎𝑙𝑢𝑒𝑠(𝐴) |𝑆|
𝐸𝑛𝑡𝑟𝑜𝑝𝑦(𝑆𝑣 )
Advantages:
• Easy to visualize and interpret.
• Works for both categorical and numerical data.
Disadvantages:
• Can overfit noisy data.
• Slight changes in data can change the structure.
Example:
Loan approval decision tree based on income, credit score, and collateral.
C. Random Forest Classifier
Concept: An ensemble learning method that combines multiple decision trees to improve
accuracy and reduce overfitting.
Working Principle:
1. Create multiple subsets of the training data (bootstrapping).
2. Train a decision tree on each subset.
3. For prediction, take the majority vote of all trees.
Advantages:
• High accuracy and robust to noise.
• Works well for large datasets.
• Reduces overfitting compared to a single tree.
Disadvantages:
• Less interpretable.
• Requires more computation and memory.
Example:
Used in medical diagnosis, stock prediction, and credit scoring systems.
D. Support Vector Machine (SVM)
Concept: A powerful classifier that finds the optimal hyperplane that separates data points of
different classes with maximum margin.
Mathematical Idea: For data points (𝑥𝑖 , 𝑦𝑖 ), find hyperplane
𝑤∙𝑥+𝑏 =0
that maximizes the margin between classes.
Margin: Distance between the hyperplane and the nearest data points (support vectors).
For Linearly Separable Data:
2
Maximize ‖𝑤‖ subject to 𝑦𝑖 (𝑤 ∙ 𝑥𝑖 + 𝑏) ≥ 1
Kernel Trick: Used for non-linear data, by transforming input into higher-dimensional
space:
• Linear kernel
• Polynomial kernel
• Radial Basis Function (RBF) kernel
Advantages:
• Effective for high-dimensional data.
• Works well with clear margin separation.
Disadvantages:
• Training can be slow for large datasets.
• Needs careful kernel and parameter tuning.
Example:
SVM for classifying cancerous vs. non-cancerous cells using biopsy data.
Comparison and Applications
Algorithm Type Strengths Limitations Typical Applications
Instance- Slow for large Pattern recognition,
kNN Simple, intuitive
based data anomaly detection
Decision Model-
Easy to interpret Overfitting Credit scoring, risk analysis
Tree based
Random High accuracy, Less
Ensemble Bioinformatics, finance
Forest robust interpretable
Model- Works in high Parameter Image recognition,
SVM
based dimensions sensitive bioinformatics