0% found this document useful (0 votes)
7 views34 pages

Unit 3 DataScience

Supervised Learning is a machine learning technique that uses labeled data to train models for classification and regression tasks, focusing on learning patterns and minimizing prediction errors. It involves several steps including problem definition, data collection, preprocessing, and model evaluation. Common algorithms include Linear Regression, Logistic Regression, Decision Trees, and Support Vector Machines, with applications ranging from house price prediction to medical diagnosis.

Uploaded by

nandinipechetti
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)
7 views34 pages

Unit 3 DataScience

Supervised Learning is a machine learning technique that uses labeled data to train models for classification and regression tasks, focusing on learning patterns and minimizing prediction errors. It involves several steps including problem definition, data collection, preprocessing, and model evaluation. Common algorithms include Linear Regression, Logistic Regression, Decision Trees, and Support Vector Machines, with applications ranging from house price prediction to medical diagnosis.

Uploaded by

nandinipechetti
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

Supervised Learning

Machine Learning Techniques


Machine Learning (ML) is a branch of Artificial Intelligence that allows computers to learn from
data and make predictions or decisions without being explicitly programmed.
Machine learning techniques are mainly divided into three types:
• Supervised Learning
• Unsupervised Learning
• Reinforcement Learning
1. Supervised Learning
Definition
Supervised learning is a machine learning technique where the model learns from labeled data.
This means the dataset contains:
• Input variables (features)
• Output variable (label)
The model learns the relationship between input and output.
Example
Size Bedrooms Price
1200 2 500000
1500 3 650000
Features → Size, Bedrooms
Label → Price
The model learns how house size affects price.
Algorithms
Common supervised learning algorithms include:
• Linear Regression
• Logistic Regression
• Decision Tree
• K-Nearest Neighbors (KNN)
• Support Vector Machine (SVM)
Applications
• Email spam detection
• House price prediction
• Medical diagnosis
• Credit risk prediction
• Stock price prediction
2. Unsupervised Learning
Definition
Unsupervised learning is a technique where the model learns from unlabeled data.
There is no target output variable.
The algorithm tries to find hidden patterns or groups in the data.
Example
Customer purchase data:
Customer Purchase Amount
C1 5000
C2 5200
C3 1000
C4 1200
The algorithm may group customers into:
High spenders
Low spenders
This is called clustering.
Algorithms
Common unsupervised learning algorithms:
• K-Means Clustering
• Hierarchical Clustering
• DBSCAN
• Principal Component Analysis (PCA)
Applications
• Customer segmentation
• Market basket analysis
• Fraud detection
• Social network analysis
• Recommendation systems
3. Reinforcement Learning
Definition
Reinforcement learning is a technique where an agent learns by interacting with the environment.
The system receives:
Reward for correct actions
Penalty for wrong actions
Over time, it learns the best strategy.
Example
Teaching a robot to walk.
If it walks correctly → reward
If it falls → penalty
The robot gradually learns the best movement.
Algorithms
Common reinforcement learning algorithms:
• Q-Learning
• SARSA
• Deep Q Networks (DQN)
• Policy Gradient Methods
Applications
• Self-driving cars
• Robotics
• Game playing (Chess, Go)
• Recommendation systems
• Traffic signal control
Comparison of Machine Learning Techniques
Technique Data Type Goal
Supervised Learning Labeled data Predict outcomes
Unsupervised Learning Unlabeled data Find hidden patterns
Reinforcement Learning Interaction with environment Learn best actions

Steps to Build a Machine Learning Model (Brief)


