Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
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

More Related Content

What's hot

KNN
KNNKNN
KNN presentation.pdf
KNN presentation.pdfKNN presentation.pdf
KNN presentation.pdf
AbhilashChauhan14
 
K-Nearest Neighbor Classifier
K-Nearest Neighbor ClassifierK-Nearest Neighbor Classifier
K-Nearest Neighbor Classifier
Neha Kulkarni
 
Unsupervised learning clustering
Unsupervised learning clusteringUnsupervised learning clustering
Unsupervised learning clustering
Arshad Farhad
 
KNN
KNN KNN
Unsupervised learning (clustering)
Unsupervised learning (clustering)Unsupervised learning (clustering)
Unsupervised learning (clustering)
Pravinkumar Landge
 
5.1 K plus proches voisins
5.1 K plus proches voisins5.1 K plus proches voisins
5.1 K plus proches voisins
Boris Guarisma
 
K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...
K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...
K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...
Simplilearn
 
K means Clustering Algorithm
K means Clustering AlgorithmK means Clustering Algorithm
K means Clustering Algorithm
Kasun Ranga Wijeweera
 
Decision tree
Decision treeDecision tree
Decision tree
Ami_Surati
 
Association rules
Association rulesAssociation rules
Association rules
Dr. C.V. Suresh Babu
 
Hierachical clustering
Hierachical clusteringHierachical clustering
Knn 160904075605-converted
Knn 160904075605-convertedKnn 160904075605-converted
Knn 160904075605-converted
rameswara reddy venkat
 
Association Rule Mining in Data Mining
Association Rule Mining in Data Mining Association Rule Mining in Data Mining
Association Rule Mining in Data Mining
Ayesha Ali
 
k Nearest Neighbor
k Nearest Neighbork Nearest Neighbor
k Nearest Neighbor
butest
 
K-Means manual work
K-Means manual workK-Means manual work
K-Means manual work
Dr.E.N.Sathishkumar
 
Machine Learning with Decision trees
Machine Learning with Decision treesMachine Learning with Decision trees
Machine Learning with Decision trees
Knoldus Inc.
 
K MEANS CLUSTERING
K MEANS CLUSTERINGK MEANS CLUSTERING
K MEANS CLUSTERING
singh7599
 
Matrix Factorization
Matrix FactorizationMatrix Factorization
Matrix Factorization
Yusuke Yamamoto
 
Presentation on K-Means Clustering
Presentation on K-Means ClusteringPresentation on K-Means Clustering
Presentation on K-Means Clustering
Pabna University of Science & Technology
 

What's hot (20)

KNN
KNNKNN
KNN
 
KNN presentation.pdf
KNN presentation.pdfKNN presentation.pdf
KNN presentation.pdf
 
K-Nearest Neighbor Classifier
K-Nearest Neighbor ClassifierK-Nearest Neighbor Classifier
K-Nearest Neighbor Classifier
 
Unsupervised learning clustering
Unsupervised learning clusteringUnsupervised learning clustering
Unsupervised learning clustering
 
KNN
KNN KNN
KNN
 
Unsupervised learning (clustering)
Unsupervised learning (clustering)Unsupervised learning (clustering)
Unsupervised learning (clustering)
 
5.1 K plus proches voisins
5.1 K plus proches voisins5.1 K plus proches voisins
5.1 K plus proches voisins
 
K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...
K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...
K Means Clustering Algorithm | K Means Clustering Example | Machine Learning ...
 
K means Clustering Algorithm
K means Clustering AlgorithmK means Clustering Algorithm
K means Clustering Algorithm
 
Decision tree
Decision treeDecision tree
Decision tree
 
Association rules
Association rulesAssociation rules
Association rules
 
Hierachical clustering
Hierachical clusteringHierachical clustering
Hierachical clustering
 
Knn 160904075605-converted
Knn 160904075605-convertedKnn 160904075605-converted
Knn 160904075605-converted
 
Association Rule Mining in Data Mining
Association Rule Mining in Data Mining Association Rule Mining in Data Mining
Association Rule Mining in Data Mining
 
