Algorithms: K Nearest Neighbors
Algorithms: K Nearest Neighbors
Algorithms: K Nearest Neighbors
Neighbors
Dr K Meena 03/05/2022 1
Simple Analogy..
• Tell me about your friends(who your neighbors
are) and I will tell you who you are.
Dr K Meena 03/05/2022 2
Instance-based Learning
Dr K Meena 03/05/2022 3
KNN – Different names
• K-Nearest Neighbors
• Memory-Based Reasoning
• Example-Based Reasoning
• Instance-Based Learning
• Lazy Learning
Dr K Meena 03/05/2022 4
What is KNN?
• A powerful classification algorithm used in pattern
recognition.
Dr K Meena 03/05/2022 5
KNN: Classification Approach
• An object (a new instance) is classified by a
majority votes for its neighbor classes.
• The object is assigned to the most common class
amongst its K nearest neighbors.(measured by a
distant function )
Dr K Meena 03/05/2022 6
KNN: Classification Approach
Dr K Meena 03/05/2022 7
Distance Measure
Compute
Distance
Test
Record
Training
Records Choose k of the
“nearest” records
Dr K Meena 03/05/2022 8
Distance measure for
Continuous Variables
Dr K Meena 03/05/2022 9
Distance Between Neighbors
• Calculate the distance between new example
(E) and all examples in the training set.
D( X ,Y ) (x y )
ni i
2
i1
03/05/2022
11 1
Dr K Meena 0
K-Nearest Neighbor Algorithm
• All the instances correspond to points in an n-dimensional
feature space.
? YES
Dr K Meena 03/05/2022 12
How to choose K?
• If K is too small it is sensitive to noise points.
Dr K Meena 03/05/2022 13
Dr K Meena 03/05/2022 14
X X X
Dr K Meena 03/05/2022 15
Strengths of KNN
• Very simple and intuitive.
• Can be applied to the data from any distribution.
• Good classification if the number of samples is large enough.
Weaknesses of KNN
Dr K Meena 03/05/2022 16