SlideShare ist ein Scribd-Unternehmen logo
1 von 11
K-Nearest Neighbor –As Supervised Classifier
Compiled by : Dr. Kumud Kundu
K Nearest Neighbor – As a Supervised Classification
Approach
• KNN is a non-parametric supervised learning technique in
which the query instance is classified to a given category with
the help of training set.
• Non-parametric means not making any assumptions on the
underlying data distribution.
• Predictions are made for a new instance (x) by searching
through the entire training set for the K most similar cases
(neighbors) and summarizing the output variable for those K
cases.
• In simple words, it captures information of all training cases
and classifies new cases based on a similarity.
2
Two Phases of Supervised Classification
3
4
EUCLIDEAN DISTANCE: SIMILARITY METRIC
𝑦2
𝑦1
𝑥1 𝑥2
𝑃𝑜𝑖𝑛𝑡 𝐵 (𝑥2, 𝑦2)
𝑃𝑜𝑖𝑛𝑡 𝐴 (𝑥1, 𝑦1)
𝑬𝒖𝒄𝒍𝒊𝒅𝒆𝒂𝒏 𝑫𝒊𝒔𝒕𝒂𝒏𝒄𝒆 = 𝒙 𝟐 − 𝒙 𝟏
𝟐 + 𝒚 𝟐 − 𝒚 𝟏
𝟐
5
MANHATTAN DISTANCE or CITY BLOCK
DISTANCE: SIMILARITY METRIC
𝑦2
𝑦1
𝑥1 𝑥2
𝑃𝑜𝑖𝑛𝑡 𝐵 (𝑥2, 𝑦2)
𝑃𝑜𝑖𝑛𝑡 𝐴 (𝑥1, 𝑦1)
𝑴𝒂𝒏𝒉𝒂𝒕𝒕𝒂𝒏 𝑫𝒊𝒔𝒕𝒂𝒏𝒄𝒆 = | 𝒙 𝟐 − 𝒙 𝟏 | + | 𝒚 𝟐 − 𝒚 𝟏 |
6
1. Select a value for k (e.g.: 1, 2, 3, 10..)
2. Calculate the Euclidian distance between the point to be classified and every
other point in the training data-set
3. Pick the k closest data points (points with the k smallest distances)
4. Run a majority vote among selected data points, the dominating
classification is the winner! Point is classified based on the dominant class.
5. Repeat if required!
K NEAREST NEIGHBORS (KNN): ALGORITHM STEPS
7
Predict Class for Tuple ( 10,7)
Apply KNN Classification Algorithm
Solution with K=3
Feature 1 Feature 2 Class Euclidean Distance Rank
1 1A 10.81665383 8
2 3A 8.94427191 7
2 4A 8.544003745 6
5 3A 6.403124237 5
8 6B 2.236067977 4
8 8B 2.236067977 3
9 6B 1.414213562 2
11 7B 1 1
Predict for 10 7 Predicted Class = B
8
1. Calculate the Euclidian distance between the point to be classified and every other point in
the training data-set
2. Pick the k=3 closest data points (points with the k smallest distances)
3. Run a majority vote among selected data points, the dominating classification is the winner!
Point is classified based on the dominant class.
How to decide the number of neighbors in KNN?
What are its effects on the classification Accuracy?
The number of neighbors(K) in KNN is a hyperparameter that is
needed to be chosen at the time of model building.
• K controls the classification accuracy of the model.
• Generally, K is chosen as an odd number if the number of
classes is even.
• Otherwise value of K is dependent upon the nature of dataset
(Domain Dependent ) for which it is to be applied.
9
KNN Implementation in Python
• First, import the KNeighborsClassifier module and create KNN classifier object by passing argument
number of neighbors in KNeighborsClassifier() function.
• by usingthe sklearn.neighbors.NearestNeighbors module
classifier = KNeighborsClassifier(n_neighbors = 5,
metric = 'minkowski', p = 2)
# KNN model with 5 neighbours and Euclidian distance as similarity metric
• Then, fit your model on the train set using fit() and perform prediction on the test set using predict().
# Fitting K-NN to the Training set
• classifier.fit(X_train, y_train)
# Predicting the Test set results
• y_predy_pred = classifier.predict(X_test) 10
from sklearn.neighbors import NearestNeighbors
QUICK CHECK
*Which of the following statements is true for k-NN classifiers?
A) The classification accuracy is better with larger values of k
B) The decision boundary is smoother with smaller values of k
C) The decision boundary is linear
D) k-NN does not require an explicit training step
*k-NN algorithm does more computation on test time rather than train time.
A) TRUE
B) FALSE
*Which of the following statement is true about k-NN algorithm?
1. k-NN performs much better if all of the data have the same scale
2. k-NN works well with a small number of input variables (p), but struggles when the
number of inputs is very large
3. k-NN makes no assumptions about the functional form of the problem being solved
A) 1 and 2
B) 1 and 3
C) Only 1
D) All of the above
11