k Nearest Neighbor
k Nearest Neighbork Nearest Neighbor
k Nearest Neighbor
 
K-Means manual work
K-Means manual workK-Means manual work
K-Means manual work
 
Machine Learning with Decision trees
Machine Learning with Decision treesMachine Learning with Decision trees
Machine Learning with Decision trees
 
K MEANS CLUSTERING
K MEANS CLUSTERINGK MEANS CLUSTERING
K MEANS CLUSTERING
 
Matrix Factorization
Matrix FactorizationMatrix Factorization
Matrix Factorization
 
Presentation on K-Means Clustering
Presentation on K-Means ClusteringPresentation on K-Means Clustering
Presentation on K-Means Clustering
 

Similar to K- Nearest Neighbor Approach

K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
Abdullah al Mamun
 
KNN Classifier
KNN ClassifierKNN Classifier
KNN Classifier
Mobashshirur Rahman 👲
 
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
 
KNN.pptx
KNN.pptxKNN.pptx
KNN.pptx
Mohamed Essam
 
Implementation of K-Nearest Neighbor Algorithm
Implementation of K-Nearest Neighbor AlgorithmImplementation of K-Nearest Neighbor Algorithm
Implementation of K-Nearest Neighbor Algorithm
Dipesh Shome
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
Zahra Amini
 
Knn demonstration
Knn demonstrationKnn demonstration
Knn demonstration
Md. Abu Bakr Siddique
 
Data analysis of weather forecasting
Data analysis of weather forecastingData analysis of weather forecasting
Data analysis of weather forecasting
Trupti Shingala, WAS, CPACC, CPWA, JAWS, CSM
 
knn-1.pptx
knn-1.pptxknn-1.pptx
knn-1.pptx
MohammedSahil63
 
MachineLearning.pptx
MachineLearning.pptxMachineLearning.pptx
MachineLearning.pptx
Bangtangurl
 
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.ppt
SyedNahin1
 
Cluster Analysis for Dummies
Cluster Analysis for DummiesCluster Analysis for Dummies
Cluster Analysis for Dummies
Venkata Reddy Konasani
 
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
Johny139575
 
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
 
Lower back pain Regression models
Lower back pain Regression modelsLower back pain Regression models
Lower back pain Regression models
Alok Tayal (PMP, PMI-ACP, TOGAF)
 
K Nearest Neighbors
K Nearest NeighborsK Nearest Neighbors
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
YI-JHEN LIN
 
K means report
K means reportK means report
K means report
Gaurav Handa
 

Similar to 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...
 
KNN.pptx
KNN.pptxKNN.pptx
KNN.pptx
 
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...
 
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
 
K Nearest Neighbors
K Nearest NeighborsK Nearest Neighbors
K Nearest Neighbors
 
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
 

Recently uploaded

QND: VOL2 GRAND FINALE QUIZ by Qui9 (2024)
QND: VOL2  GRAND FINALE QUIZ by Qui9 (2024)QND: VOL2  GRAND FINALE QUIZ by Qui9 (2024)
QND: VOL2 GRAND FINALE QUIZ by Qui9 (2024)
Qui9 (Ultimate Quizzing)
 
Q1_LE_Music and Arts 7_Lesson 1_Weeks 1-2.docx
Q1_LE_Music and Arts 7_Lesson 1_Weeks 1-2.docxQ1_LE_Music and Arts 7_Lesson 1_Weeks 1-2.docx
Q1_LE_Music and Arts 7_Lesson 1_Weeks 1-2.docx
ElynTGomez
 
Stéphan Vincent-Lancrin, Deputy Head of IMEP division and Senior Analyst - P...
Stéphan Vincent-Lancrin, Deputy Head of IMEP division and Senior Analyst  - P...Stéphan Vincent-Lancrin, Deputy Head of IMEP division and Senior Analyst  - P...
Stéphan Vincent-Lancrin, Deputy Head of IMEP division and Senior Analyst - P...
EduSkills OECD
 
