SlideShare ist ein Scribd-Unternehmen logo
1 von 9
Carla P. Gomes
CS4700
K Nearest Neighbor - CLASSIFICATION
Carla P. Gomes
CS4700
1-Nearest Neighbor
One of the simplest of all machine learning classifiers
Simple idea: label a new point the same as the closest known point
Label it red.
Carla P. Gomes
CS4700
1-NN’s Aspects as an
Instance-Based Learner:
A distance metric
– Euclidean
– When different units are used for each dimension
 normalize each dimension by standard deviation
– For discrete data, can use hamming distance
 D(x1,x2) =number of features on which x1 and x2 differ
– Others (e.g., normal, cosine)
How many nearby neighbors to look at?
– One
How to fit with the local points?
– Just predict the same output as the nearest neighbor.
Adapted from “Instance-Based Learning”
lecture slides by Andrew Moore, CMU.
Carla P. Gomes
CS4700
k – Nearest Neighbor
Generalizes 1-NN to smooth away noise in the labels
A new point is now assigned the most frequent label of its k nearest
neighbors
Label it red, when k = 3
Label it blue, when k = 7
KNN Example
New examples:
– Example 1 (great, no, no, normal, no)
– Example 2 (mediocre, yes, no, normal, no)
Food
(3)
Chat
(2)
Fast
(2)
Price
(3)
Bar
(2)
BigTip
1 great yes yes normal no yes
2 great no yes normal no yes
3 mediocre yes no high no no
4 great yes yes normal yes yes
 most similar: number 2 (1 mismatch, 4 match)  yes
Second most similar example: number 1 (2 mismatch, 3 match)  yes
Similarity metric: Number of matching attributes (k=2)
Yes
 Most similar: number 3 (1 mismatch, 4 match)  no
Second most similar example: number 1 (2 mismatch, 3 match)  yes
Yes/No
Carla P. Gomes
CS4700
Selecting the Number of Neighbors
Increase k:
– Makes KNN less sensitive to noise
Decrease k:
– Allows capturing finer structure of space
Pick k not too large, but not too small (depends on data)
Carla P. Gomes
CS4700
Curse-of-Dimensionality
Prediction accuracy can quickly degrade when number of attributes
grows.
– Irrelevant attributes easily “swamp” information from relevant
attributes
– When many irrelevant attributes, similarity/distance measure
becomes less reliable
Remedy
– Try to remove irrelevant attributes in pre-processing step
– Weight attributes differently
– Increase k (but not too much)
Carla P. Gomes
CS4700
Advantages and Disadvantages of KNN
Need distance/similarity measure and attributes that “match” target
function.
For large training sets,
 Must make a pass through the entire dataset for each classification.
This can be prohibitive for large data sets.
Prediction accuracy can quickly degrade when number of attributes
grows.
Simple to implement algorithm;
Requires little tuning;
Often performs quite weel!
(Try it first on a new learning problem).
Carla P. Gomes
CS4700
Example
Query -> x = (Maths=6,CS=8) , k=3
Euclidean Distance =
sqrt ((|x01 – xa1|2 + |x02- xa2|2))
Maths CS Result
1 4 3 F
2 6 7 P
3 7 8 P
4 5 5 F
5 8 8 P

Weitere Àhnliche Inhalte

Ähnlich wie AI-Lec16 KNN - Classification.pptx

Ähnlich wie AI-Lec16 KNN - Classification.pptx (20)

K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)K-Nearest Neighbor(KNN)
K-Nearest Neighbor(KNN)
 
Fa18_P2.pptx
Fa18_P2.pptxFa18_P2.pptx
Fa18_P2.pptx
 
KNN
KNNKNN
KNN
 
KNN presentation.pdf
KNN presentation.pdfKNN presentation.pdf
KNN presentation.pdf
 
09_dm1_knn_2022_23.pdf
09_dm1_knn_2022_23.pdf09_dm1_knn_2022_23.pdf
09_dm1_knn_2022_23.pdf
 
The rsa algorithm JooSeok Song
The rsa algorithm JooSeok SongThe rsa algorithm JooSeok Song
The rsa algorithm JooSeok Song
 
K nearest neighbours
K nearest neighboursK nearest neighbours
K nearest neighbours
 
Knn Algorithm presentation
Knn Algorithm presentationKnn Algorithm presentation
Knn Algorithm presentation
 
Clustering
ClusteringClustering
Clustering
 
DBSCAN
DBSCANDBSCAN
DBSCAN
 
K- Nearest Neighbor Approach
K- Nearest Neighbor ApproachK- Nearest Neighbor Approach
K- Nearest Neighbor Approach
 
