SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
K - Nearest Neighbours
Classification , Regression
Khan
Introduction
KNN
K - Nearest neighbors is a
lazy learning instance based
classification( regression )
algorithm which is widely
implemented in both
supervised and unsupervised
learning techniques.
Nearest Neighbors Techniques
UnSupervised Learning
● Manifold learning
● Spectral clustering
Supervised Learning
Classification / Regression
● K nearest neighbor
● Radius neighbor
It is lazy Learner as it doesn't learn from a discriminative function
from training data but memorizes training dataset.
This technique implements classification by considering majority of
vote among the “k” closest points to the unlabeled data point.
It works on unseen data and will search through the training dataset
for the k-most similar instances.
Euclidean distance / Hamming distance is used as metric for
calculating the distance between points.
Principle of KNN - Classifier
The Euclidean distance between two points in the plane with
coordinates (x, y) and (a, b) is given by
dist((x, y), (a, b)) = √((x - a)ÂČ + (y - b)ÂČ))
Hamming distance between data of equal length is the number of
positions at which the corresponding character are different.
oneforone oneandone → 3
11010110110 11000111110 → 2
Euclidean distance / Hamming distance
K Nearest Neighbor
Green circle is the unlabeled
data point
k=3 in this problem
● Closest 3 points taken
● 2 are red 1 is blue
● Votes = 2Red > 1Blue
● Green circle is a red
triangle.K = 3
K Nearest Neighbor
Green circle is the unlabeled
data point
k=5 in this problem
● Closest 5 points taken
● 2 are red 3 are blue
● Votes = 2Red < 3Blue
● Green circle is a Blue
square.
K = 5
This implements learning based on the number of neighbors within
a fixed radius of each training point.
RadiusNeighborsClassifier can be a better choice For
high-dimensional parameter spaces
The radius floating point is provided by the user for taking the
Points into consideration.
This method becomes less effective due to “curse of
dimensionality”.
RadiusNeighborsClassifier
RadiusNeighborsClassifier
● Radius = 2 units
● Within radius
○ 9 blue dots
○ 10 purple dots
● Black dot is
predicted to be a
purple dot as per
votes.
2 units
Influence of K on prediction
K = 1 → perfect classification with overfitting
K = ∞ → entire classification becomes single class
K = 3 K = 10
Choosing value of “ K “
k should be large so that error rate is minimized
k too small will lead to noisy decision boundaries
k should be small enough so that only nearby samples are included
k too large will lead to over-smoothed boundaries
Setting K to the square root of the number of training samples can lead
to better results.
No Of features = 20
K = √20 = 4.4 ~ 4
K values vs Error curve
K values vs Validation error curve
Minimum error
Best value of K
Pros :
● Non complex and Very easy to understand and implement.
● Useful for non linear data as No assumptions about data.
● High accuracy (relatively), but not competitive compared to
Supervised learning algorithms.
● Can be used both for classification or regression.
● Best used where where the probability distribution is unknown
Cons :
● Computationally expensive.
● Lot of space is consumed as all the data points are stored .
● Sensitive to irrelevant features and the scale of the data.
● Output purely depends on K value chosen by user which can
reduce accuracy for some values.
Applications :
1. Recommender Systems
2. Medicine
3. Finance
4. Text mining
5. Agriculture
Let’s code now
Data used : Iris from Sklearn
Plots : Matplotlib
K values taken - 1, 3 , 10 , 150
File : knnKpara.py
Link to code : Click here for code
Thank You

Weitere Àhnliche Inhalte

Was ist angesagt?

Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Simplilearn
 
Decision tree
Decision treeDecision tree
Decision tree
R A Akerkar
 

Was ist angesagt? (20)

K means Clustering Algorithm
K means Clustering AlgorithmK means Clustering Algorithm
K means Clustering Algorithm
 
Convolution Neural Network (CNN)
Convolution Neural Network (CNN)Convolution Neural Network (CNN)
Convolution Neural Network (CNN)
 
Presentation on K-Means Clustering
Presentation on K-Means ClusteringPresentation on K-Means Clustering
Presentation on K-Means Clustering
 
Machine Learning with Decision trees
Machine Learning with Decision treesMachine Learning with Decision trees
Machine Learning with Decision trees
 
Introduction to Clustering algorithm
Introduction to Clustering algorithmIntroduction to Clustering algorithm
Introduction to Clustering algorithm
 
Naive bayes
Naive bayesNaive bayes
Naive bayes
 