How to Restrict Price Modification to Managers in Odoo 17 POS
How to Restrict Price Modification to Managers in Odoo 17 POSHow to Restrict Price Modification to Managers in Odoo 17 POS
How to Restrict Price Modification to Managers in Odoo 17 POS
Celine George
 
How to Add Collaborators to a Project in Odoo 17
How to Add Collaborators to a Project in Odoo 17How to Add Collaborators to a Project in Odoo 17
How to Add Collaborators to a Project in Odoo 17
Celine George
 
How to install python packages from Pycharm
How to install python packages from PycharmHow to install python packages from Pycharm
How to install python packages from Pycharm
Celine George
 
How to Configure Field Cleaning Rules in Odoo 17
How to Configure Field Cleaning Rules in Odoo 17How to Configure Field Cleaning Rules in Odoo 17
How to Configure Field Cleaning Rules in Odoo 17
Celine George
 
Powerpoint on Classroom Orientation2024-2025
Powerpoint on Classroom Orientation2024-2025Powerpoint on Classroom Orientation2024-2025
Powerpoint on Classroom Orientation2024-2025
MarynolMagbanuaJimer
 
How to Manage Advanced Pricelist in Odoo 17
How to Manage Advanced Pricelist in Odoo 17How to Manage Advanced Pricelist in Odoo 17
How to Manage Advanced Pricelist in Odoo 17
Celine George
 
Email Marketing in Odoo 17 - Odoo 17 Slides
Email Marketing  in Odoo 17 - Odoo 17 SlidesEmail Marketing  in Odoo 17 - Odoo 17 Slides
Email Marketing in Odoo 17 - Odoo 17 Slides
Celine George
 
Odoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 SlidesOdoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 Slides
Celine George
 
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdfFINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
Janna Marie Ballo
 
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 InventoryHow to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
Celine George
 
Types of Diode and its working principle.pptx
Types of Diode and its working principle.pptxTypes of Diode and its working principle.pptx
Types of Diode and its working principle.pptx
nitugatkal
 
ACTION PLAN ON NUTRITION MONTH 2024.docx
ACTION PLAN ON NUTRITION MONTH 2024.docxACTION PLAN ON NUTRITION MONTH 2024.docx
ACTION PLAN ON NUTRITION MONTH 2024.docx
LeviMaePacatang1
 
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptxModule-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
MichelleMercado36
 
Replacing the Whole Capitalist Stack.pdf
Replacing the Whole Capitalist Stack.pdfReplacing the Whole Capitalist Stack.pdf
Replacing the Whole Capitalist Stack.pdf
StefanMz
 
How to Integrate Facebook in Odoo 17 - Odoo 17 Slides
How to Integrate Facebook in Odoo 17 - Odoo 17 SlidesHow to Integrate Facebook in Odoo 17 - Odoo 17 Slides
How to Integrate Facebook in Odoo 17 - Odoo 17 Slides
Celine George
 
What is the Difference Between Lot & Serial Number in Odoo 17
What is the Difference Between Lot & Serial Number in Odoo 17What is the Difference Between Lot & Serial Number in Odoo 17
What is the Difference Between Lot & Serial Number in Odoo 17
Celine George
 
Personality Development , Dr. SAROJ KUMAR DATTA
Personality Development , Dr. SAROJ KUMAR DATTAPersonality Development , Dr. SAROJ KUMAR DATTA
Personality Development , Dr. SAROJ KUMAR DATTA
CallplanetsDeveloper
 

Recently uploaded (20)

QND: VOL2 GRAND FINALE QUIZ by Qui9 (2024)
QND: VOL2  GRAND FINALE QUIZ by Qui9 (2024)QND: VOL2  GRAND FINALE QUIZ by Qui9 (2024)
QND: VOL2 GRAND FINALE QUIZ by Qui9 (2024)
 
Q1_LE_Music and Arts 7_Lesson 1_Weeks 1-2.docx
Q1_LE_Music and Arts 7_Lesson 1_Weeks 1-2.docxQ1_LE_Music and Arts 7_Lesson 1_Weeks 1-2.docx
Q1_LE_Music and Arts 7_Lesson 1_Weeks 1-2.docx
 