Weitere ähnliche Inhalte

Was ist angesagt?

KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Begin...
KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Begin...KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Begin...
KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Begin...Simplilearn
 
Spectral clustering
Spectral clusteringSpectral clustering
Spectral clusteringSOYEON KIM
 
Classification Algorithm.
Classification Algorithm.Classification Algorithm.
Classification Algorithm.Megha Sharma
 
Classification Based Machine Learning Algorithms
Classification Based Machine Learning AlgorithmsClassification Based Machine Learning Algorithms
Classification Based Machine Learning AlgorithmsMd. Main Uddin Rony
 
Introduction to Machine Learning Classifiers
Introduction to Machine Learning ClassifiersIntroduction to Machine Learning Classifiers
Introduction to Machine Learning ClassifiersFunctional Imperative
 
Random forest
Random forestRandom forest
Random forestUjjawal
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine LearningKuppusamy P
 
Introduction to Machine Learning with SciKit-Learn
Introduction to Machine Learning with SciKit-LearnIntroduction to Machine Learning with SciKit-Learn
Introduction to Machine Learning with SciKit-LearnBenjamin Bengfort
 
Introduction to Linear Discriminant Analysis
Introduction to Linear Discriminant AnalysisIntroduction to Linear Discriminant Analysis
Introduction to Linear Discriminant AnalysisJaclyn Kokx
 
K Nearest Neighbor Presentation
K Nearest Neighbor PresentationK Nearest Neighbor Presentation
K Nearest Neighbor PresentationDessy Amirudin
 
Linear models for classification
Linear models for classificationLinear models for classification
Linear models for classificationSung Yub Kim
 
[Final]collaborative filtering and recommender systems
[Final]collaborative filtering and recommender systems[Final]collaborative filtering and recommender systems
[Final]collaborative filtering and recommender systemsFalitokiniaina Rabearison
 
Dimensionality Reduction
Dimensionality ReductionDimensionality Reduction
Dimensionality Reductionmrizwan969
 
From decision trees to random forests
From decision trees to random forestsFrom decision trees to random forests
From decision trees to random forestsViet-Trung TRAN
 

Was ist angesagt? (20)

KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Begin...
KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Begin...KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Begin...
KNN Algorithm - How KNN Algorithm Works With Example | Data Science For Begin...
 
Spectral clustering
Spectral clusteringSpectral clustering
Spectral clustering
 
Classification Algorithm.
Classification Algorithm.Classification Algorithm.
Classification Algorithm.
 
KNN.pptx
KNN.pptxKNN.pptx
KNN.pptx
 
Classification Based Machine Learning Algorithms
Classification Based Machine Learning AlgorithmsClassification Based Machine Learning Algorithms
Classification Based Machine Learning Algorithms
 
Introduction to Machine Learning Classifiers
Introduction to Machine Learning ClassifiersIntroduction to Machine Learning Classifiers
Introduction to Machine Learning Classifiers
 
Random forest
Random forestRandom forest
Random forest
 
K means Clustering Algorithm
K means Clustering AlgorithmK means Clustering Algorithm
K means Clustering Algorithm
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
 
Introduction to Machine Learning with SciKit-Learn
Introduction to Machine Learning with SciKit-LearnIntroduction to Machine Learning with SciKit-Learn
Introduction to Machine Learning with SciKit-Learn
 
