0% found this document useful (0 votes)
3 views27 pages

Unit 6 - Machine Learning Algorithm XI AI Notes

The document provides an overview of Machine Learning (ML) algorithms, focusing on supervised learning (regression and classification) and unsupervised learning (clustering). It explains key concepts such as correlation, causation, and the importance of Pearson's correlation coefficient in regression analysis. Additionally, it discusses the applications of ML in various fields and highlights the challenges faced, including overfitting and bias in training data.

Uploaded by

AyshaPlays
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)
3 views27 pages

Unit 6 - Machine Learning Algorithm XI AI Notes

The document provides an overview of Machine Learning (ML) algorithms, focusing on supervised learning (regression and classification) and unsupervised learning (clustering). It explains key concepts such as correlation, causation, and the importance of Pearson's correlation coefficient in regression analysis. Additionally, it discusses the applications of ML in various fields and highlights the challenges faced, including overfitting and bias in training data.

Uploaded by

AyshaPlays
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

ARTIFICIAL INTELLIGENCE GRADE XI- TERM 2

Notes

UNIT 6: Machine Learning Algorithms


Learning Objectives:
1. Understand Machine Learning and the various machine learning algorithms
2. Understand regression as a type of supervised learning.
3. Understand classification as a type of supervised learning.
4. Understand clustering as a type of unsupervised learning.
5. List of algorithms for regression, classification and clustering
6. Differentiate between regression problem, classification problem and clustering problem.
Key Concepts:
1. Machine Learning in a nutshell
2. Types of Machine Learning
3. Supervised Learning
Regression – Understanding Correlation, Regression, Finding the line, Linear
Regression algorithm
Classification – How it works, Types, k – Nearest Neighbour algorithm
4. Unsupervised Learning
Clustering – How it works, Types, k -means Clustering algorithm

MACHINE LEARNING IN A NUTSHELL


Machine Learning (ML) is a part of artificial intelligence (AI) that focuses on teaching
computers to learn from data and make decisions without being explicitly programmed.
Unlike traditional programming where developers provide precise instructions, ML
algorithms learn from patterns and relationships in data. This allows them to generalize and
make decisions on new, unseen data.
● ML algorithms learn from various types of data, including images, text, sensor
readings, and historical records. Instead of hardcoding rules, ML models identify patterns
and relationships within the data to make predictions or decisions.
● Some common ML algorithms include decision trees, neural networks, and support vector
machines. Trained models serve as representations of the learned data, such as recognizing
handwritten digits using a neural network.
● The applications of ML are vast and diverse. It powers recommendation systems like those
used by Netflix, speech recognition, medical diagnosis, and autonomous vehicles. ML is
also behind chatbots, personalized ads, and fraud detection systems.
● However, ML also presents challenges. Overfitting, where models become too

1
specialized on training data, can lead to poor performance on new data. Bias in training
data can result in biased predictions, and some models are difficult to interpret, acting
as black boxes. Despite these challenges, ML transforms data into knowledge, enabling
computers to learn, adapt, and make decisions autonomously.
● Artificial intelligence (AI) and machine learning (ML) have significantly impacted
various aspects of our lives. From transportation and finance to healthcare and

entertainment, AI algorithms are pervasive. They power self-driving cars, fraud detection
systems, personalized shopping experiences, and virtual assistants like Siri and Alexa. As
technology continues to evolve, the influence of AI and ML is only expected to grow,
shaping the future of our society and culture.

Activity 1: Autodraw - Experience the power of machine learning with Autodraw! Autodraw
combines machine learning with the creativity of talented artists, allowing you to draw
things quickly and effortlessly. Visit the following link to play the game:

TYPES OF MACHINE LEARNING

Teachers can ask the following questions:

1. Think back to the different AI applications we discussed earlier (e.g.,


recommendation systems, facial recognition). Can you imagine how they might learn from
data? Do they all learn in the same way? This prompts them to make connections between
real-world applications and the concept of machine learning. It also primes them for the idea
of different learning approaches.
2. Imagine you have a dataset with pictures of different animals. How could a
computer program learn to group these pictures into categories like cats, dogs, and birds?
(supervised vs unsupervised learning) This is a specific scenario that introduces the concept
of labelled data (supervised learning) vs. unlabelled data (unsupervised learning).
3. Can you think of a situation where learning by trial and error might be a good
approach for a machine learning program? This taps into their understanding of
learning through experience and lays the groundwork for reinforcement learning.