2.4 rule based classification
2.4 rule based classification2.4 rule based classification
2.4 rule based classification
 
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
Decision Tree Algorithm With Example | Decision Tree In Machine Learning | Da...
 
Classification and Regression
Classification and RegressionClassification and Regression
Classification and Regression
 
Decision trees in Machine Learning
Decision trees in Machine Learning Decision trees in Machine Learning
Decision trees in Machine Learning
 
K Nearest Neighbor Algorithm
K Nearest Neighbor AlgorithmK Nearest Neighbor Algorithm
K Nearest Neighbor Algorithm
 
Decision tree
Decision treeDecision tree
Decision tree
 
Supervised learning and Unsupervised learning
Supervised learning and Unsupervised learning Supervised learning and Unsupervised learning
Supervised learning and Unsupervised learning
 
Unsupervised learning clustering
Unsupervised learning clusteringUnsupervised learning clustering
Unsupervised learning clustering
 
Cnn
CnnCnn
Cnn
 
Knn 160904075605-converted
Knn 160904075605-convertedKnn 160904075605-converted
Knn 160904075605-converted
 
Data preprocessing using Machine Learning
Data  preprocessing using Machine Learning Data  preprocessing using Machine Learning
Data preprocessing using Machine Learning
 
Clustering, k-means clustering
Clustering, k-means clusteringClustering, k-means clustering
Clustering, k-means clustering
 
Knn Algorithm presentation
Knn Algorithm presentationKnn Algorithm presentation
Knn Algorithm presentation
 
k medoid clustering.pptx
k medoid clustering.pptxk medoid clustering.pptx
k medoid clustering.pptx
 

Ähnlich wie K - Nearest neighbor ( KNN )

Ähnlich wie K - Nearest neighbor ( KNN ) (20)

KNN Classifier
KNN ClassifierKNN Classifier
KNN Classifier
 
K- Nearest Neighbor Approach
K- Nearest Neighbor ApproachK- Nearest Neighbor Approach
K- Nearest Neighbor Approach
 
ML MODULE 4.pdf
ML MODULE 4.pdfML MODULE 4.pdf
ML MODULE 4.pdf
 
Lecture 11 - KNN and Clustering, a lecture in subject module Statistical & Ma...
Lecture 11 - KNN and Clustering, a lecture in subject module Statistical & Ma...Lecture 11 - KNN and Clustering, a lecture in subject module Statistical & Ma...
Lecture 11 - KNN and Clustering, a lecture in subject module Statistical & Ma...
 
KNN.pptx
KNN.pptxKNN.pptx
KNN.pptx
 
Knn
KnnKnn
Knn
 
k-Nearest Neighbors with brief explanation.pptx
k-Nearest Neighbors with brief explanation.pptxk-Nearest Neighbors with brief explanation.pptx
k-Nearest Neighbors with brief explanation.pptx
 
K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
 
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...
 
Machine Learning Algorithm - KNN
Machine Learning Algorithm - KNNMachine Learning Algorithm - KNN
Machine Learning Algorithm - KNN
 
Data mining with weka
Data mining with wekaData mining with weka
Data mining with weka
 
Data analysis of weather forecasting
Data analysis of weather forecastingData analysis of weather forecasting
Data analysis of weather forecasting
 
CLUSTER ANALYSIS ALGORITHMS.pptx
CLUSTER ANALYSIS ALGORITHMS.pptxCLUSTER ANALYSIS ALGORITHMS.pptx
CLUSTER ANALYSIS ALGORITHMS.pptx
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Machine learning in science and industry — day 1
Machine learning in science and industry — day 1Machine learning in science and industry — day 1
Machine learning in science and industry — day 1
 
Master defense presentation 2019 04_18_rev2
Master defense presentation 2019 04_18_rev2Master defense presentation 2019 04_18_rev2
Master defense presentation 2019 04_18_rev2
 
K-NN K-Nearest Neighbors Algorithm.pptx
K-NN K-Nearest Neighbors Algorithm.pptxK-NN K-Nearest Neighbors Algorithm.pptx
K-NN K-Nearest Neighbors Algorithm.pptx
 
3.2 partitioning methods
3.2 partitioning methods3.2 partitioning methods
3.2 partitioning methods
 
Instance based learning
Instance based learningInstance based learning
Instance based learning
 
MachineLearning.pptx
MachineLearning.pptxMachineLearning.pptx
MachineLearning.pptx
 

Mehr von Mohammad Junaid Khan (6)