Stéphan Vincent-Lancrin, Deputy Head of IMEP division and Senior Analyst - P...
Stéphan Vincent-Lancrin, Deputy Head of IMEP division and Senior Analyst  - P...Stéphan Vincent-Lancrin, Deputy Head of IMEP division and Senior Analyst  - P...
Stéphan Vincent-Lancrin, Deputy Head of IMEP division and Senior Analyst - P...
 
How to Restrict Price Modification to Managers in Odoo 17 POS
How to Restrict Price Modification to Managers in Odoo 17 POSHow to Restrict Price Modification to Managers in Odoo 17 POS
How to Restrict Price Modification to Managers in Odoo 17 POS
 
How to Add Collaborators to a Project in Odoo 17
How to Add Collaborators to a Project in Odoo 17How to Add Collaborators to a Project in Odoo 17
How to Add Collaborators to a Project in Odoo 17
 
How to install python packages from Pycharm
How to install python packages from PycharmHow to install python packages from Pycharm
How to install python packages from Pycharm
 
How to Configure Field Cleaning Rules in Odoo 17
How to Configure Field Cleaning Rules in Odoo 17How to Configure Field Cleaning Rules in Odoo 17
How to Configure Field Cleaning Rules in Odoo 17
 
Powerpoint on Classroom Orientation2024-2025
Powerpoint on Classroom Orientation2024-2025Powerpoint on Classroom Orientation2024-2025
Powerpoint on Classroom Orientation2024-2025
 
How to Manage Advanced Pricelist in Odoo 17
How to Manage Advanced Pricelist in Odoo 17How to Manage Advanced Pricelist in Odoo 17
How to Manage Advanced Pricelist in Odoo 17
 
Email Marketing in Odoo 17 - Odoo 17 Slides
Email Marketing  in Odoo 17 - Odoo 17 SlidesEmail Marketing  in Odoo 17 - Odoo 17 Slides
Email Marketing in Odoo 17 - Odoo 17 Slides
 
Odoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 SlidesOdoo 17 Project Module : New Features - Odoo 17 Slides
Odoo 17 Project Module : New Features - Odoo 17 Slides
 
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdfFINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
FINAL MATATAG LANGUAGE CG 2023 Grade 1.pdf
 
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 InventoryHow to Use Serial Numbers to Track Products in Odoo 17 Inventory
How to Use Serial Numbers to Track Products in Odoo 17 Inventory
 
Types of Diode and its working principle.pptx
Types of Diode and its working principle.pptxTypes of Diode and its working principle.pptx
Types of Diode and its working principle.pptx
 
ACTION PLAN ON NUTRITION MONTH 2024.docx
ACTION PLAN ON NUTRITION MONTH 2024.docxACTION PLAN ON NUTRITION MONTH 2024.docx
ACTION PLAN ON NUTRITION MONTH 2024.docx
 
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptxModule-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
Module-1_Sectors-of-ICT-and-Its-Career-and-Business-Opportunities-e6qbvs.pptx
 
Replacing the Whole Capitalist Stack.pdf
Replacing the Whole Capitalist Stack.pdfReplacing the Whole Capitalist Stack.pdf
Replacing the Whole Capitalist Stack.pdf
 
How to Integrate Facebook in Odoo 17 - Odoo 17 Slides
How to Integrate Facebook in Odoo 17 - Odoo 17 SlidesHow to Integrate Facebook in Odoo 17 - Odoo 17 Slides
How to Integrate Facebook in Odoo 17 - Odoo 17 Slides
 
What is the Difference Between Lot & Serial Number in Odoo 17
What is the Difference Between Lot & Serial Number in Odoo 17What is the Difference Between Lot & Serial Number in Odoo 17
What is the Difference Between Lot & Serial Number in Odoo 17
 
Personality Development , Dr. SAROJ KUMAR DATTA
Personality Development , Dr. SAROJ KUMAR DATTAPersonality Development , Dr. SAROJ KUMAR DATTA
Personality Development , Dr. SAROJ KUMAR DATTA
 

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