We introduced you to the fascinating world of artificial intelligence (AI) and its various
learning mechanisms. We discussed three main types of machine learning: supervised
learning, unsupervised learning, and reinforcement learning. These terms represent the
algorithms that drive AI systems, serving as the building blocks for programming
intelligent behavior and decision-making processes. Now, let us delve deeper into how these
algorithms shape the landscape of AI applications.
● Supervised learning involves the model learning from labeled data, where the input data is
accompanied by the correct output. The algorithm learns to map input data to output labels
based on example input-output pairs provided during training. The goal is to learn a mapping
function so that the model can make predictions on unseen data. Examples include linear
regression, logistic regression, decision trees, support vector machines, and neural networks.
● Unsupervised learning, on the other hand, deals with unlabelled data, where the
2
algorithm tries to find hidden patterns or structure without explicit guidance. The goal of this
is to explore and discover inherent structures or relationships within the

data, such as clusters or associations. Examples include k-means clustering,


hierarchical clustering, principal component analysis, and autoencoders.
● Finally, reinforcement learning involves an agent learning to make decisions by
interacting with an environment to maximize cumulative rewards. Through trial and error, the
agent learns a policy or strategy to take actions that lead to the highest cumulative reward
over time. These rewards serve as feedback, guiding the agent towards favorable actions,
while penalties discourage undesirable behavior. This iterative learning approach is
particularly effective in scenarios requiring a sequence of decisions, such as game-playing or
robot control, where the agent adapts behavior based on past experiences and outcomes.
Examples include Q-learning, deep Q- networks, policy gradients, and actor-critic methods.

A. SUPERVISED LEARNING
Supervised learning stands out as one of the foundational types of Machine Learning. It is
a powerful approach that allows machines to learn from labeled data, making
predictions or decisions based on that learning. Within supervised learning, two primary
types of algorithms emerge:
1. Regression – works with continuous data

Continuous data examples would include the following:

• An individual’s height or weight


• The temperature daily in your city
• How much time is required to finish an activity or project

2. Classification – works with discrete data

Discrete data examples include the following:

• The size of your department’s workforce.


3
• How many new clients you brought on board in the previous quarter?
• How many items are currently kept in stock?

1. REGRESSION
Understanding Correlation: The Foundation of Regression Analysis
In data analysis, correlation is a fundamental concept that helps us grasp the
relationship between variables, laying the groundwork for predictive modeling and insightful
analysis. Correlation is a measure of the strength of a linear relationship between two
quantitative variables (e.g. price, sales). If the change in one variable appears to be
accompanied by a change in the other variable the two variables are said to be correlated
and this inter dependence is called correlation.

4
Types of Correlation:

1. Positive Correlation: In a positive


correlation, both variables move in the
same direction. As one variable
increases, the other also tends to
increase, and vice versa.

2. Negative Correlation: Conversely, in a


negative correlation, variables move in
opposite directions. An increase in one
variable is associated with a decrease in
the other, and vice versa.

3. Zero Correlation: When there is no


apparent relationship between two
variables, they are said to have zero
correlation. Changes in one variable do not
predict changes in the other

Correlation can have a value:


● 1 is a perfect positive correlation
● 0 is no correlation (the values don't seem linked at all)
● -1 is a perfect negative correlation

Causation
Causation indicates that one event is the result of the occurrence of the other event.
Example: Since there is hot weather, the person will use more sunscreen or eat more ice
cream.
Sometimes, these two events may be correlated also. Example: smoking causes an increase in the
risk of developing lung cancer or it can
correlate with another like-smoking is correlated with alcoholism, but it does not cause
alcoholism. Therefore, we can say causation is not always correlation.

PEARSON’S R
Pearson's correlation coefficient (often denoted as Pearson's r) is one of the crucial
factors to consider when assessing the appropriateness of regression analysis. Pearson's r
measures the strength and direction of the linear relationship between two continuous
variables. In the context of regression analysis, a high degree of correlation between the
independent and dependent variables sug

gests that there may be a meaningful relationship to explore using regression

5
techniques.
The requirements when considering the use of Pearson's correlation coefficient are:

6
1. Scale of measurement should be interval or ratio.
2. Variables should be approximately normally distributed.
3. The association should be linear.
4. There should be no outliers in the data.
Pearson’s r is calculated using the formula:

r can take a range of values from +1 to -1

● A value of 0 indicates that there is no association between the two variables.


● A value greater than 0 indicates a positive association; that is, as the value of one variable
increases, so does the value of the other variable.
● A value less than 0 indicates a negative association; that is, as the value of one variable
increases, the value of the other variable decreases.
Example 1
In the example below of 6 people with different ages and different weight, let us try
calculating the value of the Pearson r.

Solution:
For the Calculation of the Pearson Correlation Coefficient, we will first calculate the
following values:

Here the total number of people is 6 so, n=6

7
Now the calculation of the Pearson R is as follows:

r = (n (∑xy)- (∑x)(∑y))/(√ [n ∑x2-(∑x)2][n ∑y2– (∑y)2 )


r = (6 * (13937)- (202)(409)) / (√ [6 *7280 -(202)2] * [6 * 28365- (409)2 )
r = (6 * (13937)- (202) * (409))/(√ [6 *7280 -(202)2] * [6 * 28365- (409)2 )
r = (83622- 82618)/(√ [43680 -40804] * [170190- 167281 )
r = 1004/(√ [2876] * [2909 ) r = 1004 / (√ 8366284)
r = 1004 / 2892.452938
r = 0.35
The value of the Pearson correlation coefficient is 0.35

It is important to note that, regression analysis may not be suitable in certain situations:
1. No Correlation: If there is no correlation between the variables, meaning they change
independently of each other, regression analysis will not provide meaningful insights or
predictions.
2. Non-linear Relationships: While regression can model linear relationships well, it may not
capture more complex, non-linear relationships effectively. In such cases, alternative
techniques like polynomial regression or non-linear regression may be more appropriate.
3. Outliers: Outliers, or extreme data points, can disproportionately influence the
regression model and lead to inaccurate predictions. In the presence of outliers, it is
essential to assess their impact and consider alternative modeling approaches.
4. Violation of Assumptions: Regression analysis relies on certain assumptions, such as the
linearity of relationships and the absence of multicollinearity (high correlation between
predictor variables). If these assumptions are violated, the results of the regression
analysis may be unreliable.
REGRESSION
Regression is a statistical technique used to model the relationship between a
dependent variable and one or more independent variables. Its primary objective is to
understand and predict the value of the dependent variable based on the values of the
independent variables. In simpler terms, regression helps us understand how changes in one
or more variables are associated with changes in another variable.

8
Regression analysis is particularly useful when dealing with continuous data, where
variables can take on any value within a certain range. For example, variables such as height,
temperature, salary, and time are all continuous, meaning they can be measured along a
continuous scale. In regression, these continuous variables are used to predict or explain
the variability in another continuous variable, known as the dependent variable. By
analyzing the relationship between the independent and dependent variables, regression
allows us to make predictions and understand how changes in one variable may impact the
other. This makes regression a powerful tool for forecasting, prediction, and understanding
complex relationships in various fields such as economics, social sciences, and healthcare
When we make a distribution in which there is an involvement of more than one
variable, then such an analysis is called Regression Analysis. It generally focuses on
finding or rather predicting the value of the variable that is dependent on the other.
Let there be two variables x and y. If y depends on x, then the result comes in the form of a simple
regression. Furthermore, we name the variables x and y as:
y – Regression / Dependent / Explained Variable.
It is the variable we want to predict or understand.

x – Independent /Predictor / Explanator Variable


It is used to predict or explain changes in the dependent variable.
Therefore, if we use a simple linear regression model where y depends on x, then the
regression line of y on x is:
y = a + bx + e
In this equation,
● a represents the intercept of the regression line with the y-axis.
● b represents the slope of the regression line, indicating the rate of change in y for a unit
change in x.
● e represents the error or residual, which accounts for the difference between the observed
values of y and the values predicted by the regression equation.
FINDING THE LINE
Regression analysis relies on the concept of the regression line or curve, which
represents the best-fit relationship between the variables involved. This line or curve is
determined by minimizing the differences between the observed values of the dependent
variable and the values predicted by the regression model.
The least squares method is commonly employed to find this best-fit line or curve. This
method minimizes the squared differences between observed and predicted values,
ensuring that the regression line captures the overall trend or pattern in the data as
accurately as possible. By systematically estimating the parameters of the regression
model, such as the intercept and slope, the least squares method provides a precise
description of the relationship between the variables.
Through the least squares method, regression analysis yields estimate of the
regression coefficients that define the best-fit relationship between the variables. These

9
coefficients allow for making predictions about the dependent variable based on the values of the
independent variable(s) with greater accuracy and reliability. As a result, this is widely used in
regression analysis.
Properties of the Regression line:
● The line minimizes the sum of squared difference between the observed values
(actual y-value) and the predicted value (ŷ value)
● The line passes through the mean of independent and dependent features.
Example 1
In the example of 6 people with different ages and different weight, let us draw the line of best
fit in Excel.

Solution:
Step 1: Select the Age and Weight.
Step 2: Insert a scatter chart and make changes to the following: Trendline Name: Linear,
check Display Equation on Chart X axis minimum: 20

Step 3: Let us verify the values of slope and intercept using slope() and intercept() function in
excel.
Step 4: Click on any cell and type =slope (Now, select the values of Weight, and then type
comma. Now, select the values of Age and press enter.
Step 5: Click on any cell and type =intercept (Now, select the values of Weight, and then type
comma. Now, select the values of Age and press enter.

Some of the regression algorithms include Linear Regression, Logistic Regression,


Decision Tree Regression, Random Forest Regression. Let us learn about Linear Regression.

10
Linear Regression
Linear regression is one of the most basic types of regression in machine learning. The
linear regression model consists of a predictor variable and a dependent variable related
linearly to each other. In case the data involves more than one independent variable, then linear
regression is called multiple linear regression models.
Linear regression is further divided into two types:
a) Simple Linear Regression: The dependent variable's
value is predicted using a single independent variable in
simple linear regression.
b) Multiple Linear Regression: In multiple linear regression,
more than one independent variable is used to predict the
value of the dependent variable.
Applications of Linear Regression:
● Market Analysis: Linear regression helps understand how different factors like pricing,
sales quantity, advertising, and social media engagement relate to each other in the market.
● Sales Forecasting: It predicts future sales by analyzing past sales data along with factors like
marketing spending, seasonal trends, and consumer behavior.
● Predicting Salary Based on Experience: Linear regression estimates a person's salary
based on their years of experience, education, and job role, aiding in recruitment and
compensation planning.
● Sports Analysis: Linear regression analyzes player and team performance by
considering statistics, game conditions, and opponent strength, assisting coaches and team
management in decision-making.
● Medical Research: Linear regression examines relationships between factors like age,
weight, and health outcomes, helping researchers identify risk factors and evaluate
interventions.
Advantages of Linear regression
● Simple technique and easy to implement
● Efficient to train the machine on this model
Disadvantages of Linear regression
1. Sensitivity to outliers, which can significantly impact the analysis.
2. Limited to linear relationships between variables. [Link]
regression-in-machine-learning

11
For Advanced Learners – Python program for Linear regression
Import scipy and draw the line of Linear Regression:

This program:

● Imports numpy for numerical calculations and [Link] for plotting.


● Defines sample data for x and y. You can replace this with your own data.
● Calculates mean, standard deviation, covariance, and slope.
● Calculates y-intercept based on slope and mean.
● Predicts y values for given x using the linear equation.
● Plots the data points and the regression line.
● Prints the estimated slope and intercept values.

12
The expected output of the above program would be

REFERENCES
Video links:
● [Link]
● [Link]
● [Link]
● [Link]
2. CLASSIFICATION

Classification is a fundamental concept in artificial intelligence and machine learning


that involves categorizing data into predefined classes or categories. The main objective of
classification is to assign labels to data instances based on their features or attributes. In
classification, the data is typically labeled with class labels or categories, and the goal is to
build a model that can accurately assign these labels to new, unseen data instances. This
process is supervised learning, where the model learns from labeled training data to make
predictions on unseen data.

For example, let us say, you live in a gated housing society and your society has
separate dustbins for different types of waste: paper waste, plastic waste, food waste and so on.
What you are basically doing over here is classifying the waste into different categories and
then labeling each category. In the picture given below, we are assigning the labels ‘paper’,
‘metal’, ‘plastic’, and so on to different types of waste.

13
Look at the two graphs below and suggest which graph represents the classification
problem.

Graph 1 Graph 2

How Classification Works


In classification tasks within machine learning, the process revolves around
categorizing data into distinct groups or classes based on their features. Here is how it
typically works:
● Classes or Categories: Data is divided into different classes or categories, each
representing a specific outcome or group. For example, in a binary classification scenario,
there are two classes: positive and negative.
● Features or Attributes: Each data instance is described by its features or attributes, which
provide information about the instance. These features are crucial for the classification
model to differentiate between different classes. For instance, in email classification, features
might include words in the email text, sender information, and email subject.
● Training Data: The classification model is trained using a dataset known as training data.
This dataset consists of labelled examples, where each data instance is associated with
a class label. The model learns from this data to understand the relationship between the
features and the corresponding class labels.
● Classification Model: An algorithm or technique is used to build the classification model.
This model learns from the training data to predict the class labels of new, unseen data
instances. It aims to generalize from the patterns and relationships in the training data to
make accurate predictions.
● Prediction or Inference: Once trained, the classification model is used to predict the class
labels of new data instances. This process, known as prediction or inference, relies on the
learned patterns and relationships from the training data.

Types of classification
The four main types of classification are:
1) Binary Classification
2) Multi-Class Classification
3) Multi-Label Classification

14
4) Imbalanced Classification
Classification Binary Multi-Class Multi-Label Imbalanced
Type Classification Classification Classification Classification
Classification Classification tasks
Classification tasks where with unequally
Classification
tasks with more each example distributed class
Description tasks with two
than two class may belong to labels, typically with
class labels.
labels. multiple class a majority and
labels. minority class.
• Email spam
• Face
detection -
classification
spam or not
• Plant species • Photo
• Conversion
classification classification
prediction -
• Optical - objects • Fraud detection
buy or not
character present in the • Outlier detection
Example • Medical test
recognition photo • Medical diagnostic
- Cancer
• Image (bicycle, tests
detected or not
classification apple, person,
• Exam
into thousands etc.)
results -
of classes
pass/fail

K- Nearest Neighbour algorithm (KNN)


The K-Nearest Neighbors algorithm, commonly known as KNN or k-NN, is a versatile
non-parametric supervised learning technique used for both classification and regression
tasks. It operates based on the principle of proximity, making predictions or classifications by
considering the similarity between data points.

Why KNN Algorithm is Needed:


KNN is particularly useful when dealing with classification problems where the
decision boundaries are not clearly defined or when the dataset does not have a well-
defined structure. It provides a simple yet effective method for identifying the category or
class of a new data point based on its similarity to existing data points.

15
Steps involved in k-NN
● Select the number K of the neighbors
● Calculate the Euclidean distance of K number of neighbors
● Take the K nearest neighbors as per the calculated Euclidean distance.
● Among these k neighbors, count the number of the data points in each category.
● Assign the new data points to that category for which the number of the neighbor is
maximum.
● Our model is ready.
Applications of KNN:
● Image recognition and classification
● Recommendation systems
● Healthcare diagnostics
● Text mining and sentiment analysis
● Anomaly detection
Advantages of KNN:
● Easy to implement and understand.
● No explicit training phase; the model learns directly from the training data.
● Suitable for both classification and regression tasks.
● Robust to outliers and noisy data.
Limitations of KNN:
● Computationally expensive, especially for large datasets.
● Sensitivity to the choice of distance metric and the number of neighbors (K).
● Requires careful preprocessing and feature scaling.
● Not suitable for high-dimensional data due to the curse of dimensionality. For
advanced learners – Python Program for K Nearest Neighbor Algorithm # importing
libraries
import numpy as nm
import [Link] as mtp
import pandas as pd #importing datasets
data_set= pd.read_csv('user_data.csv')

#Extracting Independent and dependent Variable x= data_set.iloc[:, [2,3]].values


y= data_set.iloc[:, 4].values
# Splitting the dataset into training and test set.
from sklearn.model_selection
import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25, random_state=0)
#feature Scaling
from [Link] import StandardScaler st_x= StandardScaler()
x_train= st_x.fit_transform(x_train) x_test= st_x.transform(x_test)

