29
Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction Introduction to kNN Classification and CNN Data Reduction Oliver Sutton February, 2012 1 / 29

Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Embed Size (px)

Citation preview

Page 1: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Introduction to kNN Classification and CNN DataReduction

Oliver Sutton

February, 2012

1 / 29

Page 2: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

1 The Classification ProblemExamplesThe Problem

2 The k Nearest Neighbours AlgorithmThe Nearest Neighbours ApproachThe IdeaThe AlgorithmApplet ExampleProblems With the Algorithm

3 Condensed Nearest Neighbour Data ReductionThe IdeaThe AlgorithmExample Using the Applet

4 Summary

2 / 29

Page 3: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

The Classification Problem: Example 1

I Suppose we have a database of the characteristics of lots ofdifferent people, and their credit rating

I e.g. how much they earn, whether they own their house, howold they are, etc.

I Want to be able to use this database to give a new person acredit rating

I Intuitively, we want to give similar credit ratings to similarpeople

3 / 29

Page 4: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

The Classification Problem: Example 2

I We have a database of characteristic measurements from lotsof different flowers, along with the type of flower

I e.g. their height, their colour, their stamen size, etc.

I Want to be able to use this database to work out what type anew flower is, based on its measurements

I Again, we want to classify it with the type of flower it is mostsimilar to

4 / 29

Page 5: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

The Classification Problem

I In general, we start with a database of objects who’sclassification we already know

I Known as the training database, since it trains us to knowwhat the different types of things look like

I We take a new sample, and want to know what classificationit should be

I The classification is based on which items of the trainingdatabase the new sample is similar to

The Problem

What makes two items count as similar, and how do we measuresimilarity?

5 / 29

Page 6: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

One way of solving these problems is with the nearest neighboursapproach...

6 / 29

Page 7: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

The Nearest Neighbours Idea: Measuring Similarity

I The nearest neighbours approach involves interpreting eachentry in the database as a point in space

I Each of the characteristics is a different dimension, and theentry’s value for that characteristic is its coordinate in thatdimension

I Then, the similarity of two points is measured by the distancebetween them

I Different metrics can be used, although in general they givedifferent results

7 / 29

Page 8: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

The Nearest Neighbours Idea: Sufficiently Similar

I The nearest neighbours approach then classifies the newsample by looking at the classifications of those closest to it

I In the k Nearest Neighbours (kNN), this is achieved byselecting the k entries which are closest to the new point

I An alternative method might be to use all those points withina certain range of the new point

I The most common classification of these points is then givento the new point

8 / 29

Page 9: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

The k Nearest Neighbours Algorithm

The algorithm (as described in [1] and [2]) can be summarised as:

1. A positive integer k is specified, along with a new sample

2. We select the k entries in our database which are closest tothe new sample

3. We find the most common classification of these entries

4. This is the classification we give to the new sample

9 / 29

Page 10: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Example Using the Applet

I First we set up the applet [1] like this:

I i.e. with a cluster of 40 points of different colours in oppositecorners, and 20 points of random noise from each colour

10 / 29

Page 11: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Example Using the Applet

I These blue and pink points will be our training dataset, sincewe have specified their characteristics (x and y coordinates),and their classification (colour)

I We then select the “Handle Test” tab at the top, and clickanywhere in the screen to simulate trying to classify a newpoint

I For the value of k specified in the parameters tab, the appletwill find and highlight the k nearest data points to the testpoint

I The most common colour is then shown in the big circlewhere the test point was placed, as shown on the next slide

11 / 29

Page 12: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Example Using the Applet

12 / 29

Page 13: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Example Using the Applet

I Alternatively, we can use the applet to draw the map of thedata

I The colour at each point indicates the result of classifying atest point at that location

13 / 29

Page 14: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Cross Validation

I In order to investigate how good our initial training set is, wecan use a procedure called cross validation

I Essentially, this involves running the kNN algorithm on eachof the points in the training set in order to determine whetherthey would be recognised as the correct type

I Clearly, this can very easily be corrupted by introducingrandom noise points into the training set

I To demonstrate this, the applet was used with increasingnumbers of random data points in the training set, and thenumber of cross validation errors counted, as shown in thegraph on the next slide

14 / 29

Page 15: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Cross Validation

Average'Number'of'Errors

0

10

20

30

40