Machine Learning models learn patterns from data and use them to make predictions or decisions
automatically. Building a model usually follows several important steps.
1. Define the Problem
First, clearly understand what problem you want to solve.
Examples:
Predict house prices → Regression problem
Detect spam emails → Classification problem
You must decide:
What the input data is
What the expected output is.
2. Collect Data
Collect relevant data from different sources such as:
Databases
Surveys
Sensors
Public datasets (Kaggle, UCI)
The quality and quantity of data strongly affect model performance.
3. Data Cleaning and Preprocessing
Raw data is usually messy and must be prepared.
Common preprocessing tasks:
Remove duplicate data
Handle missing values
Normalize or scale data
Convert categorical data into numerical form
This step ensures the data is suitable for training the model.
4. Exploratory Data Analysis (EDA)
EDA helps understand the dataset before building a model.
Tasks include:
Visualizing data with graphs
Checking distributions
Finding relationships between variables
Detecting outliers or anomalies.
5. Feature Selection and Engineering
Features are the input variables used by the model.
This step involves:
Selecting the most important features
Removing irrelevant features
Creating new useful features
Good features improve model accuracy.
6. Split the Dataset
The dataset is divided into:
Training Set → used to train the model
Validation Set → used to tune parameters
Test Set → used to evaluate final performance.
7. Choose a Machine Learning Algorithm
Select the appropriate algorithm based on the problem.
Examples:
Linear Regression
Decision Trees
Random Forest
Support Vector Machine (SVM)
Neural Networks.
8. Train the Model
Training means feeding the training data to the algorithm so it can learn patterns and
relationships.
The model adjusts its parameters to minimize prediction errors.
9. Evaluate the Model
Test the model using unseen data to measure its performance.
Common evaluation metrics:
Accuracy
Precision
Recall
F1-score
RMSE or MAE.
10. Hyperparameter Tuning
Improve the model by adjusting hyperparameters such as:
learning rate
number of trees
model depth
Techniques like Grid Search and Cross Validation are often used.
11. Deploy the Model
After training, the model is integrated into real applications such as:
Web applications
Mobile apps
Cloud systems.
12. Monitor and Maintain the Model
Even after deployment, the model must be monitored.
If performance decreases due to new data, the model should be updated or retrained.
Supervised Learning
Supervised Learning is a type of Machine Learning where a model learns from labeled data.
Each training example consists of input features (X) and a target output (Y).
The objective is to learn a mapping function f: X → Y that can accurately predict outputs for
unseen data.

Basic Concept
In supervised learning, the dataset contains input-output pairs (X, Y).
X represents independent variables (features), and Y represents the dependent variable (label).
The model learns a relationship between X and Y from training data.

Labeled Data
Labeled data means that the correct output is already known for each input.
Example:
Hours Studied → Exam Result (Pass/Fail)
The algorithm learns how study hours influence the result.

Objective of Supervised Learning


• Learn patterns from historical data.
• Minimize prediction error using a loss function.
• Generalize well to new, unseen data.

Types of Supervised Learning

A) Classification
In classification, the output variable is categorical.
Examples:
• Spam / Not Spam
• Disease / No Disease
• Pass / Fail
• Image classification (Cat/Dog)
Types of Classification:
• Binary Classification (two classes)
• Multi-class Classification (more than two classes)
• Multi-label Classification (multiple labels per instance)

Common Algorithms:
• Linear Regression
• Logistic Regression
• Decision Tree
• K-Nearest Neighbors (KNN)
• Support Vector Machine (SVM)

B) Regression
In regression, the output variable is continuous (real-valued).
Examples:
• House price prediction
• Temperature prediction
• Salary estimation
• Stock price forecasting

Common Algorithms:
• Linear Regression
• Polynomial Regression
• Support Vector Regression
• Random Forest Regression
• Neural Networks

Mathematical Representation
Supervised learning assumes:
Y = f(X) + ε
Where f(X) is the true function and ε is random noise.
The goal is to approximate f(X) using training data.