Support Vector Machines ( SVM )
Support Vector Machines ( SVM ) Support Vector Machines ( SVM )
Support Vector Machines ( SVM )
 
Rise of PHP and Laravel. Latest Web Frameworks built on PHP.
Rise of PHP and Laravel. Latest Web Frameworks built on PHP.Rise of PHP and Laravel. Latest Web Frameworks built on PHP.
Rise of PHP and Laravel. Latest Web Frameworks built on PHP.
 
Python
PythonPython
Python
 
My SQL 1
My SQL 1My SQL 1
My SQL 1
 
Ruby_Basic.pptx
Ruby_Basic.pptxRuby_Basic.pptx
Ruby_Basic.pptx
 
Wireless transmission of power
Wireless transmission of powerWireless transmission of power
Wireless transmission of power
 

KĂŒrzlich hochgeladen

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

KĂŒrzlich hochgeladen (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 

K - Nearest neighbor ( KNN )

  • 1. K - Nearest Neighbours Classification , Regression Khan
  • 2. Introduction KNN K - Nearest neighbors is a lazy learning instance based classification( regression ) algorithm which is widely implemented in both supervised and unsupervised learning techniques.
  • 3. Nearest Neighbors Techniques UnSupervised Learning ● Manifold learning ● Spectral clustering Supervised Learning Classification / Regression ● K nearest neighbor ● Radius neighbor
  • 4. It is lazy Learner as it doesn't learn from a discriminative function from training data but memorizes training dataset. This technique implements classification by considering majority of vote among the “k” closest points to the unlabeled data point. It works on unseen data and will search through the training dataset for the k-most similar instances. Euclidean distance / Hamming distance is used as metric for calculating the distance between points. Principle of KNN - Classifier
  • 5. The Euclidean distance between two points in the plane with coordinates (x, y) and (a, b) is given by dist((x, y), (a, b)) = √((x - a)ÂČ + (y - b)ÂČ)) Hamming distance between data of equal length is the number of positions at which the corresponding character are different. oneforone oneandone → 3 11010110110 11000111110 → 2 Euclidean distance / Hamming distance
  • 6. K Nearest Neighbor Green circle is the unlabeled data point k=3 in this problem ● Closest 3 points taken ● 2 are red 1 is blue ● Votes = 2Red > 1Blue ● Green circle is a red triangle.K = 3
  • 7. K Nearest Neighbor Green circle is the unlabeled data point k=5 in this problem ● Closest 5 points taken ● 2 are red 3 are blue ● Votes = 2Red < 3Blue ● Green circle is a Blue square. K = 5
  • 8. This implements learning based on the number of neighbors within a fixed radius of each training point. RadiusNeighborsClassifier can be a better choice For high-dimensional parameter spaces The radius floating point is provided by the user for taking the Points into consideration. This method becomes less effective due to “curse of dimensionality”. RadiusNeighborsClassifier
  • 9. RadiusNeighborsClassifier ● Radius = 2 units ● Within radius ○ 9 blue dots ○ 10 purple dots ● Black dot is predicted to be a purple dot as per votes. 2 units
  • 10. Influence of K on prediction K = 1 → perfect classification with overfitting K = ∞ → entire classification becomes single class
  • 11. K = 3 K = 10
  • 12. Choosing value of “ K “ k should be large so that error rate is minimized k too small will lead to noisy decision boundaries k should be small enough so that only nearby samples are included k too large will lead to over-smoothed boundaries Setting K to the square root of the number of training samples can lead to better results. No Of features = 20 K = √20 = 4.4 ~ 4
  • 13. K values vs Error curve
  • 14. K values vs Validation error curve Minimum error Best value of K
  • 15. Pros : ● Non complex and Very easy to understand and implement. ● Useful for non linear data as No assumptions about data. ● High accuracy (relatively), but not competitive compared to Supervised learning algorithms. ● Can be used both for classification or regression. ● Best used where where the probability distribution is unknown
  • 16. Cons : ● Computationally expensive. ● Lot of space is consumed as all the data points are stored . ● Sensitive to irrelevant features and the scale of the data. ● Output purely depends on K value chosen by user which can reduce accuracy for some values.
  • 17. Applications : 1. Recommender Systems 2. Medicine 3. Finance 4. Text mining 5. Agriculture
  • 18. Let’s code now Data used : Iris from Sklearn Plots : Matplotlib K values taken - 1, 3 , 10 , 150 File : knnKpara.py Link to code : Click here for code