0 50 100 150 200

Average'Percentage'of'Errors

Percen

tage'of'e

rrors

Number'of'random'points'added

Clearly, there is a definite upward trend as the number of random points is increased, although the rate of increase seems to slow down when the number of random points gets large. Testing on a purely random dataset shows that one expects the percentage of points misclassified to tend to about half for 3 Nearest Neighbours, as would be expected (since the chance of any neighbour of a data point being from either data group is about equal). From Chart 1, it can be seen that the percentage of errors would be expected to eventually (as the number of data points tends to infinity) tend to about 50%. Taking both these observations together, it suggests that perhaps the percentage of errors follows a relationship similar to y = 1/(kx+0.02) + 50.

Condensed Nearest Neighbour data reduction was also implemented on 3 different data sets as described in Figure 1, to obtain the percentage of data points classified as outlier points (those which would not be recognised as the correct class if they were added to the data set later), prototype points (the minimum set of points required in

Average'Percentage'of'OutliersAverage'Percentage'of'Prototype'PointsAverage'Percentage'Of'Absorbed'Points

15 / 29

Page 16: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Cross Validation

I It can clearly be seen that including more random noise pointsin the training set increases the number of cross validationerrors

I As the number of random noise points becomes very large,the percentage of points which fail the cross validation tendsto 50%

I As would be expected for a completely random data set

16 / 29

Page 17: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Main Problems With the Algorithm

I Difficult to implement for some datatypesI e.g. colour, geographical location, favourite quotation, etc.I This is because it relies on being able to get a quantitative

result from comparing two itemsI Can often be got around by converting the data to a numerical

value, for example converting colour to an RGB value, orgeographical location to latitude and longitude

I Slow for large databasesI Since each new entry has to be compared to every other entryI Can be sped up using data reduction...

17 / 29

Page 18: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Condensed Nearest Neighbours Data Reduction

Data Reduction

The database is summarised by finding only the important data-points

I Datapoints in the training set are divided into three types (asdescribed in [1]):

1. Outliers: points which would not be recognised as the correcttype if added to the database later

2. Prototypes: the minimum set of points required in thetraining set for all the other non-outlier points to be correctlyrecognised

3. Absorbed points: points which are not outliers, and would becorrectly recognised based just on the set of prototype points

I New samples only need to now be compared with theprototype points, rather that the whole database

18 / 29

Page 19: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Condensed Nearest Neighbours Data Reduction

Data Reduction

The database is summarised by finding only the important data-points

I Datapoints in the training set are divided into three types (asdescribed in [1]):

1. Outliers: points which would not be recognised as the correcttype if added to the database later

2. Prototypes: the minimum set of points required in thetraining set for all the other non-outlier points to be correctlyrecognised

3. Absorbed points: points which are not outliers, and would becorrectly recognised based just on the set of prototype points

I New samples only need to now be compared with theprototype points, rather that the whole database

19 / 29

Page 20: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

CNN Data Reduction Algorithm

The algorithm (as described in [1]) is as follows:

1. Go through the training set, removing each point in turn, andchecking whether it is recognised as the correct class or not

I If it is, then put it back in the setI If not, then it is an outlier, and should not be put back

2. Make a new database, and add a random point.

3. Pick any point from the original set, and see if it is recognisedas the correct class based on the points in the new database,using kNN with k = 1

I If it is, then it is an absorbed point, and can be left out of thenew database

I If not, then it should be removed from the original set, andadded to the new database of prototypes

4. Proceed through the original set like this

5. Repeat steps 3 and 4 until no new prototypes are added

20 / 29

Page 21: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Effects of CNN Data Reduction

I After applying data reduction, we can classify new samples byusing the kNN algorithm against the set of prototypes

I Note that we now have to use k = 1, because of the way wefound the prototypes

I Classifying a new sample point is now faster, since we don’thave to compare it to so many other points

I Classifying new samples against the new reduced data set willsometimes lead to different results than comparing the newsample against the whole training set

I This is the trade-off we have to make, between speed andaccuracy

21 / 29

Page 22: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Example Using the Applet

I We set up a dataset as beforeI Under the parameters tab, we select ”Implement Reduction”,

and reduce the number of nearest neighbours for test pointsto 1

I Crosses mark outliers, squares mark prototypes, and circlesmark absorbed points

22 / 29

Page 23: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Example Using the Applet

I Introducing a test point as before, we can see that it still hasthe same result:

23 / 29

Page 24: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

How Noise Affects CNN

I Again using the applet, we can investigate how well CNNmanages to reduce the number of data points, in the presenceof different quantities of random noise

I To do this, we record what percentage of points were assignedeach classification on each of three different data sets as weincreased the number of random noise points

I The results are presented in the graph on the next slide

24 / 29

Page 25: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

How Noise Affects CNN

The effects of implementing CNN data reduction on increasinglynoisy data sets:

group is more likely to have its nearest neighbours from the other group, so making it more likely for it to be classified as an outlier. Similarly, the points which are not classified as outliers from each data group will be more disparate, so we will require more prototype points to represent our whole group. Then, since the absorbed points must be the points which are neither outliers nor prototypes, we can expect the percentage of these to decrease as more random points are added, which is represented by the green curve in Chart 2.

Question 5As with any algorithm, when using the K Nearest Neighbours algorithm it is important to balance the accuracy of the results against how expensive it is to run. For this reason, the effects of varying the parameters of the KNN algorithm were compared with the results produced. Clearly, as the value of k (the number of neighbours to consider) is increased the algorithm becomes more expensive to run. On the other hand, implementing data reduction means that any new point to be classified needs to be compared against fewer points from the data set, although it

Number'of'Random'Data'Points

Average'Percentage'of'Cross8Valida;on'Errors

Average'Percentage'of'Prototype'Points

Average'Percentage'of'Outlier'Points

Average'Percentage'of'Absorbed'Points

0 0 3 0 9820 17.5 16.16667 5.5 78.3333340 25 22 9.333333 6960 29.66666667 25.66667 8.833333 65.580 33.58333333 31.16667 10 59100 34.66666667 31.16667 11.16667 57.66667120 35.83333333 33.66667 10.5 55.66667140 37.25 32.33333 12.33333 55.16667160 38.5 37 11.5 52.16667

0

25

50

75

100

0 50 100 150 200

Condensed'NN'Data'Reduc;on

Percen

tage'of'P

oints'Assigne

d'Each'Status

Number'of'Random'Data'Points

Average'Percentage'of'OutliersAverage'Percentage'of'Prototype'PointsAverage'Percentage'Of'Absorbed'Points

25 / 29

Page 26: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

How Noise Affects CNN

I As can be seen, increasing the number of noise points had thefollowing effects:

1. Percentage of points classed as outliers increased dramatically2. Percentage of points classed as absorbed decreased3. Percentage of points classed as prototypes increased slightly

I These are all as would be expected

26 / 29

Page 27: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Problems With CNN

I The CNN algorithm can take a long time to run, particularlyon very large data sets, as described in [4] and [3].

I There are alternative methods for reducing dimensionality,which are described in [3]

I An alternative version of CNN which often performs fasterthan standard CNN is also given in [4]

I Using CNN can cause our system to give us differentclassifications than if we just used kNN on the raw data

27 / 29

Page 28: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

Summary

I We can use the kNN algorithm to classify new points, bygiving the same classification as those which the new point isclose to

I For large datasets, we can apply CNN data reduction to get asmaller training set to work with

I Tests after CNN will be obtained more quickly, and willnormally have the same result

28 / 29

Page 29: Introduction to kNN Classification and CNN Data Reduction · Introduction to kNN Classi cation and CNN Data Reduction Oliver Sutton ... although the rate of increase seems to slow

Outline The Classification Problem The k Nearest Neighbours Algorithm Condensed Nearest Neighbour Data Reduction

E. Mirkes, 2011KNN and Potential Energy (Applet),University of Leicester. Available at: http://www.math.le.

ac.uk/people/ag153/homepage/KNN/KNN3.html, 2011.

L. Kozma, 2008k Nearest Neighbours Algorithm.Helsinki University of Technology. Available at:http://www.lkozma.net/knn2.pdf, 2008.

N. Bhatia et al,Survey of Nearest Neighbor Techniques.International Journal of Computer Science and InformationSecurity, Vol. 8, No. 2, 2010.

F. Anguilli,Fast Condensed Nearest Neighbor Rule.Proceedings of the 22nd International Conference on MachineLearning, Bonn, Germany, 2005.

29 / 29