DBSCAN (2014_11_25 06_21_12 UTC)
DBSCAN (2014_11_25 06_21_12 UTC)DBSCAN (2014_11_25 06_21_12 UTC)
DBSCAN (2014_11_25 06_21_12 UTC)
 
Data analysis of weather forecasting
Data analysis of weather forecastingData analysis of weather forecasting
Data analysis of weather forecasting
 
unit 4 nearest neighbor.ppt
unit 4 nearest neighbor.pptunit 4 nearest neighbor.ppt
unit 4 nearest neighbor.ppt
 
The rsa algorithm
The rsa algorithmThe rsa algorithm
The rsa algorithm
 
The rsa algorithm
The rsa algorithmThe rsa algorithm
The rsa algorithm
 
MLHEP Lectures - day 1, basic track
MLHEP Lectures - day 1, basic trackMLHEP Lectures - day 1, basic track
MLHEP Lectures - day 1, basic track
 
K nearest neighbor algorithm
  K nearest neighbor algorithm  K nearest neighbor algorithm
K nearest neighbor algorithm
 
Introduction to data mining and machine learning
Introduction to data mining and machine learningIntroduction to data mining and machine learning
Introduction to data mining and machine learning
 
KNN
KNNKNN
KNN
 

Mehr von HirazNor

MobileAdHocRoutingProtocols.pptx
MobileAdHocRoutingProtocols.pptxMobileAdHocRoutingProtocols.pptx
MobileAdHocRoutingProtocols.pptxHirazNor
 
csevpnppt-170905123948 (1).pdf
csevpnppt-170905123948 (1).pdfcsevpnppt-170905123948 (1).pdf
csevpnppt-170905123948 (1).pdfHirazNor
 
Folk Media Lec 14.pptx
Folk Media Lec 14.pptxFolk Media Lec 14.pptx
Folk Media Lec 14.pptxHirazNor
 
AI-Lec2-Agents.pptx
AI-Lec2-Agents.pptxAI-Lec2-Agents.pptx
AI-Lec2-Agents.pptxHirazNor
 
AI-Lec5-Searches in AI.pptx
AI-Lec5-Searches in AI.pptxAI-Lec5-Searches in AI.pptx
AI-Lec5-Searches in AI.pptxHirazNor
 
C3_Gossip_B_CSRAfinal-.pptx
C3_Gossip_B_CSRAfinal-.pptxC3_Gossip_B_CSRAfinal-.pptx
C3_Gossip_B_CSRAfinal-.pptxHirazNor
 
IAP presentation-1.pptx
IAP presentation-1.pptxIAP presentation-1.pptx
IAP presentation-1.pptxHirazNor
 
IAP PPT-2.pptx
IAP PPT-2.pptxIAP PPT-2.pptx
IAP PPT-2.pptxHirazNor
 
Lecture 15.ppt
Lecture 15.pptLecture 15.ppt
Lecture 15.pptHirazNor
 
Routing Protocols.pptx
Routing Protocols.pptxRouting Protocols.pptx
Routing Protocols.pptxHirazNor
 
W13-L1&2.pptx
W13-L1&2.pptxW13-L1&2.pptx
W13-L1&2.pptxHirazNor
 
W09-L01.pptx
W09-L01.pptxW09-L01.pptx
W09-L01.pptxHirazNor
 
hafsa 24.pptx
hafsa 24.pptxhafsa 24.pptx
hafsa 24.pptxHirazNor
 
IAP PPT-1.pptx
IAP PPT-1.pptxIAP PPT-1.pptx
IAP PPT-1.pptxHirazNor
 
Group#04-PPT-IA&P.pptx
Group#04-PPT-IA&P.pptxGroup#04-PPT-IA&P.pptx
Group#04-PPT-IA&P.pptxHirazNor
 

Mehr von HirazNor (15)

MobileAdHocRoutingProtocols.pptx
MobileAdHocRoutingProtocols.pptxMobileAdHocRoutingProtocols.pptx
MobileAdHocRoutingProtocols.pptx
 
csevpnppt-170905123948 (1).pdf
csevpnppt-170905123948 (1).pdfcsevpnppt-170905123948 (1).pdf
csevpnppt-170905123948 (1).pdf
 
Folk Media Lec 14.pptx
Folk Media Lec 14.pptxFolk Media Lec 14.pptx
Folk Media Lec 14.pptx
 
AI-Lec2-Agents.pptx
AI-Lec2-Agents.pptxAI-Lec2-Agents.pptx
AI-Lec2-Agents.pptx
 
AI-Lec5-Searches in AI.pptx
AI-Lec5-Searches in AI.pptxAI-Lec5-Searches in AI.pptx
AI-Lec5-Searches in AI.pptx
 