Reference: [Link] learning


Note to the teacher: Can introduce Teachable Machine to develop any image classifier
problem.

Website: [Link]

REFERENCES

Video Session:

Classification: [Link]
KNN Algorithm: [Link]
B. UNSUPERVISED LEARNING

3. CLUSTERING
Clustering, or cluster analysis, is a machine learning technique used to group unlabeled
dataset into clusters or groups based on similarity. Clustering aims to organize data points
into groups where points within the same group are more similar to each other than to those in
other groups. It involves finding patterns or structures in the data without the need for
predefined class labels. It does it by finding some similar patterns in the unlabelled dataset
such as shape, size, color, behavior, etc., and divides them as per the presence and absence of
those similar patterns. It is an unsupervised learning method, hence no supervision is
provided to the algorithm, and it deals with the unlabeled dataset.
The clustering technique is commonly used for statistical data analysis.
example: Let us consider the clustering technique using a real-world example. Imagine you are
visiting a shopping center where items are grouped together based on their similarities. For
instance, in the fruits section, you will find apples, bananas, and grapes neatly arranged
together. This organization makes it convenient for shoppers to locate specific items they
are looking for.

Based on colour

Based on size
In a similar way, clustering algorithms group similar data points together based on common
characteristics or features. This approach helps in organizing and making sense of large
datasets in various tasks, such as market segmentation, image recognition, and customer
segmentation.
How Clustering Works:
To cluster data effectively, follow these key steps:
1) Prepare the Data: Select the right features for clustering and make sure the data is ready
by scaling or transforming it as needed.
2) Create Similarity Metrics: Define how similar data points are by comparing their
features. This similarity measure is crucial for clustering.
3) Run the Clustering Algorithm: Apply a clustering algorithm to group the data. Choose one
that works well with your dataset size and characteristics.
4) Interpret the Results: Analyze the clusters to understand what they represent. Since
clustering is unsupervised, interpretation is essential for assessing the quality of the
clusters.
Types of Clustering Methods
Some of the common clustering methods used in Machine learning are:
1) Partitioning Clustering
2) Density-Based Clustering
3) Distribution Model-Based Clustering
4) Hierarchical Clustering