Summary:
Supervised Learning uses labeled data to train models for classification and regression tasks.
It focuses on learning patterns, minimizing error, and making accurate predictions on new
data.
First Steps in Supervised Learning
Supervised learning follows a structured pipeline. The quality of the final model depends heavily
on how carefully the initial steps are performed. Below is a detailed academic explanation of
each step.
Step 1: Problem Definition
Problem definition is the most critical step in supervised learning.
1.1 Identify the Objective
Clearly define:
What do you want to predict?
Is it a classification problem or a regression problem?
Example 1:
Predict whether a student will pass or fail
→ Classification problem
Example 2:
Predict house price
→ Regression problem
1.2 Identify Input Features (X)
Features are measurable properties used to make predictions.
Example (Student Performance Prediction):
Feature Description
Study Hours Number of hours studied
Attendance Percentage attendance
Internal Marks Mid-term score
These are independent variables.
1.3 Identify Target Variable (Y)
The output variable we want to predict.
Example:
Pass / Fail (Classification)
Final Exam Score (Regression)
1.4 Define Evaluation Metric
Before building the model, decide:
Accuracy? (for classification)
MSE or RMSE? (for regression)
Without clear metrics, model performance cannot be measured properly.
Step 2: Data Collection
Data is the foundation of supervised learning. High-quality data leads to high-quality models.
2.1 Sources of Data
Surveys
Sensors (IoT devices)
Databases
APIs
Web scraping
Public datasets (e.g., Kaggle, UCI Repository)
2.2 Data Quantity
More data generally improves performance because:
It reduces variance
Improves generalization
However, quality is more important than quantity.
2.3 Data Relevance
Collected data must:
✔ Be relevant to the problem
✔ Represent real-world scenarios
✔ Avoid bias
Example:
If predicting rainfall in India, data from Europe may not be relevant.
Step 3: Data Preprocessing
Raw data is rarely clean. Preprocessing ensures the model can learn effectively.
3.1 Handling Missing Values
Missing values can be handled by:
Removing rows
Replacing with mean/median
Using predictive imputation
Example:
If Age is missing:
Replace with average age of dataset.
3.2 Handling Outliers
Outliers are extreme values that distort model learning.
Example:
Salary values: 20k, 30k, 40k, 10,000k
The last value is an outlier.
Techniques:
Z-score method
IQR method
3.3 Feature Scaling
Some algorithms (like SVM, kNN) require scaling.
Common scaling methods:
Normalization (Min-Max Scaling)
Standardization (Z-score Scaling)
Example:
Age range: 18–60
Salary range: 20,000–2,00,000
Without scaling, salary dominates learning.
3.4 Encoding Categorical Variables
Algorithms require numerical input.
Example:
Gender:
Male → 0
Female → 1
Techniques:
Label Encoding
One-Hot Encoding
3.5 Feature Selection
Remove irrelevant features to:
Reduce overfitting
Improve accuracy
Reduce computation time
Step 4: Dataset Splitting
Splitting ensures proper evaluation and avoids overfitting.
4.1 Training Set (70%)
Used to:
Learn model parameters
Fit the model
The model sees this data during training.
4.2 Validation Set (15%)
Used to:
Tune hyperparameters
Select best model
Example:
Choosing:
Value of C in SVM
Number of trees in Random Forest
4.3 Test Set (15%)
Used to:
Evaluate final model performance
Provide unbiased performance estimate
Important:
The test set must not be used during training.
Why Dataset Splitting is Important?
If we train and test on the same data:
Model may memorize patterns
Performance appears high
Poor real-world performance
This problem is called Overfitting.
Alternative: Cross-Validation
Instead of fixed 70-15-15 split, we can use:
k-Fold Cross-Validation
Advantages:
✔ Better performance estimate
✔ Uses entire dataset efficiently
Complete Example
Problem: Predict loan approval.
Step 1: Define problem → Classification
Step 2: Collect data → Income, Credit Score, Age
Step 3: Preprocess data → Handle missing values, scale features
Step 4: Split dataset → 70% train, 15% validation, 15% test
Step 5: Train model
Step 6: Evaluate accuracy
Supervised Learning Algorithms
1. Linear Regression
Definition:
Linear Regression is a supervised learning algorithm used to predict continuous numerical
values. It assumes a linear relationship between input and output.
The relationship is represented by:
y=mx+b
Where
x = input variable
y = predicted output
m = slope
b = intercept
Example
Predict house price based on house size.
House Size ([Link]) Price (₹ Lakhs)
1000 30
1500 45
2000 60
The model learns the relationship between size and price.
If a new house has 1800 [Link], the model predicts its price based on the learned line.
Applications
• House price prediction
• Sales forecasting
• Temperature prediction
2. Logistic Regression
Definition
Logistic Regression is used for classification problems where the output belongs to specific
categories.
It predicts the probability of belonging to a class.
Example
Email spam detection.
Email Message Label
“Win money now” Spam
“Meeting at 5 PM” Not Spam
The model learns patterns from these emails.
If a new email arrives:
“Congratulations! You won a prize”
The algorithm calculates probability:
Spam probability = 0.9
Since it is high, the email is classified as Spam.
Applications
Spam detection
Disease prediction
Fraud detection
3. Decision Tree
Definition
A Decision Tree is a tree-structured model used for classification and regression.
It splits data based on decision rules.
Example
Suppose we want to decide whether to play cricket.
Weather Play Cricket
Sunny No
Rainy No
Cloudy Yes
Decision Tree:
Weather?
| | |
Sunny Rainy Cloudy
| | |
No No Yes
The model uses conditions to make decisions.
Applications
• Loan approval
• Medical diagnosis
• Customer classification
4. Random Forest
Definition
Random Forest is an ensemble algorithm that combines many decision trees.
Each tree makes a prediction, and the final prediction is based on majority voting.
Example
Predict whether a patient has a disease.
Tree Prediction
Tree 1 Disease
Tree 2 No Disease
Tree 3 Disease
Tree 4 Disease
Final result → Disease (majority vote).
Applications
• Medical diagnosis
• Credit scoring
• Stock market prediction
5. Support Vector Machine (SVM)
Definition
Support Vector Machine is used mainly for classification problems.
It separates data into classes using a boundary called a hyperplane.
Example
Classifying fruits using weight.
Weight Fruit
120g Apple
150g Apple
250g Orange
270g Orange
SVM finds the best boundary that separates apples and oranges.
New fruit weight = 240g
The model classifies it as Orange.
Applications
• Image classification
• Face recognition
• Text classification
6. K-Nearest Neighbors (KNN)
Definition
KNN is a simple algorithm that classifies data based on the nearest neighbors.
It checks the k closest data points and assigns the majority class.
Example
Suppose we classify a fruit based on weight and color.
Nearby fruits:
Fruit Color
Apple Red
Apple Red
OrangeOrange
If most nearby fruits are apples, the new fruit is also classified as Apple.
Applications
• Recommendation systems
• Pattern recognition
• Image classification
Final Summary
Algorithm Example
Linear Regression Predict house price
Logistic Regression Spam email detection
Decision Tree Loan approval
Random Forest Disease prediction
SVM Image classification
KNN Product recommendation
In simple words
Linear Regression → Predict numbers
Logistic Regression → Predict categories
Decision Tree → Rule-based decisions
Random Forest → Many trees combined
SVM → Best separating boundary
KNN → Check nearest data points

