0% found this document useful (0 votes)
6 views52 pages

AI Module 2 Notes

This document provides an introduction to machine learning and its subfields, including neural networks, machine learning, deep learning, natural language processing, and computer vision. It explains how these technologies work, their applications, and the key components of machine learning models, such as tasks, experience, and performance. Additionally, it discusses supervised and unsupervised learning algorithms, highlighting classification and regression techniques.

Uploaded by

namithaa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views52 pages

AI Module 2 Notes

This document provides an introduction to machine learning and its subfields, including neural networks, machine learning, deep learning, natural language processing, and computer vision. It explains how these technologies work, their applications, and the key components of machine learning models, such as tasks, experience, and performance. Additionally, it discusses supervised and unsupervised learning algorithms, highlighting classification and regression techniques.

Uploaded by

namithaa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE 2 INTRODUCTION TO MACHINE LEARNING

MODULE 2
CHAPTER:Artificial Intelligence Technologies

Techniques in AI
Artificial Intelligence (AI) means making computers or machines think and act like
humans.
How does it work?
It works by using a lot of data (information), fast computers, and smart programs
(algorithms). These programs learn from the data automatically — they look for
patterns and make decisions or predictions on their own.
When we build AI, we’re basically trying to copy human intelligence — like how we
see, speak, learn, and make choices — and make a machine do the same.
AI is a big area that includes many smaller parts (called sub-domains), All these areas
use data and algorithms to help the system learn and improve automatically.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
2

[Link] networks
Neural networks are a way for computers to learn and make decisions like the human
brain.
In our brain, we have neurons (nerve cells) that send signals to each other. Similarly,
in a neural network, there are artificial neurons (called nodes) that pass information to
one another.
Each node receives some input (data), does some calculations, and sends the result to
the next node. The computer repeats this process many times to learn patterns and
understand the data better.
A neural network usually has three main parts:
Input layer – where the data enters the network (for example, an image or a number).
Hidden layer(s) – where the actual learning happens. The computer adjusts values
called weights and biases and thresholds to improve accuracy each time it processes
the data.
Output layer – This layer gives the final result or prediction after processing.
(for example, identifying an image as a cat or dog).

[Link] Learning
Machine Learning is a part of computer science that helps computers learn from data
on their own — without humans giving step-by-step instructions.

Instead of telling the computer exactly what to do, we give it lots of examples (data).
The computer then studies that data, finds patterns, and learns how to make decisions
or predictions.
Machine Learning (ML) is a subfield of Artificial Intelligence (AI) that enables
computers to analyze data, identify patterns, and make decisions with minimal human
intervention.
Instead of being explicitly programmed, ML systems learn from experience by
processing large datasets and recognizing trends or relationships within the data.
ML algorithms continuously improve their performance by comparing predicted
results with actual outcomes and adjusting their models accordingly.
The learning process involves training the system on historical data until it achieves a
desired level of accuracy.
Once trained, the machine can make predictions or decisions on new, unseen data
with improved precision.
This automation enhances efficiency, reduces human effort, and supports data-driven
decision-making across various domains.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
3

3. Deep Learning (DL):


Deep Learning is a Machine Learning (ML) technique that enables machines to
process data through multiple layers to classify, infer, and predict outcomes with high
accuracy.

How Deep Learning Works:

1. Neural Networks with Layers: DL uses deep neural networks, which are neural
networks with multiple hidden layers. Each hidden layer processes the output from
the previous layer, allowing the system to learn complex patterns in large datasets.
Example: In image recognition, the first layer might detect edges, the next layer might
recognize shapes, and further layers might identify objects.

2. Forward Propagation: Data moves from the input layer, through hidden layers, to
the output layer. This step, called forward propagation, produces the model’s
prediction.
Example: In an image recognition task, forward propagation will take raw pixel
data, process it layer by layer, and finally output a classification like "cat" or "dog."

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
4

3. Backward Propagation: If the output is not accurate, the system calculates the error,
updates the weights assigned to each neuron, and sends the adjustments back through
the network. This process, called backward propagation, trains the model to improve
accuracy.
Example: If the model misclassifies an image, backward propagation helps the model
adjust its calculations to avoid the same mistake next time.

4. Training the Model: The model learns from both the forward and backward
propagation processes by adjusting weights, improving its accuracy with every
iteration.
Example: Over time, with enough data, the model gets better at recognizing
images or predicting outcomes.

Learning Capabilities: Deep learning can work with both labeled (supervised learning)
and unlabeled (unsupervised learning) data, making it highly versatile.
Applications: Image Recognition Speech Recognition, Natural Language Processing
(NLP)

4. Natural language processing


Natural language processing(NLP) is a science in which a machine is made to read,
understand, interpret and respond to a human language.
The main goal is to make machines capable of communicating naturally with humans.

Applications of NLP: Chatbots and Virtual Assistants, Machine Translation,


Sentiment Analysis
How NLP Works:
[Link] and Understanding:
[Link]:
[Link]:

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
5

5. Computer Vision (CV)


Computer Vision is a branch of Artificial Intelligence (AI) that enables machines to
analyze, interpret, and understand images or videos.
It studies each part of the image to learn patterns by breaking it down into several
parts to learn patterns.
This helps machine to classify and learn from a set of images so that it can make
better decisions based on previous observations.
Machine can process, analyse and understand images, they can capture images or
videos in real time and interpret their surroundings.

Applications of Computer Vision: Computer vision techniques are used today for
facial recognition that helps in surveillance and security systems, autonomous
vehicles, retail stores for tracking inventory and customers, in medicine for
diagnosing diseases, in financial Institutions to prevent fraud, and so on.

Additional technologies that enable and support AI


1. Graphical processing units that provide heavy computing power required for
iterative processing and training neural networks.

GPUs are designed to handle these large volumes of data more efficiently than
traditional CPUs, making them ideal for tasks like:

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
6