1. Partitioning Clustering
It is a type of clustering that divides the data into
non- hierarchical groups. It is also known as the
centroid- based method. The most common
example of partitioning clustering is the K-
Means Clustering algorithm. In this type, the
dataset is divided into a set of k groups, where k is
used to define the number of pre-defined groups.
The cluster center is created in such a way that
the distance between the data points of one
cluster is minimum as compared to
another cluster centroid.
2. Density-Based Clustering
The density-based clustering method connects the
highly-dense areas into clusters, and the
arbitrarily shaped distributions are formed as long as
the dense region can be connected. This algorithm
does it by identifying different clusters in the
dataset and connects the areas of high
densities into clusters. The dense areas in data
space are divided from each other by sparser
areas. These algorithms can face difficulty in
clustering the data points if the dataset
has varying densities and high dimensions.
3. Distribution Model-Based Clustering
In the distribution model-based clustering
method, the data is divided based on the
probability of how a dataset belongs to a
particular distribution. The grouping is done
by assuming some distributions commonly
Gaussian Distribution.
The example of this type is the Expectation-
Maximization Clustering algorithm that uses
Gaussian Mixture Models (GMM).
4. Hierarchical Clustering
Hierarchical clustering can be used as an
alternative for the partitioned clustering as
there is no requirement of pre-specifying the
number of clusters to be created. In this
technique, the dataset is divided into clusters
to create a tree-like structure, which is also called
a dendrogram. The observations or any number of
clusters can be selected by cutting the tree at the
correct level. The most common example of
this method is the Agglomerative
Hierarchical algorithm.