KNN
KNNKNN
KNN
 
Introduction to Linear Discriminant Analysis
Introduction to Linear Discriminant AnalysisIntroduction to Linear Discriminant Analysis
Introduction to Linear Discriminant Analysis
 
K Nearest Neighbor Presentation
K Nearest Neighbor PresentationK Nearest Neighbor Presentation
K Nearest Neighbor Presentation
 
Linear models for classification
Linear models for classificationLinear models for classification
Linear models for classification
 
KNN presentation.pdf
KNN presentation.pdfKNN presentation.pdf
KNN presentation.pdf
 
[Final]collaborative filtering and recommender systems
[Final]collaborative filtering and recommender systems[Final]collaborative filtering and recommender systems
[Final]collaborative filtering and recommender systems
 
Dimensionality Reduction
Dimensionality ReductionDimensionality Reduction
Dimensionality Reduction
 
K Nearest Neighbor Algorithm
K Nearest Neighbor AlgorithmK Nearest Neighbor Algorithm
K Nearest Neighbor Algorithm
 
Presentation on K-Means Clustering
Presentation on K-Means ClusteringPresentation on K-Means Clustering
Presentation on K-Means Clustering
 
From decision trees to random forests
From decision trees to random forestsFrom decision trees to random forests
From decision trees to random forests
 

Ähnlich wie K- Nearest Neighbor Approach

Investigating the Performance of Distanced-Based Weighted-Voting approaches i...
Investigating the Performance of Distanced-Based Weighted-Voting approaches i...Investigating the Performance of Distanced-Based Weighted-Voting approaches i...
Investigating the Performance of Distanced-Based Weighted-Voting approaches i...Dario Panada
 
Implementation of K-Nearest Neighbor Algorithm
Implementation of K-Nearest Neighbor AlgorithmImplementation of K-Nearest Neighbor Algorithm
Implementation of K-Nearest Neighbor AlgorithmDipesh Shome
 
MachineLearning.pptx
MachineLearning.pptxMachineLearning.pptx
MachineLearning.pptxBangtangurl
 
Enhancing Classification Accuracy of K-Nearest Neighbors Algorithm using Gain...
Enhancing Classification Accuracy of K-Nearest Neighbors Algorithm using Gain...Enhancing Classification Accuracy of K-Nearest Neighbors Algorithm using Gain...
Enhancing Classification Accuracy of K-Nearest Neighbors Algorithm using Gain...IRJET Journal
 
Lecture_3_k-mean-clustering.ppt
Lecture_3_k-mean-clustering.pptLecture_3_k-mean-clustering.ppt
Lecture_3_k-mean-clustering.pptSyedNahin1
 
instance bases k nearest neighbor algorithm.ppt
instance bases k nearest neighbor algorithm.pptinstance bases k nearest neighbor algorithm.ppt
instance bases k nearest neighbor algorithm.pptJohny139575
 
Massively Parallel K-Nearest Neighbor Computation on Distributed Architectures
Massively Parallel K-Nearest Neighbor Computation on Distributed Architectures Massively Parallel K-Nearest Neighbor Computation on Distributed Architectures
Massively Parallel K-Nearest Neighbor Computation on Distributed Architectures Intel® Software
 
Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks, arXiv e-...
Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks, arXiv e-...Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks, arXiv e-...
Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks, arXiv e-...ssuser2624f71
 
MrKNN_Soft Relevance for Multi-label Classification
MrKNN_Soft Relevance for Multi-label ClassificationMrKNN_Soft Relevance for Multi-label Classification
MrKNN_Soft Relevance for Multi-label ClassificationYI-JHEN LIN
 
Satellite Image Classification using Decision Tree, SVM and k-Nearest Neighbor
Satellite Image Classification using Decision Tree, SVM and k-Nearest NeighborSatellite Image Classification using Decision Tree, SVM and k-Nearest Neighbor
Satellite Image Classification using Decision Tree, SVM and k-Nearest NeighborNational Cheng Kung University
 