Parallel Processing: GPUs can process many operations at once, speeding up the
training of models.
Faster Training of Neural Networks: They help process the data through multiple
layers in deep learning, making models train faster.
Example: In deep learning, GPUs are used to train models for tasks like image
recognition or
speech recognition, where data is analyzed and patterns are learned across millions of
inputs.
2. Internet of Things : The Internet of Things (IoT) refers to a network of connected
devices that generate massive amounts of data. These devices can range from
smartphones and wearables to home appliances and industrial sensors. However, this
data often remains unprocessed or under-analyzed.

AI and advanced algorithms help in automating the analysis of IoT data, extracting
useful insights and making sense of the vast amounts of information. Here's how AI
plays a role:

Data Analysis at Scale: AI can analyze data from millions of connected devices
quickly and efficiently.
Predicting Rare Events: AI models can identify patterns and predict rare events (like
equipment failure in manufacturing) that could otherwise be missed.
Understanding Complex Systems: AI helps make sense of complex systems (e.g.,
smart cities, healthcare systems) by analyzing data from various sources.

Machine Learning (ML) involves teaching a computer program to improve its


performance at a specific task through experience (data).

Tom M Mitchell:
“A computer program is said to learn from experience E with respect to some
class of tasks T and performance measure P, if its performance at tasks in T, as
measured by P, improves with experience E.”

This definition leads to understanding Machine Learning Models


in three key components:
[Link] (T)

[Link] €
[Link] (P).

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
7

The Three Key Part


1. Task (T) — What the computer is trying to do
This is the problem you want the machine to solve.
For example:
Predicting tomorrow’s weather ô
Checking if an email is spam or not ô
Recognizing faces in photos
Technically, examples of ML based tasks are:
Classification → deciding a category (spam/not spam)
Regression → predicting a number (price, temperature)
Clustering → grouping similar things (grouping customers)
2. Experience (E) — is the knowledge gained from data provided to the model. Once
data is provided, the model runs iteratively to learn some inherent patterns.
Just like humans learn from past experiences, machines learn from data. The more
data
the model is exposed to, the better it can learn and improve.
Example:If you want the model to classify emails, you provide it with a set of labeled
emails(spam and non-spam). This is its experience (E).The main ways a machine
learns from experience are:
Supervised learning → learning from labeled examples (input + correct answer)
Unsupervised learning → finding patterns without labels
Reinforcement learning → learning by trial and error (reward/punishment)
[Link] (P) is a measure that indicates how well a particular ML algorithm
has performed the given task T using experience E.(How well it does the task)
This measures how good the machine is at the task.
It’s like checking your test score after learning something.
Performance is analysed based on well-defined metrics including accuracy, F1,
confusion matrix, precision, recall, sensitivity, etc.
Example of the ML Model:
Let’s say we want to build a model to predict house prices
Task (T): Predict the price of a house based on features like size, location, and
number of rooms.
Experience (E): We provide the model with historical data on house sales (e.g., size,
location, price).
Performance (P): The model’s performance is measured by how accurately it predicts
the price of new
houses.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
8

Types of Machine Learning Algorithms

Supervised Machine Learning Algorithms


As the name indicates, such algorithms have a supervisor as teacher.
Supervised Machine Learning is a type of learning where the machine learns from
labeled data. Think of it like a student learning from a teacher who provides both the
questions (inputs) and the correct answers (outputs).
How It Works
[Link] Function
Let’s say:
X = input data (features)
Y = correct output (label)
The algorithm tries to learn a mapping function
The relationship between input (X) and output (Y) can be expressed as a function, like
Y = f(X), where the machine learns to map inputs to outputs.
so that when you give it new input data (X), it can correctly predict the output Y.
Example: If the input is a fruit with color = red, shape = round, the model predicts that
it’s an apple.
Example: Fruit Classification
You give the computer pictures of fruits and tell it the names:

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
9

After training on many examples, it learns:


“If it’s round and red → Apple”
“If it’s oval-shaped and yellowish-orange → Mango”
Now, if you show a new picture (say, an apple it hasn’t seen before), the machine can
predict that it’s an apple.
NOTE:Supervised learning should be used when output of data in the training
set is known.
As you already know, supervised learning means the model learns from labelled data
— data that already has the correct answers (outputs).
Now, these algorithms are mainly divided into two types:

[Link] Algorithms
[Link] Algorithms
[Link] Algorithms
A classification algorithm classifies data into a particular [Link]
techniques predict discrete categories(Discrete (fixed categories) — not numbers that
vary continuously.).
The algorithm learns from examples and then classifies new data into one of the
known categories.
The output will be based on what the model has learned in training phase.

Example:
Predicting if a fruit is Apple or Mango
Predicting if an email is Spam or Not Spam
Predicting if a tumour is Cancerous or non-Cancerous

Classification algorithms are best used if data can be tagged,categorized, or separated into specific
groups or classes.

How Classification Works:


Training Phase:
During the training phase, the model is given labeled data, meaning each piece of data
already has a known category (label).

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
10

Example: If you have a dataset of fruits with labels like apple and banana, the
algorithm learns the features (color, size, shape) of each fruit and associates them with
the correct label(category)
Prediction:
After learning from the data, the model can predict the category (class) for new,
unseen data based on the patterns it has learned.
Example: When given a new fruit (e.g., color = red, shape = round), the model
predicts that it’s an apple.

[Link] Algorithms
A Regression Algorithm is used in machine learning to predict a real value (a
continuous number) based on the data it has learned from. Unlike classification
algorithms, which predict discrete categories, regression algorithms predict
continuous values.
For example, the cost of a product, the value of a stock, changes in temperature or
fluctuations in power demand.

How Regression Works:


[Link] Phase:
During training, the algorithm is provided with labeled data that includes input
variables (features) and their corresponding real values (output).
Example: You have data on house features (size, location, number of rooms) and their
prices. The model learns the relationship between these features and the price.
[Link]:
[Link] learning, the model can predict the real value (continuous output)for new,
unseen data.
Example: Given the size, location, and number of rooms of a new
house, the model can predict its price.
Simple Way to Remember
ô Classification = “category(label) prediction”
ô Regression = “Number prediction”
Example to Connect Both
Let’s say you’re studying fruits ôô:
Classification:
“Is the fruit an Apple or Mango?” → (Category)
Output = Apple / Mango
Regression:
“What is the weight of the fruit?” → (Numeric)
Output = 120 grams

Un-Supervised Machine Learning Algorithms


Unsupervised Learning is a type of machine learning where the model is trained using
data that is neither labeled nor classified.
This means the machine doesn’t have any predefined answers to learn from and must
discover patterns or relationships in the data on its own.
The machine only receives raw data — and it has to find patterns or groupings on its
own.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
11

In unsupervised learning, the machine learns by itself — without any teacher or


correct answers.
It observes the data, looks for similarities and differences, and groups similar things
together.

For example, if we give an image of mango and anorange, then initially, the machine
has no idea about how a mango looks and how the orange looks.
It just looks at the features:color,shape,size,texture.
Then it notices patterns like:
“These round and orange-colored fruits look similar.”
“These oval and yellow ones look similar.”
So it groups them into two clusters:
Group 1 → Round, orange fruits
Group 2 → Oval, yellow fruits
What an unsupervised machine learning cannot do is specifying labels to the clusters
The machine doesn’t know the names (it doesn’t know “this is mango” or “this is
orange”).
It just groups them based on similarity.