C3_Gossip_B_CSRAfinal-.pptx
C3_Gossip_B_CSRAfinal-.pptxC3_Gossip_B_CSRAfinal-.pptx
C3_Gossip_B_CSRAfinal-.pptx
 
IAP presentation-1.pptx
IAP presentation-1.pptxIAP presentation-1.pptx
IAP presentation-1.pptx
 
IAP PPT-2.pptx
IAP PPT-2.pptxIAP PPT-2.pptx
IAP PPT-2.pptx
 
Lecture 15.ppt
Lecture 15.pptLecture 15.ppt
Lecture 15.ppt
 
Routing Protocols.pptx
Routing Protocols.pptxRouting Protocols.pptx
Routing Protocols.pptx
 
W13-L1&2.pptx
W13-L1&2.pptxW13-L1&2.pptx
W13-L1&2.pptx
 
W09-L01.pptx
W09-L01.pptxW09-L01.pptx
W09-L01.pptx
 
hafsa 24.pptx
hafsa 24.pptxhafsa 24.pptx
hafsa 24.pptx
 
IAP PPT-1.pptx
IAP PPT-1.pptxIAP PPT-1.pptx
IAP PPT-1.pptx
 
Group#04-PPT-IA&P.pptx
Group#04-PPT-IA&P.pptxGroup#04-PPT-IA&P.pptx
Group#04-PPT-IA&P.pptx
 

KĂŒrzlich hochgeladen

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 

KĂŒrzlich hochgeladen (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 

AI-Lec16 KNN - Classification.pptx

  • 1. Carla P. Gomes CS4700 K Nearest Neighbor - CLASSIFICATION
  • 2. Carla P. Gomes CS4700 1-Nearest Neighbor One of the simplest of all machine learning classifiers Simple idea: label a new point the same as the closest known point Label it red.
  • 3. Carla P. Gomes CS4700 1-NN’s Aspects as an Instance-Based Learner: A distance metric – Euclidean – When different units are used for each dimension  normalize each dimension by standard deviation – For discrete data, can use hamming distance  D(x1,x2) =number of features on which x1 and x2 differ – Others (e.g., normal, cosine) How many nearby neighbors to look at? – One How to fit with the local points? – Just predict the same output as the nearest neighbor. Adapted from “Instance-Based Learning” lecture slides by Andrew Moore, CMU.
  • 4. Carla P. Gomes CS4700 k – Nearest Neighbor Generalizes 1-NN to smooth away noise in the labels A new point is now assigned the most frequent label of its k nearest neighbors Label it red, when k = 3 Label it blue, when k = 7
  • 5. KNN Example New examples: – Example 1 (great, no, no, normal, no) – Example 2 (mediocre, yes, no, normal, no) Food (3) Chat (2) Fast (2) Price (3) Bar (2) BigTip 1 great yes yes normal no yes 2 great no yes normal no yes 3 mediocre yes no high no no 4 great yes yes normal yes yes  most similar: number 2 (1 mismatch, 4 match)  yes Second most similar example: number 1 (2 mismatch, 3 match)  yes Similarity metric: Number of matching attributes (k=2) Yes  Most similar: number 3 (1 mismatch, 4 match)  no Second most similar example: number 1 (2 mismatch, 3 match)  yes Yes/No
  • 6. Carla P. Gomes CS4700 Selecting the Number of Neighbors Increase k: – Makes KNN less sensitive to noise Decrease k: – Allows capturing finer structure of space Pick k not too large, but not too small (depends on data)
  • 7. Carla P. Gomes CS4700 Curse-of-Dimensionality Prediction accuracy can quickly degrade when number of attributes grows. – Irrelevant attributes easily “swamp” information from relevant attributes – When many irrelevant attributes, similarity/distance measure becomes less reliable Remedy – Try to remove irrelevant attributes in pre-processing step – Weight attributes differently – Increase k (but not too much)
  • 8. Carla P. Gomes CS4700 Advantages and Disadvantages of KNN Need distance/similarity measure and attributes that “match” target function. For large training sets,  Must make a pass through the entire dataset for each classification. This can be prohibitive for large data sets. Prediction accuracy can quickly degrade when number of attributes grows. Simple to implement algorithm; Requires little tuning; Often performs quite weel! (Try it first on a new learning problem).
  • 9. Carla P. Gomes CS4700 Example Query -> x = (Maths=6,CS=8) , k=3 Euclidean Distance = sqrt ((|x01 – xa1|2 + |x02- xa2|2)) Maths CS Result 1 4 3 F 2 6 7 P 3 7 8 P 4 5 5 F 5 8 8 P