K- Means clustering
K-Means Clustering is an unsupervised learning algorithm that is used to solve the
clustering problems in machine learning or data science. The k-means algorithm is one of
the most popular clustering algorithms. It classifies the dataset by dividing the samples into
different clusters of equal variances. The number of clusters must be specified in this
algorithm.
Steps involved K-Means Clustering:
The working of the K-Means algorithm is explained in the below steps:
● Select the number K to decide the number of clusters.
● Select random K points or centroids. (It can be other from the input dataset).
● Assign each data point to their closest centroid, which will form the predefined K
clusters.
● Calculate the variance and place a new centroid of each cluster.
● Repeat the third steps, which means reassign each datapoint to the new closest
centroid of each cluster.
● If any reassignment occurs, then go to step-4 else go to FINISH.
● The model is ready.
Activity: Visual AI: This tool allows you to visualize K-means clustering in real-time. Upload
your own data or use provided examples, adjust parameters, and see how clusters change
visually using the link Visualise k-means

Applications of K-Means Clustering:


● Market Segmentation: group customers based on similar purchasing behaviours or
demographics for tailored marketing strategies.
● Image Segmentation: partition images into regions of similar colours to aid in tasks like
object detection and compression.
● Document Clustering: categorize documents based on content similarity, aiding in
organization and information retrieval.
● Anomaly Detection: identify outliers by clustering normal data points and detecting
deviations.
● Customer Segmentation: segment customers for targeted marketing and
personalized experiences.

