+ All Categories
Home > Documents > Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest...

Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest...

Date post: 10-Sep-2020
Category:
Upload: others
View: 12 times
Download: 1 times
Share this document with a friend
13
Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity College Dublin ADAPT Research Centre The ADAPT Centre is funded under the SFI Research Centres Programme (Grant 13/RC/2106) and is co-funded under the European Regional Development Fund.
Transcript
Page 1: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

Example KNN: The Nearest Neighbor Algorithm

Dr. Kevin Koidl School of Computer Science and Statistic Trinity College DublinADAPT Research Centre

The ADAPT Centre is funded under the SFI Research Centres Programme (Grant 13/RC/2106) and is co-funded under the European Regional Development Fund.

Page 2: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieKNN Similarity based learning

• Needed: A feature space representation of the instance in the dataset and a measure of similarity between instances.

• Each instance in the training set is stored in a memory.

• Initial storing is standard however once all training examples are stored a second run needs to determine the distance between the different instances.

• The prediction is based on finding out what class the nearest instance belongs to.

Page 3: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieMain Algorithm

Require: a set of training instancesRequire: a query instance

1. Iterate across the instances in memory to find the nearest neighbour – this is the instance with the shortest distance across the feature space to the query instance.

2. Make a prediction for the query instance that is equal to the value of the target feature of the nearest neighbour.

Page 4: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieExample

From:  Fundamental  of  Machine  Learning  John  Kelleher

Page 5: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieQuery instance

Page 6: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieDistance Results

Page 7: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieExplanation

• KNN creates local models (or neighbourhoods) across the feature space with each space defined by a subset of the training data.

• Implicitly a ‘global’ decision space is created with boundaries between the training data.

Page 8: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieUpdating the decision space

• One advantage of KNN is that updating the decision space is easy. • KNN is a nearest neighbour algorithm that creates an implicit global

classification model by aggregating local models, or neighborhoods.

Page 9: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieHandling noisy data

• Outliers can create individual spaces which belong to a class but are separated. This mostly relates to noise in the data.

• The solution is dilution of dependency of the algorithm on individual (possibly noisy) instances.

Page 10: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieVoting

• Once we have obtained the K-Nearest-Neighbours using the distance function, it is time for the neighbors to vote in order to predict it’s class class.

• Majority voting assumes that all votes are equal. • For each class l ∈ L we count the amount of k-neighbours that have that

class. • We return the class with the most votes.

Page 11: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieVoting more mathematically

• Modification of the algorithm to return the majority vote within the set of k nearest neighbours to a query q.

• Mk(q) is the prediction of the model M for query q given the parameter of the model k.

• Levels(l) is the set of of levels (classes) in the domain of the target feature and l is an element of this set.

• i iterates over the distance di in increasing distance from the query q• ti is the value of the target feature for distance di

• Delta(ti , l) is the Knoecker Delta function which takes two parameters and returns 1 if they are equal or 0 if not.

Page 12: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieVisualisation

Page 13: Example KNN: The Nearest Neighbor Algorithmkoidlk/cs4062/ExampleKNN.pdf · Example KNN: The Nearest Neighbor Algorithm Dr. Kevin Koidl School of Computer Science and Statistic Trinity

www.adaptcentre.ieOther situation

• Weighted k nearest neighbour approach • K high for example results in including instances that are very far away

from the query instance. • Counterbalance is provided by using distance weighted k nearest neighbour

approach.• Votes close to the query get a higher relevance.• Either by taking the reciprocal (inverse) of the squared distance

or The distance weighted k nearest neighbour approach.


Recommended