Ähnlich wie K- Nearest Neighbor Approach (20)

K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
 
KNN Classifier
KNN ClassifierKNN Classifier
KNN Classifier
 
Investigating the Performance of Distanced-Based Weighted-Voting approaches i...
Investigating the Performance of Distanced-Based Weighted-Voting approaches i...Investigating the Performance of Distanced-Based Weighted-Voting approaches i...
Investigating the Performance of Distanced-Based Weighted-Voting approaches i...
 
Implementation of K-Nearest Neighbor Algorithm
Implementation of K-Nearest Neighbor AlgorithmImplementation of K-Nearest Neighbor Algorithm
Implementation of K-Nearest Neighbor Algorithm
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Knn demonstration
Knn demonstrationKnn demonstration
Knn demonstration
 
Data analysis of weather forecasting
Data analysis of weather forecastingData analysis of weather forecasting
Data analysis of weather forecasting
 
knn-1.pptx
knn-1.pptxknn-1.pptx
knn-1.pptx
 
MachineLearning.pptx
MachineLearning.pptxMachineLearning.pptx
MachineLearning.pptx
 
Enhancing Classification Accuracy of K-Nearest Neighbors Algorithm using Gain...
Enhancing Classification Accuracy of K-Nearest Neighbors Algorithm using Gain...Enhancing Classification Accuracy of K-Nearest Neighbors Algorithm using Gain...
Enhancing Classification Accuracy of K-Nearest Neighbors Algorithm using Gain...
 
KNN
KNNKNN
KNN
 
Lecture_3_k-mean-clustering.ppt
Lecture_3_k-mean-clustering.pptLecture_3_k-mean-clustering.ppt
Lecture_3_k-mean-clustering.ppt
 
Cluster Analysis for Dummies
Cluster Analysis for DummiesCluster Analysis for Dummies
Cluster Analysis for Dummies
 
instance bases k nearest neighbor algorithm.ppt
instance bases k nearest neighbor algorithm.pptinstance bases k nearest neighbor algorithm.ppt
instance bases k nearest neighbor algorithm.ppt
 
Massively Parallel K-Nearest Neighbor Computation on Distributed Architectures
Massively Parallel K-Nearest Neighbor Computation on Distributed Architectures Massively Parallel K-Nearest Neighbor Computation on Distributed Architectures
Massively Parallel K-Nearest Neighbor Computation on Distributed Architectures
 
Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks, arXiv e-...
Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks, arXiv e-...Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks, arXiv e-...
Weisfeiler and Leman Go Neural: Higher-order Graph Neural Networks, arXiv e-...
 
Lower back pain Regression models
Lower back pain Regression modelsLower back pain Regression models
Lower back pain Regression models
 
MrKNN_Soft Relevance for Multi-label Classification
MrKNN_Soft Relevance for Multi-label ClassificationMrKNN_Soft Relevance for Multi-label Classification
MrKNN_Soft Relevance for Multi-label Classification
 
K means report
K means reportK means report
K means report
 
Satellite Image Classification using Decision Tree, SVM and k-Nearest Neighbor
Satellite Image Classification using Decision Tree, SVM and k-Nearest NeighborSatellite Image Classification using Decision Tree, SVM and k-Nearest Neighbor
Satellite Image Classification using Decision Tree, SVM and k-Nearest Neighbor
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 