Training, Validation, and Test Sets

Introduction
In supervised learning, we cannot train and evaluate a model using the same data.
If we use the same dataset for both training and testing, the model may simply memorize
the data instead of learning real patterns. To properly evaluate a machine learning model,
the dataset is divided into three parts: Training Set, Validation Set, and Test Set.

This division allows us to measure how well the model performs on unseen data and
ensures that the model generalizes well to new situations.
Training Set
The training set is the portion of the dataset used to train the machine learning model.
During this stage, the algorithm analyzes the input data and learns the relationship
between the input features and the output labels.

Usually, about 70–80% of the entire dataset is used as training data.

Purpose of the Training Set:


• To help the model learn patterns from the data
• To build the mathematical relationship between inputs and outputs
• To adjust internal parameters of the algorithm

Example:

Study Hours | Result


1 | Fail
2 | Fail
5 | Pass
6 | Pass

From this data, the model learns that students who study more hours
are more likely to pass the exam.

This stage is similar to a student studying from textbooks before an exam.

Validation Set
The validation set is used to evaluate the model during training and to tune the model.
It helps in selecting the best algorithm and adjusting important parameters
called hyperparameters.

Usually 10–15% of the dataset is used for validation.

Purpose of the Validation Set:


• To compare different models
• To tune model parameters
• To prevent overfitting
• To select the best-performing model

Example:

Suppose we train three different models to predict exam results.

Model A – Accuracy: 80%


Model B – Accuracy: 85%
Model C – Accuracy: 78%

Since Model B has the highest validation accuracy, it is selected as the best model.

The validation set works like a practice test before the final exam.
Test Set
The test set is used to evaluate the final performance of the trained model.
This dataset is completely new and has never been used during training or validation.

Usually 10–15% of the dataset is reserved for testing.

Purpose of the Test Set:


• To measure the final accuracy of the model
• To evaluate performance on unseen data
• To estimate how well the model will work in real-world applications

Example:

Study Hours | Actual Result | Predicted Result


4 | Pass | Pass
2 | Fail | Fail

If the predicted results match the actual results, the model is considered accurate.

Accuracy Formula:

Accuracy = (Number of Correct Predictions) / (Total Number of Predictions)

For example, if a model correctly predicts 90 out of 100 test cases,


its accuracy is 90%.

The test set acts like the final exam for the machine learning model.

Why Data Splitting is Important


Dividing the dataset into training, validation, and test sets is important
because it prevents the model from simply memorizing the training data.

Without proper data splitting, the model may perform well during training
but fail when it encounters new real-world data.

This problem is known as overfitting.

Proper dataset splitting ensures that the model learns meaningful patterns
and can make accurate predictions on unseen data.

Typical Dataset Split


A common way to divide the dataset is:

Training Set: 70–80%


Validation Set: 10–15%
Test Set: 10–15%
Example:

If we have 1000 data samples:


Training data = 700 samples
Validation data = 150 samples
Test data = 150 samples

This distribution helps balance learning, tuning, and evaluation.

Real-Life Analogy
Machine learning training can be compared to preparing for an exam.

Training Set – Studying textbooks and learning concepts


Validation Set – Practicing with sample tests
Test Set – Writing the final exam

This process ensures that the student (or model) truly understands the
concepts and can perform well when faced with new questions.

Summary
Training, validation, and test datasets are essential components of
supervised learning.

Training Set – Used to teach the model.


Validation Set – Used to improve and tune the model.
Test Set – Used to evaluate final model performance.

By dividing the dataset in this way, machine learning models can learn
patterns effectively and perform well on new unseen data.

Learning Curves
Learning Curves in Machine Learning
Introduction
Learning curves are graphical representations used in machine learning to understand how well a model
learns from training data. They show the relationship between the amount of training data used and the
performance of the machine learning model. By analyzing learning curves, researchers and students can
identify whether a model is learning properly or facing problems such as underfitting or overfitting.

Definition of Learning Curve


A learning curve is a graph that shows how the performance of a machine learning model changes as the
number of training examples increases. It helps us understand whether adding more training data
improves the model or not.

Components of a Learning Curve Graph


A typical learning curve contains two axes and two lines (curves).

X-axis: Number of Training Examples

This axis represents the amount of data used to train the model. Initially the model is trained with a small
number of examples, and gradually more data is added.

Y-axis: Error or Accuracy

This axis represents how well the model performs. Sometimes the graph shows error rate, and sometimes
it shows accuracy. Lower error means better performance.

Two curves are usually plotted in a learning curve:

1. Training Error Curve

The training error shows how well the model performs on the training data itself. When the model is
trained on a small amount of data, the training error is usually low because the model can easily
memorize the data.

2. Validation Error Curve

The validation error shows how well the model performs on new unseen data (validation dataset). This
helps to measure the generalization ability of the model.

Why Learning Curves are Important


Learning curves help us understand the behavior of machine learning models. They are useful for
diagnosing common problems during model training and help researchers improve model performance.

They help to:

- Understand if the model is learning properly

- Detect underfitting (high bias)

- Detect overfitting (high variance)

- Decide whether more training data is needed

Underfitting (High Bias)


Underfitting occurs when the model is too simple to capture the patterns in the data. In this case, the
model performs poorly on both training data and validation data.

Characteristics in Learning Curve:

- Training error is high


- Validation error is also high

- Both curves are close to each other

Example:

Suppose we try to predict student marks using only one simple rule when many factors like study time,
attendance, and practice affect marks. The model becomes too simple and cannot learn the real pattern.

Overfitting (High Variance)


Overfitting occurs when the model learns the training data too well, including noise or random details. As
a result, it performs very well on training data but poorly on new unseen data.

Characteristics in Learning Curve:

- Training error is very low

- Validation error is high

- Large gap between the two curves

Example:

If a model memorizes exact answers from training questions instead of learning the concept, it will fail
when new questions appear.

Ideal Learning Curve


In an ideal situation, as the number of training examples increases, both training error and validation error
decrease and become close to each other. This means the model is learning the correct pattern and
generalizing well.

Simple Real-Life Example


Consider teaching a child mathematics. At first, the child learns with only a few examples and may make
mistakes. As more practice problems are given, the child's understanding improves and the number of
mistakes decreases. This improvement over time is similar to a learning curve in machine learning.

Conclusion
Learning curves are an important diagnostic tool in machine learning. They help researchers understand
whether a model is underfitting, overfitting, or learning properly. By analyzing learning curves, we can
decide how to improve the model, such as adding more data, choosing a better algorithm, or adjusting
model complexity.

.
Learning Model Generalization
Learning model generalization refers to the ability of a machine learning model to perform
accurately on new, unseen data after being trained on a dataset. Instead of memorizing the
training data, the model should learn the general patterns and relationships within the data.
A model with good generalization:
• Learns the true patterns in the training data.
• Makes accurate predictions on new data.
• Avoids memorizing noise or specific examples.
1. Meaning of Generalization
Generalization means the model can apply its learned knowledge to different but similar
situations.
For example, if a model is trained to recognize handwritten digits, it should correctly identify
digits written by new people whose handwriting it has never seen before.
If the model only works well on the training examples but fails on new examples, then it does
not generalize well.
2. Why Generalization is Important
In real-world applications, the data used during training is only a small sample of all possible
data. Therefore, the model must be able to predict correctly for future data.
Examples:
• Spam detection: classify new emails as spam or not spam.
• Medical diagnosis: predict diseases for new patients.
• Weather prediction: forecast future weather conditions.
Without generalization, a model is not useful in practical applications.

3. Underfitting and Overfitting


Two major problems affect generalization:
Underfitting
Underfitting occurs when the model is too simple to capture the underlying pattern in the data.
Characteristics:
• High training error
• High testing error
• Model fails to learn the data properly
Example:
Using a linear model for data that actually follows a complex nonlinear pattern.

Overfitting
Overfitting occurs when the model learns the training data too well, including noise and small
fluctuations.
Characteristics:
• Very low training error
• High testing error
• Poor performance on new data
Example:
A very deep decision tree that memorizes all training samples.

4. Bias and Variance


Generalization is closely related to bias and variance.
Bias
• Error caused by overly simple assumptions.
• Leads to underfitting.
Example: Using linear regression for nonlinear data.
Variance
• Error caused by the model being too sensitive to training data.
• Leads to overfitting.
Example: A complex model that memorizes training samples.
5. Bias–Variance Tradeoff
The goal of machine learning is to balance bias and variance.
• High bias → model too simple → underfitting
• High variance → model too complex → overfitting
A good model should have:
• Low bias
• Low variance
This balance leads to better generalization and accurate predictions.

6. Methods to Improve Generalization


Some techniques used to improve model generalization include:
• Cross-validation – testing the model on different data splits.
• Regularization – adding penalties to reduce model complexity.
• More training data – helps the model learn real patterns.
• Feature selection – removing irrelevant features.
• Ensemble methods – combining multiple models (e.g., Random Forest).
Summary
Learning model generalization ensures that a machine learning model learns meaningful
patterns from training data and performs well on unseen data. Achieving good
generalization requires avoiding underfitting and overfitting by maintaining a balance
between bias and variance.

Support Vector Machines (SVM)


Support Vector Machine (SVM) is a supervised machine learning algorithm mainly used for
classification problems, though it can also be used for regression.
The main objective of SVM is to find the optimal boundary (called a hyperplane) that
separates data points belonging to different classes. The algorithm tries to create a boundary that
maximizes the distance between the classes, which improves prediction accuracy.