1. Clustering
Clustering is a type of unsupervised learning where the computer automatically
groups/clusters similar data points together — without being told what the groups are.
This technique helps to discover hidden patterns or inherent groupings(already
existing inside) in the data, which might not be obvious at first glance.x`
Clustering finds groups, but not names for those groups.
You, the human or data scientist, look at each cluster afterward and decide what it
represents.
Think of clustering as the process of organizing things that are alike into the same
group.
For example:
All red, round fruits (apples) go in one cluster
All yellow, oval fruits (mangoes) go in another cluster
Even though the machine doesn’t know the names “apple” or “mango,” it groups
them based on similar features like color and shape.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
12

How Clustering Works:


Grouping Data Based on Similarities:
The main idea of clustering is to group data points that are similar to each other.
Each group is called a cluster, and the goal is for data within the same cluster to be
similar while data in different clusters is distinct or different.
Example:
In a dataset of customer purchases, clustering might group customers who buy similar
products into the same cluster. One cluster might include people who buy tech
gadgets, while another might include people who buy home appliances.

[Link] analysis
Association analysis is an unsupervised learning technique used to find relationships
or connections between items in large datasets.
It discovers rules or patterns like:
“If one thing happens, another thing often happens too.”
In other words, it helps find which items tend to occur together.
How Association Analysis Works:
Finding Relationships:
Association analysis tries to find patterns that describe how different items or actions
are linked together. Example: If customers frequently buy bread, they might also buy
butter. The analysis finds that these two products are often purchased together.
Creating Association Rules:
The main goal is to create association rules in the form of: "If X happens, then Y
happens".
These rules help businesses understand which items tend to be bought together.
Example: "If a customer buys a laptop (X), then they are likely to buy a mouse (Y)."
This rule can be used to recommend related products to customers.
Simple Meaning
It’s like the machine is saying:
“People who buy this, often buy that too!”
So, it helps us understand how items are related in the data.
Example: Shopping Store
Many customers who buy bread also buy butter.
The machine finds this pattern automatically.

Applications of Association Analysis:


Retail and E-commerce:
Businesses use association analysis to discover which products are often bought
together.
Example: In a supermarket, if many customers buy diapers and baby wipes together,
the store might place these items next to each other to increase sales.
Market Basket Analysis:
Commonly used in retail, this technique finds associations between products
purchased together. Example: "If a customer buys shampoo, they are likely to buy
conditioner."
Recommender Systems:

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
13

Used in online platforms (like Amazon or Netflix) to suggest products or movies


based on what others with similar preferences have liked.
Example: "Customers who bought this book also bought these other books."

3. Dimensionality reduction
Dimensionality Reduction is a technique used to simplify a dataset by reducing the
number of features (variables) without losing important information. This is
especially useful when you have datasets with a large number of features, often in the
millions, making it difficult to process and analyze the data effectively.

When we collect data, sometimes each data item has many features (variables).
For example, if you have data about people:

If you have too many features (sometimes thousands or even millions), it becomes:
Hard for the computer to process
Slower to train a model
Difficult to visualize or understand

So, Dimensionality Reduction helps by reducing the number of features, while still
keeping the important information.
Why is Dimensionality Reduction Important?

Simplifies Data:
When datasets have too many features (variables), it can be overwhelming and
computationally expensive to work with them.
Dimensionality reduction reduces the number of features, making the dataset more
manageable.
Reduces Complexity:
Fewer features make models easier to train, faster to process, and often lead to better
performance. Reducing features also helps to avoid problems like overfitting, where a
model is too complex and doesn’t generalize well to new data.
Improves Visualization:
With too many features, it's hard to visualize the data. Dimensionality reduction
makes it easier to visualize the dataset in 2D or 3D.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
14

4. Outlier Detection
Outlier detection, also known as anomaly detection, is a technique used to find rare or
unusual events in a dataset that do not follow the normal pattern.
These events or observations are called outliers and can indicate important issues like
fraud, errors, or new trends.
An outlier (or anomaly) is something that doesn’t fit with the rest of the data — it’s
rare or unusual.
Suppose you have the following data of students’ exam marks:
75, 80, 82, 78, 79, 5, 81, 77
Here, the mark 5 is very different from the rest — that’s an outlier.
The machine can be trained to detect such unusual values automatically.
How Outlier Detection Works:
[Link] Anomalies:
Outlier detection identifies data points that differ significantly from the rest of the
data.
Example: In credit card transactions, a sudden large withdrawal made in an unusual
location could be
flagged as an anomaly (possible fraud).
[Link] (KNN):

K-Nearest Neighbors (KNN) is a common technique used to detect anomalies by


checking the distance
between data points. If a data point is far from its neighbors, it's considered an outlier.
Example: In a KNN-based anomaly detection, if a customer transaction is much
higher than the usual range,it may be flagged as suspicious.

Semi-Supervised Learning
Semi-supervised learning algorithms is a mix of supervised and unsupervised learning
[Link] they use both labelled and unlabelled data for training.
It uses:
A small amount of labelled data (data with correct answers given)
A large amount of unlabelled data (data with no answers)

Simple Meaning:
Imagine you are a teacher training a student:

You give a few solved examples (labelled data).

Then you give many unsolved examples (unlabelled data).

The student uses what they learned from the few solved ones to guess
answers for the rest.

That’s semi-supervised learning!

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
15

Semi-supervised learning techniques can be applied using any of the two approaches
given below.
Approach 1:
Start with a small labelled dataset (Supervised step) → train a basic model.
Then, use this model to predict labels for the large unlabelled dataset (Unsupervised
step).
Repeat the process multiple times — the model becomes more accurate each time.
Example:
You label 100 fruit photos manually → the model learns.
It then labels 1,000 unlabelled photos automatically → retrains on them → becomes
smarter.
Approach 2:
Start with Unsupervised Learning (Clustering) → group similar unlabelled data.
Then assign labels to each cluster and train the model using that information.
Example:
Group all similar fruits first (apples together, bananas together).
Then label each group (“This cluster = apples”).
Use this new labelled data to train the model.

Reinforcement Learning (Rl)


These techniques are different from the previously discussed techniques and are rarely
used.
In a reinforcement learning algorithm, an agent(machine) is trained over a period of
time so that it can interact with a specific environment.
It learns using a reward and punishment system
The agent receives rewards by performing correctly and
penalties for performing incorrectly. In this way, the agent
learns without any human intervention to maximize its reward and minimize its
penalty.

Agent: The decision-maker that performs


actions.
Environment: The world or system in which
the agent operates.
State: The situation or condition the agent is
currently in.
Action: The possible moves or decisions the
agent can make.
Reward: The feedback or result from the
environment based on the agent’s action.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
16

Reinforcement Learning needs lots of experiences (data) to learn, so it’s mainly used
in simulated environments (like games or robotics) where the agent can safely try
many times and learn the best possible way (path or behaviour) to reach its goal.
But in the real world, it’s not always possible or safe to try many times.
For example:
A self-driving car cannot keep crashing in real life just to learn driving!
So, it needs a simulated environment (a computer-generated world) to practice safely.
Reinforcement learning is different from supervised learning.
In supervised learning, the training data has labels, so the
model is trained with the correct answer but in case of RL, the reinforcement agent
decides what to do to perform the given task.
In the absence of a training dataset, it is bound to learn
from its own experience.

Example:
Imagine a robot inside a game:
It wants to reach a diamond (reward) ô
But there are fires (hurdles or punishments) ô in the path
The robot tries different paths:
If it moves closer to the diamond → ✅ it gets a reward,If it touches
fire → ❌ it gets a penalty
Each right step earns a reward and every wrong
step will subtract the reward of the robot. The total reward is
calculated when it reaches the final reward that is the diamond

After many tries, the robot learns which path gives maximum
reward (best result) and avoids penalties (wrong steps).

How Reinforcement Learning Works:


Agent-Environment Interaction:
An agent interacts with its environment by taking actions (decisions) and receiving
feedback in the form of rewards or penalties.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
17

Example: In a game scenario, the agent might be a robot and the environment is the
game world.
Rewards and Penalties:
Rewards are given for performing correct actions, while penalties are given for
incorrect actions. The agent’s goal is to maximize its total reward over time by
learning from experience.
Example: If a robot picks the right path in the game (avoiding fire), it earns a reward
(like a diamond). If it chooses the wrong path (touches fire), it loses some reward.
Trial and Error:
The agent starts with no knowledge and learns by trying different actions, observing
the results, and adjusting its strategy.
Over time, the agent learns which actions lead to the highest rewards and which
actions lead to penalties.

Types of Reinforcement
In Reinforcement Learning (RL), reinforcement refers to the process of giving
feedback to an agent based on its actions to encourage or discourage certain behaviors.
1. Positive Reinforcement-
Positive reinforcement occurs when an action taken by the agent produces a positive
outcome or reward, which increases the likelihood of that action being repeated in the
future. This type of reinforcement strengthens the desired behaviour and helps the
agent learn faster. It motivates the agent to continue performing actions that lead to
beneficial results.

Example : Consider a robot learning to navigate a maze. Each time it moves closer to
the exit, it receives a reward of +10 points. This reward acts as positive reinforcement,
encouraging the robot to take similar paths in the future. Over time, the robot learns
the optimal route that gives the maximum cumulative reward.

Positive reinforcement helps in maximizing performance and allows the learned


behaviour to be sustained for a long period because the agent associates the action
with a good outcome.

Meaning:
When you reward a good action or behaviour so that it happens more often in
the future.
✅ In simple words:
If you do something right and get a reward, you’ll want to do it again.
Example 1 – Human life:

A teacher praises a student for submitting homework on time.


The student feels good and continues doing homework regularly.
(Reward = Praise, Behaviour = Submitting homework)

2. Negative Reinforcement
Negative reinforcement occurs when a certain behaviour is strengthened by removing
or avoiding a negative condition. In this case, the agent learns to perform actions that

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
18

prevent punishment or reduce negative outcomes. It does not mean punishment —


instead, it teaches the agent how to avoid undesirable states.

Example: In the same maze problem, if the robot hits a wall, it receives a penalty of
−5 points. When it changes direction to avoid the wall and the penalty stops, this
avoidance behaviour is negatively reinforced. The robot learns that moving away
from walls helps it avoid losing points, so it continues to do so in future attempts.

Negative reinforcement helps the agent refine its actions by learning which paths or
strategies reduce negative feedback and lead to better overall performance.

Meaning:
When you remove or avoid something unpleasant to
encourage the behaviour.

✅ In simple words:
If you do something right and it helps you avoid a bad
situation, you’ll do it more often.

Applications of Reinforcement Learning

[Link] is used in large environments when a model of the environment is known,


but an analytic solution is not available.
Imagine an autonomous drone that delivers packages in a [Link] know the
environment - roads, wind conditions, and delivery points - but there’s no simple
formula to decide the best route every time because traffic, obstacles, and weather
keep [Link], the RL agent (drone) learns through trial and error: It gets rewards
for delivering safely and quickly. It gets penalties for delays or crashes. Over time, it
learns the best flying strategy even without an exact equation to solve the problem.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
19

[Link] can be used in robotics for industrial automation.


Robots in factories can use RL to learn how to perform tasks like assembling parts,
packing, or moving objects efficiently by continuously improving from feedback.

[Link] is used to make machines learn.


RL helps machines learn on their own from experience — just like humans learn by
trying, making mistakes, and improving over time.

[Link] is used in data processing applications


RL can help automate data-related tasks such as optimizing data routing, improving
data storage efficiency, or managing large-scale data systems by learning the best
strategies.
Imagine a data center that stores and transfers files between many servers. An RL
agent can : Learn how to route data through the fastest network path, Balance the load
between servers to avoid overload. Over time, the system automatically learns the best
strategy for handling and storing data efficiently without human control.

[Link] can be used to create training systems that provide custom instruction and
materials according to the requirement of students.
RL can design personalized learning platforms that adjust lessons or exercises based
on how each student performs giving easier or harder questions as needed.

Regression Analysis in Machine Learning


Regression Analysis is a machine learning technique (and a statistical tool) used to
predict continuous values — like price, age, temperature, sales, or salary — based on
one or more factors.
It helps us find relationships between:
A dependent variable (target) → the thing you want to predict, and
One or more independent variables (predictors) → the factors that affect it.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
20

A regression line is the best-fit line


that shows the relationship between
two variables —
the independent variable (X) and the
dependent variable (Y).

It is the line that predicts how Y


changes when X changes.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
21

How Regression Analysis Works?


[Link] Relationships:
Regression analysis aims to determine how changes in the independent variables
affect the dependent variable.
Example: A company may want to find out how advertising expenditure affects sales.
[Link] Continuous Values:
Unlike classification, which predicts discrete categories, regression predicts
continuous values like price, salary, or temperature.
Example: Predicting the price of a house based on its size, location, and age.

Steps in Regression Analysis:


[Link] Collection:
Collect data for the independent and dependent variables.
[Link] Creation:
Create a mathematical equation that defines the dependent variable as a function of
the independent variables.
Example:
If Y is the price of a house, and X1 and X2 are size and location, then the model
might be:
Y = f(X1, X2).
[Link] the Best Fit:
A regression line or curve is plotted on a graph to find the best fit through the data
points.
[Link] the Model:

After building the regression model, evaluate its accuracy by measuring how well it
predicts new data.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
22

Case Study 2 - Monthly Rental Cost:


Examples of Regression Analysis:
Problem: A company’s rental cost is
Case Study 1 - Auto Fare Calculation:
based on a fixed cost plus a per-
Problem: The cost of an auto fare depends
employee charge.
on a fixed charge plus a per-kilometer rate.
Linear Equation:
Linear Equation:
y=10000x+20000
y=11x+30 (y=mx+c)
Where x is the number of employees and
y → total auto fare (the output or
y is the total rental cost.
dependent variable)
For 20 employees, the monthly rental is:
y=10000×20+20000=220000 Rs.
x → distance traveled in kilometers (the
input or independent variable)

30 → the fixed charge (the amount you pay


even if you travel 0 km)

11 → the rate per kilometer


y=11×10+30=140 Rs.

Summary:
Regression Analysis helps predict continuous values (e.g., price, salary) based on the
relationship between dependent and independent variables.
Linear Regression is the most common technique, but multiple regression, non-linear
regression, and other techniques are used for more complex tasks.
Applications include cost predictions, profit calculations, and sales forecasting.

Model Evaluation Metrics


The best regression model is the one with the lowest prediction error. The most
widely used metrics for comparing regression models are discussed here:
[Link] Root Mean Squared error
2.R2 (R-Squared) - Coefficient of Determination
[Link] R2 (Adjusted R-Squared)

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
23

RMSE Root Mean Squared error

Example - how rmse is calculated

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
24

R2 (R-Squared) - Coefficient of Determination

It means-how much the changes in Y happen because of changes in X.


When X changes (goes up or down), Y also changes — and R² tells us how strongly
those two are connected.

For example:

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
25

If you study more hours (X), your marks (Y) increase.

R² tells how much of the change in marks is because of the change in study hours.

If R² = 1 (or 100%), the model predicts perfectly — all data points lie exactly on the
regression line.

If R² = 0, the model is useless — it doesn’t explain any relationship between X and Y.

R² is between 0 and 1:It means the model partially explains the relationship between
the independent variable (X) and the dependent variable (Y).

Adjusted R2 (Adjusted R-Squared)


Adjusted R² is a modified version of R² that adjusts for the number of predictors (independent
variables) in your model.

Why?
Because when you add more x variables, R² always increases — even if those variables are not useful.

Example
Suppose you are predicting house price (y) using:
x₁ = area
x₂ = number of rooms
x₃ = color of the door (not important)
If you add x₃ (color of door),
R² will increase slightly — even though that variable is useless.
Adjusted R² will decrease — because it recognizes that x₃doesn’t really help.
In short:
R² tells you how well your model fits.
Adjusted R² tells you how well your model fits — after removing the effect of
unnecessary variables.

How do we identify useless variables?


There are a few ways ô

✅ (a) Adjusted R²

If you add a new variable:


Adjusted R² increases → the variable is useful.
Adjusted R² decreases → the variable is useless.
That’s exactly why Adjusted R² was created — it penalizes useless
variables.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
26

Evaluation Methods :
1. Train-Test Split
Dividing your dataset into two parts —
one part to train the model, and
another part to test how well it performs.
Why we do it:
We want to check whether our model can predict new, unseen data — not just
memorize the data it was trained on.
So we keep some data hidden (test data) and check the model’s performance later.

Typical split:
Most commonly:
Training set: 70–80% of the data
Testing set: 20–30% of the data

we split our data into:

Training set → used to train the model

Testing set → used to check accuracy

But if we split only once, the result may depend on how we split (some splits might be
“lucky” or “unlucky”).
That’s where K-Fold Cross-Validation helps.

2.k-fold cross-validation
It is a technique to evaluate a model more reliably by dividing the data into K equal
parts (folds).
Process:
Step 1: Randomly split the data set into k-subsets (or k-fold). For example, to generate
5 subsets, value of k = 5.
Step 2: Reserve one subset and call it as test data. Use rest of subsets to the train the
model.
Step 3: Test the performance of the model using the test data set and record the
prediction error.
Step 4: Repeat the above steps until each of the k subsets have been used as the test
set.
Step 5: Calculate the average of the k recorded errors. This is also known as cross-
validation error. Finally, the best model is the one that has the lowest cross-validation
error, RMSE.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
27

Types of Regression
Regression analysis involves different types of models, each suited for specific types
of data and relationships between variables.

1. Linear Regression
2. Logistic regression
3. Ridge regression
4. Lasso (Least Absolute Shrinkage Selector Operator) regression
5. Polynomial regression
6. Stepwise regression
7. ElasticNet regression

1. Linear Regression

Linear regression is a statistical technique used to find the relationship between a


dependent variable (Y) and one or more independent variables (X). It fits a straight
line through the data points that best represents this relationship.
It predicts a dependent variable (Y) based on one or more independent variables (X)
by fitting a straight line through the data.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
28

Example::Predicting house prices


based on size, location, and number of
rooms.
Equation: Y=bX+C
Y is the dependent variable (e.g., house
price).
X is the independent variable (e.g., size
of the house).
b is the slope/coefficient (relationship
between X and Y).
C is the intercept.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
29

NOTE
The coefficient 10 means:
For every 1 additional hour of study, marks increase by 10.
The intercept (20) means:
The intercept (20) means:
Even if the student studies 0 hours, the predicted marks = 20.
(It’s the base or starting value when X = 0.)

The coefficient (10) shows the strength of the relationship — it’s the number that connects X
(hours) to Y (marks).
It tells how much Y changes when X changes.

Types linear of Regression:


[Link] Linear Regression:
The simplest form of regression where the relationship between the dependent and
independent variables is assumed to be linear (a straight line).
Example: Predicting house price based on area.
[Link] Linear Regression:
Uses two or more independent variables.
Example: Predicting salary based on experience, education, and age.

2. Logistic Regression

Logistic Regression is a classification algorithm,Used in Predicting a binary


outcome(yes/no, true/false) or the probability of an event occurring.
Equation: The output is transformed using the logit function (log-odds).
Example: Predicting whether an email is spam or not spam based on certain features.
For example:
“What is the probability that a student passes based on study hours?”
“Will a customer buy a product (Yes/No) based on income and age?”

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
30

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
31

3. Ridge Regression

Ridge Regression is a extension of Linear Regression that helps when:

your model has too many predictors, or

your predictors (X variables) are highly correlated (multicollinearity), or

your model is overfitting (performing well on training data but poorly on test data).

It’s a regularized regression — meaning it adds a penalty to control the model’s


complexity.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
32

[Link] Regression(Least Absolute Shrinkage and Selection Operator)

Lasso Regression is another type of regularized linear regression — just like Ridge —
but with a small twist.
Both Ridge and Lasso add a penalty to prevent overfitting.
However, Lasso can actually make some coefficients exactly zero, which means it can
remove irrelevant variables from the model.
So the name itself means:

Shrinkage → reduces and removes the size of coefficients

Selection → can set some coefficients to zero

[Link] Regression:

When the relationship between the


independent and dependent variables is non-linear.
Example: Predicting temperature changes with
respect to time, where the relationship is curved
(e.g., seasonal variations).

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
33

NOTE:

6. Stepwise Regression:

Builds the regression model by adding or removing variables step by step based on
performance.
Stepwise Regression is a method of selecting the most important independent
variables (features) for building the best regression model.
In simple words:
Instead of using all the predictors (X₁, X₂, X₃, …), stepwise regression automatically
chooses which ones to keep and which ones to remove — step by step.
Approaches:
Forward Selection: Starts with no variables and adds them one at a time.
Backward Elimination: Starts with all variables and removes them step by step.
Bidirectional Elimination: Combines both approaches.
Example: Selecting the most significant predictors in a dataset for predicting sales
growth

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
34

Classification Techniques
Classification is a type of machine learning task where the goal is to predict which
category or class an observation belongs to.
The model is trained using labeled data, where each input is already tagged with the
correct class or category. Based on this, the model learns the patterns and uses them to
predict the class of new, unseen data
The classification model takes input data (features) and assigns it to one of the
predetermined categories.

Example: In an email classification task, the model predicts whether an email is spam
or not spam based on the features (like subject, sender, content, etc.).

K-Nearest Neighbors (KNN)


The K-Nearest Neighbors (KNN) algorithm is a supervised learning method that
classifies data points based on the similarity of nearby data points. It is used for
classification and regression tasks.
K-NN works on the idea that similar data points exist close to each other.
When a new, unknown data point appears, the algorithm looks at the K most similar
(nearest) data points from the training set and assigns the most common label among
them to the new point.
It is a non-parametric algorithm — it makes no assumption about data distribution.
Classification is based purely on distance or similarity between data points.
“K” represents the number of neighbours to consider when making the decision.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
35

K-Nearest Neighbors (KNN)


Steps in KNN Algorithm:
Choose 'k' Value:
'k' is the number of nearest neighbors to consider. For example, k=3 means looking at
the three closest neighbors to classify a new data point.
Calculate Distance:
Measure the distance between the new data point and all other data points in the
training set.
Find Nearest Neighbors:
Identify the 'k' closest data points to the new data point.
Classify or Predict:
For classification: Assign the class that is the most common among the 'k' neighbors.
For regression: Calculate the average of the 'k' neighbors' values.

Steps in KNN Algorithm:EXPLANATION WITH EXAMPLE

Step 1 — Choose the number of neighbors (K)


K = number of nearby points the algorithm will check.
Example: K = 3 → look at the 3 closest data points.
Step 2 — Calculate Distance
For the new, unknown data point, calculate its distance from all known data points in
the training dataset.
Most commonly used distance:Euclidean Distance

Step 3 — Sort and Select Nearest Neighbors


Sort all distances in increasing order.
Select the K smallest distances — these are your K nearest neighbors.
Step 4 — Majority Voting (for Classification)
Look at the classes of the selected neighbors.
The most frequent class becomes the prediction for the new data point.
Example:
K = 5 → [Cat, Dog, Dog, Dog, Cat]
✅ Predicted = Dog (because “Dog” appears most often).

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
36

KNN EXAMPLE PROBLEM

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
37

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
38

Clustering Techniques
Clustering is a set of unsupervised learning techniques used in Machine Learning to
divide or partition a dataset into a number of smaller groups, called clusters.

The purpose of clustering is to ensure that data objects within the same cluster are
similar to each other, while objects belonging to different clusters are dissimilar.

In simple terms, clustering helps to identify hidden patterns or natural groupings


within data that do not have predefined labels or categories.

Clustering plays a significant role in understanding the meaningfulness and usefulness


of data.

Meaningful clusters help us learn something new or insightful about data. Clustering
method enhances domain knowledge by revealing previously unknown relationships
among data objects.
Example (Healthcare):
Doctors can group patients based on how their bodies respond to a particular medicine.
Cluster 1: Patients who got cured fast.
Cluster 2: Patients who needed longer treatment.
Cluster 3: Patients who didn’t respond well.

This helps doctors to understand that one medicine doesn’t work the same way for
everyone.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
39

On the other hand, useful clusters are those that serve as an intermediate step in a data
analysis process.
Example (Business):
A company can group customers based on buying habits:
Cluster 1: Regular buyers
Cluster 2: Occasional buyers
Cluster 3: One-time buyers
Then, they can send special offers to regular buyers and discount coupons to
occasional ones. This helps them target ads effectively.

Overview of Clustering Techniques:


There are various clustering techniques available, each with its own advantages and
limitations. The choice of clustering algorithm depends on the nature of the dataset,
such as the number of data points, the shape and density of clusters, the presence of
outliers, and the number of features (variables).
There are three main types of clustering algorithms, each suited to different types of
data:
1. Partitional clustering
2. Hierarchical clustering
3. Density-based clustering

1. Partitional clustering

Partition-based clustering is a technique used to divide data objects into


non-overlapping groups, known as clusters.
In this approach, each data object belongs to exactly one cluster, and no object can
appear in more than one cluster.
In other words, the clusters formed are distinct and separate from one another.
Every cluster must contain at least one data object, meaning that no empty clusters
are allowed in the final grouping.

So, if you have 10 data points and you choose =3


k=3, the algorithm will divide the data into 3 groups (clusters) — for example:
Cluster 1 → {Points 1, 3, 5}
Cluster 2 → {Points 2, 4, 7, 8}
Cluster 3 → {Points 6, 9, 10}
Role of “k”
In partitional clustering, you (the user) must decide how many clusters (k) you want
beforehand.
Example: In k-means, you set k = 3, so the algorithm will form 3 clusters.
Two examples of partitional clustering algorithms are k-means and k-medoids.
k-means-Divides the data into k clusters based on distance from cluster centers
(centroids).
Each cluster is represented by the mean (average) of the points in that cluster.

k-medoids-Similar to K-Means, but instead of using the average, it chooses an actual


data point (medoid) as the center of each cluster.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
40

[Link] Clustering

It’s a type of clustering that builds a tree-like structure of clusters — called a


dendrogram.

Instead of just dividing data into a fixed number of groups directly (like k-means),
it shows how clusters are formed step by step — either by merging or splitting.
Two Types of Hierarchical Clustering
[Link] (Bottom-Up approach)
Start with each data point as its own cluster.
Then merge the two clusters that are most similar (closest).
Keep merging until everything becomes one single big cluster.
Example:
Let’s say you have 4 points: A, B, C, D
Start → each is alone: {A}, {B}, {C}, {D}
Merge the two closest → {A,B}, {C}, {D}
Next closest → {A,B,C}, {D}
Finally → {A,B,C,D}

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
41

This builds up a tree of merges (bottom to top).


2 Divisive (Top-Down approach)
Start with all points in one big cluster.
Then split the points that are least similar (most different).
Keep splitting until each point stands alone.
Example:
Start → {A,B,C,D}
Split → {A,B}, {C,D}
Split again → {A}, {B}, {C}, {D}
This builds the tree from top to bottom.

Advantages Limitations
1 Shows relationships at a finer level 1 Computationally expensive
Hierarchical clustering doesn’t just form clusters —
it shows how each data point is related to others. For large datasets, it takes a lot of time and memory.
The dendrogram (tree) helps you see which points Because it has to calculate distances between all pairs of
are closer or farther. points many times.
So, you can understand data structure in detail, not So it’s slower than simpler methods like K-Means.
just the final groups. Example:
Example: If you have 10,000 data points → the algorithm has to
If you cluster students based on marks — compare millions of pairs!
you can see which students are most similar, which [Link] to noise and outliers
small groups form first, and how they later merge
into bigger groups. A few unusual(outliers) or incorrect data points(noise) can
2 Easy to interpret disturb the whole tree structure.

The tree (dendrogram) makes results visual and Once an incorrect merge/split happens early, it can’t be
simple to understand. corrected later.
So accuracy decreases if the data is messy or contains
You can literally see how clusters were formed or noise.
split.

3. Density-Based Clustering

Note :
In data science or clustering, density means how closely the data points are packed
together in a region. If many data points lie close to each other, that area is said to have
high density.
If very few points are present and they’re far apart, that area has low density.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
42

In contrast to other clustering categories, density-based approach does not require the
user to specify the number of clusters. It uses a distance-based parameter that acts as a
threshold. The threshold value helps the algorithm to determine how close points must
be to be considered a cluster member.
Popular examples of density based Algorithms are :
Density-Based Spatial Clustering of Applications with Noise (or DBSCAN)
Ordering Points To Identify the Clustering Structure (or OPTICS)
Imagine stars in the sky ô
Where stars are close together, you can call that a cluster
(galaxy).
Where stars are spread far apart, that’s empty space (low
density).

Density-based clustering works in the same way —


it groups points that are crowded together and separates
those that are far away.

K-Means Clustering

K-Means is a clustering algorithm that divides a dataset into K groups (clusters) based
on the similarity between data points.
It is called unsupervised learning because it works without labels — it finds patterns
or groups on its own.
Data points that are similar are grouped into the same cluster.
Data points that are different are placed in different clusters.

Steps of the K-Means Algorithm:


step 1: Choose the number of clusters (K):
You must decide how many clusters you want before running the algorithm (e.g., K =
3).
Step 2: Initialize centroids:
The algorithm randomly selects K points from the dataset as the starting cluster
centers (centroids).

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
43

Step 3: Assign points to nearest centroid:


Each data point is assigned to the cluster whose centroid is closest to it (based on
distance — usually Euclidean distance).
Step 4:Update centroids:
Once all points are assigned, the algorithm calculates a new centroid for each cluster
— this is the average position of all points in that cluster.
Step 5 : Repeat:
Steps 3 and 4 are repeated until the centroids no longer change much, meaning the
algorithm has converged.

K-Means Algorithm explanation with example


EXAMPLE 1

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
44

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
45

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
46

EXAMPLE 2

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
47

Pros and Cons of K-Means Algorithm


Pros
1. K-means clustering algorithm is very simple and fast.
2. The algorithm can efficiently deal with very large data sets
Cons
1. Number of clusters must be pre-specified.
2. The algorithm is sensitive to outliers
3. Changing the order of data will give different results

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
48

Real-World Applications of Clustering


1. Customer Segmentation

2. Image Segmentation

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
49

3. Recommendation Systems

4. Document Clustering

Evaluation Metrics for Clustering


When we perform clustering, the goal is not just to form groups,but to form good and
meaningful clusters —
that means:
Points in the same cluster should be close to each other (similar).
Different clusters should be far apart (distinct).
But in real life, data has many features (columns), so we cannot visualize and decide
by just looking at it.
Hence, we use evaluation metrics — mathematical ways to measure the quality of
the clusters.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
50

1. Inertia
Inertia indicates how far the points within a cluster are.
Inertia checks how close the points inside a cluster are (compactness),
It measures how tightly grouped the points are within each cluster.
It tells us if points inside a cluster are close to the cluster center (centroid) or not.

When we calculate Inertia, we deal with many clusters, not just one.

Step-by-step

Inertia indicates how far the points within a cluster are.

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
51

First sum of distances of all the points within a cluster from the centroid of that cluster
is calculated.
Once inertia is calculated for all the clusters, the final value of inertia is obtained by
adding all these values.
The final value that gives the distance within the clusters is known as intra-cluster
distance. So, inertia gives us the sum of intra-cluster distances.
The inertial value ensures that the first property of clusters is satisfied.

Low Inertia → Points are close to the center → Clusters are compact → ✅ Good
High Inertia → Points are far away → Clusters are spread out → ❌ Poor
Goal: We want inertia to be as small as possible.
Simple Example:
Suppose we cluster students by their marks:
In Cluster 1, all marks are between 90–95 (close together) → small distance → low
inertia ✅
In Cluster 2, marks are between 60–90 (spread out) → large distance → high inertia

So, the first cluster is better.

2. Dunn Index

So it combines two properties of a good clustering:


Points within a cluster should be close (compact).
Different clusters should be far apart (well separated).

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC


MODULE 2 INTRODUCTION TO MACHINE LEARNING
52

High Dunn Index → Clusters are well separated and compact → ✅ Good
Low Dunn Index → Clusters overlap or are spread out → ❌ Poor
Goal: We want Dunn Index to be as high as possible.

The Dunn Index ensures that the second property of clusters is satisfied — clusters
should be far apart from each other.

******************************* END************************************

NAMITHA AV/ASST. PROF/SJEC SEM-1/CSDS/ICBS/SJEC

You might also like