Kürzlich hochgeladen (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

K- Nearest Neighbor Approach

  • 1. K-Nearest Neighbor –As Supervised Classifier Compiled by : Dr. Kumud Kundu
  • 2. K Nearest Neighbor – As a Supervised Classification Approach • KNN is a non-parametric supervised learning technique in which the query instance is classified to a given category with the help of training set. • Non-parametric means not making any assumptions on the underlying data distribution. • Predictions are made for a new instance (x) by searching through the entire training set for the K most similar cases (neighbors) and summarizing the output variable for those K cases. • In simple words, it captures information of all training cases and classifies new cases based on a similarity. 2
  • 3. Two Phases of Supervised Classification 3
  • 4. 4 EUCLIDEAN DISTANCE: SIMILARITY METRIC 𝑦2 𝑦1 𝑥1 𝑥2 𝑃𝑜𝑖𝑛𝑡 𝐵 (𝑥2, 𝑦2) 𝑃𝑜𝑖𝑛𝑡 𝐴 (𝑥1, 𝑦1) 𝑬𝒖𝒄𝒍𝒊𝒅𝒆𝒂𝒏 𝑫𝒊𝒔𝒕𝒂𝒏𝒄𝒆 = 𝒙 𝟐 − 𝒙 𝟏 𝟐 + 𝒚 𝟐 − 𝒚 𝟏 𝟐
  • 5. 5 MANHATTAN DISTANCE or CITY BLOCK DISTANCE: SIMILARITY METRIC 𝑦2 𝑦1 𝑥1 𝑥2 𝑃𝑜𝑖𝑛𝑡 𝐵 (𝑥2, 𝑦2) 𝑃𝑜𝑖𝑛𝑡 𝐴 (𝑥1, 𝑦1) 𝑴𝒂𝒏𝒉𝒂𝒕𝒕𝒂𝒏 𝑫𝒊𝒔𝒕𝒂𝒏𝒄𝒆 = | 𝒙 𝟐 − 𝒙 𝟏 | + | 𝒚 𝟐 − 𝒚 𝟏 |
  • 6. 6 1. Select a value for k (e.g.: 1, 2, 3, 10..) 2. Calculate the Euclidian distance between the point to be classified and every other point in the training data-set 3. Pick the k closest data points (points with the k smallest distances) 4. Run a majority vote among selected data points, the dominating classification is the winner! Point is classified based on the dominant class. 5. Repeat if required! K NEAREST NEIGHBORS (KNN): ALGORITHM STEPS
  • 7. 7 Predict Class for Tuple ( 10,7) Apply KNN Classification Algorithm
  • 8. Solution with K=3 Feature 1 Feature 2 Class Euclidean Distance Rank 1 1A 10.81665383 8 2 3A 8.94427191 7 2 4A 8.544003745 6 5 3A 6.403124237 5 8 6B 2.236067977 4 8 8B 2.236067977 3 9 6B 1.414213562 2 11 7B 1 1 Predict for 10 7 Predicted Class = B 8 1. Calculate the Euclidian distance between the point to be classified and every other point in the training data-set 2. Pick the k=3 closest data points (points with the k smallest distances) 3. Run a majority vote among selected data points, the dominating classification is the winner! Point is classified based on the dominant class.
  • 9. How to decide the number of neighbors in KNN? What are its effects on the classification Accuracy? The number of neighbors(K) in KNN is a hyperparameter that is needed to be chosen at the time of model building. • K controls the classification accuracy of the model. • Generally, K is chosen as an odd number if the number of classes is even. • Otherwise value of K is dependent upon the nature of dataset (Domain Dependent ) for which it is to be applied. 9
  • 10. KNN Implementation in Python • First, import the KNeighborsClassifier module and create KNN classifier object by passing argument number of neighbors in KNeighborsClassifier() function. • by usingthe sklearn.neighbors.NearestNeighbors module classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2) # KNN model with 5 neighbours and Euclidian distance as similarity metric • Then, fit your model on the train set using fit() and perform prediction on the test set using predict(). # Fitting K-NN to the Training set • classifier.fit(X_train, y_train) # Predicting the Test set results • y_predy_pred = classifier.predict(X_test) 10 from sklearn.neighbors import NearestNeighbors
  • 11. QUICK CHECK *Which of the following statements is true for k-NN classifiers? A) The classification accuracy is better with larger values of k B) The decision boundary is smoother with smaller values of k C) The decision boundary is linear D) k-NN does not require an explicit training step *k-NN algorithm does more computation on test time rather than train time. A) TRUE B) FALSE *Which of the following statement is true about k-NN algorithm? 1. k-NN performs much better if all of the data have the same scale 2. k-NN works well with a small number of input variables (p), but struggles when the number of inputs is very large 3. k-NN makes no assumptions about the functional form of the problem being solved A) 1 and 2 B) 1 and 3 C) Only 1 D) All of the above 11