Advantages of K-Means Clustering:


● Easy to implement, making it suitable for users of all levels.
● Handles large datasets with low computational resources.
● Works well with numerous features and data points.
● Are easy to understand, aiding in decision-making.
● Applicable across various domains and data types.

Limitations of K-Means Clustering:


● Results can vary based on initial centroid placement.
● Assumes clusters are spherical, which is not always true.
● Number of clusters must be known beforehand.
● Outliers can distort clusters due to their influence on centroids.
● May converge to suboptimal solutions instead of the global optimum.
import numpy as np
import [Link] as plt
from [Link] import make_blobs
from [Link] import KMeans

# Generate synthetic data


X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)

# Apply K-means clustering


kmeans = KMeans(n_clusters=4)
[Link](X)
y_kmeans = [Link](X)

# Plot the data points and centroids

For advanced Learners – Python Program for K Means Clustering


[Link](X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')
centers = kmeans.cluster_centers_
[Link](centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75)
[Link]('K-means Clustering')
[Link]('Feature 1')
[Link]('Feature 2')
[Link]()

Output

This program does the following:


1. Generates synthetic data using make_blobs from [Link].
2. Applies K-means clustering with n_clusters=4.
3. Plots the data points colored by their cluster assignments and shows the centroids as red
circles.
You can adjust the parameters like the number of clusters, standard deviation, and number of
samples in make_blobs to observe different clustering scenarios.
REFERENCES
Video Session
Videos: Clustering: [Link]
K-means clustering: [Link]
EXERCISES
A. Multiple Choice Questions
1. Which of the following are the types of correlation?
a. Positive correlation
b. Negative Correlation
c. No correlation
d. All of the above
2. Which of the following techniques is an analysis of the relationship betweentwo variables to
provide the prediction mechanism?
a. Standard error
b. Correlation
c. Regression
d. None of the above

3. Which of the given plots is suitable for testing the linear relationship betweena dependent
and independent variable?
a. Bar chart
b. Scatter plot
c. Histograms
d. All of the above
4. Which of the following scatter plots represents a positive correlation?
a. points scattered randomly with no apparent trend
b. points forming a diagonal line and bottom left to top right
c. points forming a diagonal line from top left to bottom right
d. points clustered around a central point
5. Which regression technique is used when there is only one independentvariable?
a. logistic regression
b. multiple linear regression
c. simple linear regression
d. polynomial regression
6. What is one advantage of linear regression analysis?
a. it is robust to outliers
b. it can capture nonlinear relationships between variables
c. it is simple and easy to interpret
d. it is suitable for classification tasks
7. What is supervised learning in Artificial Intelligence?
a. training a computer algorithm on input data that is not labelled.
b. training a computer algorithm on input data that has been labelled fora specific output.
c. training a computer algorithm without any input data
d. training a computer algorithm to perform unsupervised tasks.
8. Which type of classification involves categorizing data into two distinct classes?
a. multi-class classification
b. binary classification
c. unsupervised classification
d. regression classification
9. What is logistic regression commonly used for in binary classification?
a. categorizing observations into multiple classes
b. predicting continuous values for input data
c. categorizing observations into two distinct classes
d. identifying unstructured data patterns
10. What is the primary goal of classification in AI?
a. categorizing data into random groups
b. locating and classifying things or concepts into predefined groups
c. predicting continuous values for input data
d. identifying unstructured data patterns
11. Which algorithm is commonly used for binary classification?
a. Decision trees
b. Support Vector Machine
c. Logistic Regression
d. k-Nearest Neighbors
12. The K-Nearest Neighbors (KNN) algorithm assigns a class to new data point byconsidering:
a. Distance from the data point to a predefined decision boundary
b. Majority vote of its K nearest neighbors in the training data
c. Similarity of the data point to a cluster centroid
d. probability of each class given the data point’s features.
13. What does a classification model in AI ultimately want to achieve?
a. to identify patterns and associations in data
b. to predict continuous numerical values
c. to categorize input data into predefined classes or labels
d. to optimize decision-making processes
14. What are some challenges in applying classification models to real-worldproblems?
a. Data bias and fairness
b. Interpretability and explainability
c. overfitting and underfitting
d. All of the [Link] is clustering?
a. Grouping labeled dataset
b. Dividing data into different clusters
c. Finding linear association between variables
d. Predicting future behaviors of a dependent variable
16. Which type of learning does clustering belong to?
a. Supervised learning
b. Unsupervised learning
c. Semi-supervised learning
d. Reinforcement learning
17. Which method is used to group highly dense areas into clusters?
a. Partitioning clustering
b. Density-based clustering
c. Distribution model-based clustering
d. Hierarchical clustering
18. Which algorithm is an example of partitioning clustering?
a. Mean-shift algorithm
b. DBSCAN algorithm
c. K-Means algorithm
d. Fuzzy clustering algorithm
19. Which clustering method allows data objects to belong to more than one group orcluster?
a. Partitioning clustering
b. Density-based clustering
c. Distribution model-based clustering
d. Fuzzy clustering
20. Which clustering algorithm is sensitive to outliers?
a. K-Means algorithm
b. Mean-shift algorithm
c. DBSCAN algorithm
d. Hierarchical clustering

B. Fill in the blanks


1. In type of ML, the models are not trained in labeled data sets.
2. The measures the linear relationship between the
independent and dependent variables.
3. predicts continuous numerical values, while Logistic
regression predicts discrete categories.
4. are data points on the scatterplot that do not follow the pattern ofthe dataset.
5. algorithm operates based on the principle of proximity, makingpredictions by
considering the similarity between data points.
6. Clustering is a machine learning technique used to group dataset.
7. Partitioning clustering divides the data into non-hierarchical groups, also known as
method.
8. Density-based clustering connects highly dense areas into clusters, separated byareas of
.
9. The primary requirement for the number of clusters in K-Means algorithm is
beforehand.
10. Clustering is widely used in applications such as market segmentation and
.

C. True or False:
1. Clustering is a supervised learning technique.
2. Hierarchical Clustering requires pre-specifying the number of clusters.
3. Fuzzy clustering is a hard clustering method.
4. Classification is an unsupervised learning technique.
5. In k-NN algorithm, k is the number of nearest data points.
6. K-Means algorithm requires specifying the number of clusters.
ANSWERS
A. Multiple Choice Questions
1. a. All of the above
2. c. Regression
3. b. Scatter plot
4. b. points forming a diagonal line and bottom left to top right
5. c. simple linear regression
6. c. it is simple and easy to interpret
7. b. training a computer algorithm on input data that has been labelled for a
specific
output.
8. b. binary classification
9. c. categorizing observations into two distinct classes
10. b. locating and classifying things or concepts into predefined groups
11. c. Logistic Regression
12. b. Majority vote of its K nearest neighbors in the training data
13. c. to categorize input data into predefined classes or label
14. d. All of the above
15. b. Dividing data into different clusters
16. b. Unsupervised learning
17. c. Distribution model-based clustering
18. c. K-Means algorithm
19. d. Fuzzy clustering algorithm
20. a. K-Means algorithm
B. Fill in the blanks
1. Unsupervised Learning
2. Correlation coefficient
3. Linear Regression
4. Outlier
5. K-nearest neighbors (KNN) algorithm
6. unlabelled dataset
7. centroid-based method
8. low point density
9. Specified
10. Data Analysis
C. True or False:
1. False 2. False 3. False 4. False 5. True 6. True

You might also like