1. Basic Idea of SVM


In classification problems, the dataset contains points belonging to different categories. SVM
tries to draw a line or boundary that separates these categories.
• In 2-dimensional data, the boundary is a line.
• In 3-dimensional data, the boundary is a plane.
• In higher dimensions, it is called a hyperplane.
The best hyperplane is the one that separates the classes with the maximum margin
(distance) from the nearest data points.
2. Example
Consider the following dataset:
Height Weight Category
150 50 Student
155 52 Student
180 80 Adult
175 78 Adult

Here:
• Students have lower height and weight
• Adults have higher height and weight
SVM will plot these data points on a graph and draw a line that separates Students and Adults.
The algorithm chooses the line that maximizes the gap between the two groups.
3. Hyperplane
A hyperplane is the decision boundary used by SVM to separate different classes.
Example:
If we represent height and weight on a graph:
• One side of the line → Students
• Other side → Adults
Mathematically, a hyperplane can be represented as:
w⋅x+b=0w \cdot x + b = 0w⋅x+b=0

Where:
• w = weight vector
• x = input feature vector
• b = bias term
This equation defines the separating boundary.
4. Support Vectors
Support vectors are the data points that lie closest to the decision boundary (hyperplane).
These points are very important because:
• They define the position of the hyperplane
• If support vectors change, the hyperplane also changes
• Other distant points do not significantly affect the boundary
In simple words:
Support vectors are the critical training samples that determine the classifier.
5. Margin
The margin is the distance between the hyperplane and the nearest data points from each
class.
SVM tries to maximize this margin.
Why maximize margin?
• Larger margin → better generalization
• Less chance of misclassification
So SVM is also called a maximum margin classifier.
6. Types of SVM
1. Linear SVM
Used when data can be separated by a straight line.
Example:
Two groups of points clearly separated.
2. Non-Linear SVM
Used when data cannot be separated by a straight line.
Example:
Data points are mixed in circular patterns.
To solve this, SVM uses something called the Kernel Trick, which transforms data into a higher
dimension where separation becomes easier.
Common kernels:
• Linear Kernel
• Polynomial Kernel
• Radial Basis Function (RBF)
• Sigmoid Kernel
7. Advantages of SVM
• Works well with high-dimensional data
• Effective for small datasets
• Good generalization ability
• Robust to overfitting when margin is maximized
8. Disadvantages of SVM
• Training time can be slow for very large datasets
• Choosing the right kernel can be difficult
• Less effective when data contains a lot of noise
9. Applications of SVM
Support Vector Machines are widely used in many real-world applications:
1. Spam Email Detection
Classifies emails as spam or not spam.
2. Face Recognition
Identifies and verifies human faces in images.
3. Text Classification
Used in tasks such as sentiment analysis and document categorization.
4. Image Classification
Classifies images into categories such as animals, objects, or scenes.
5. Bioinformatics
Used for gene classification and protein analysis.

Random Forest
Random Forest is a supervised machine learning algorithm used for classification and
regression problems. It belongs to the category of ensemble learning methods.
Ensemble learning means combining multiple models to improve the overall performance of
the system.
Instead of using a single decision tree, Random Forest builds many decision trees and
combines their predictions to produce a more accurate and stable result.
1. Basic Idea of Random Forest
A decision tree may sometimes make incorrect predictions because it learns from only one view
of the data. Random Forest solves this problem by:
• Creating multiple decision trees
• Training each tree on different subsets of the data
• Combining the results of all trees
The final prediction is made using majority voting (for classification) or average prediction
(for regression).
This approach reduces overfitting and improves prediction accuracy.
2. How Random Forest Works
Random Forest follows several steps during training and prediction.
Step 1: Random Sampling of Data (Bootstrap Sampling)
From the original dataset, random subsets of data are selected.
Each subset may contain different samples from the original dataset.
This process is called bootstrapping.
Step 2: Build Multiple Decision Trees
For each subset of data:
• A decision tree is constructed.
• Each tree learns patterns independently.
Additionally, during tree construction, the algorithm randomly selects a subset of features at
each split instead of considering all features.
This increases diversity among the trees.
Step 3: Each Tree Makes a Prediction
When new input data is given:
• Every decision tree in the forest makes its own prediction.
Since each tree was trained on slightly different data, their predictions may differ.

Step 4: Majority Voting


The final prediction is determined by combining predictions from all trees.
For classification problems:
• The class with the most votes is chosen.
For regression problems:
• The average value of predictions is taken.
3. Example
Consider the dataset:
Age BMI Glucose Diabetes
45 28 160 Yes
30 22 90 No
50 31 180 Yes

A Random Forest model creates several decision trees.


Predictions from trees:
• Tree 1 → Yes
• Tree 2 → Yes
• Tree 3 → No
• Tree 4 → Yes
• Tree 5 → Yes
Now we count the votes:
• Yes → 4 votes
• No → 1 vote
Final Prediction → Yes
So the model predicts that the person has diabetes.
4. Why Random Forest Works Well
Random Forest improves prediction because:
• Multiple trees reduce the impact of errors from a single tree.
• Random feature selection creates diversity among trees.
• Combining predictions improves generalization.
It is less likely to overfit compared to a single decision tree.
5. Advantages of Random Forest
• High accuracy compared to many algorithms.
• Reduces overfitting because it averages multiple trees.
• Works well with large datasets.
• Can handle missing values and noisy data.
• Can be used for both classification and regression.
6. Disadvantages of Random Forest
• Training time is longer than a single decision tree.
• Model can be complex and harder to interpret.
• Requires more computational resources.
7. Applications of Random Forest
Random Forest is widely used in many fields.
Healthcare
• Disease prediction
• Medical diagnosis
Finance
• Credit risk prediction
• Fraud detection
E-commerce
• Product recommendation
Image and Text Processing
• Image classification
• Sentiment analysis

Applications of Supervised Learning


Supervised learning is widely used in many real-world applications.

Healthcare:
Disease diagnosis and medical prediction.

Finance:
Fraud detection and credit scoring.

E-commerce:
Product recommendation systems.

Image Processing:
Face recognition and object detection.

Natural Language Processing:


Spam detection and sentiment analysis.

EXAMPLES

1. Supervised Learning – Example


Supervised learning uses labeled data (input + correct output).
Example: Email Spam Detection
Email Words Label
“Win money now” Spam
“Meeting at 5 PM” Not Spam
“Free lottery ticket” Spam

Input → Email text


Output → Spam / Not Spam
The model learns from labeled emails and predicts whether a new email is spam.

2. Learning Curves – Example


Suppose we train a house price prediction model.
Training data sizes and error:
Training Data Size Training Error Validation Error
100 25% 30%
500 15% 18%
1000 10% 12%

As training data increases, both errors decrease.


This improvement is shown using a learning curve graph.

3. Training, Validation, and Test – Example


Suppose we have 1000 images for face recognition.
Data split:
• Training set → 700 images
• Validation set → 150 images
• Test set → 150 images
Training → Model learns patterns
Validation → Tune model parameters
Test → Final evaluation of model

4. Learning Model Generalization – Example


Example: Handwritten Digit Recognition
Training data contains digits written by some people.
If the model can correctly recognize digits written by new people, it shows good generalization.
Bad generalization example:
• Model memorizes training digits
• Fails to recognize new handwriting styles

5. Support Vector Machine (SVM) – Example


Classifying Students and Adults based on height and weight.
Height Weight Category
150 50 Student
155 52 Student
180 80 Adult
175 78 Adult

SVM finds the best separating line (hyperplane) between the two groups.
Support vectors are the points closest to that line.

6. Random Forest – Example


Predicting diabetes based on medical data.
Age BMI Glucose Diabetes
45 28 160 Yes
30 22 90 No
50 31 180 Yes

Multiple trees predict:


• Tree1 → Yes
• Tree2 → Yes
• Tree3 → No
• Tree4 → Yes
• Tree5 → Yes
Majority vote → Yes
Final prediction → Diabetes = Yes

You might also like