A kNN Numerical Example (Hand Computation)


Here is step by step on how to compute K-nearest neighbors kNN algorithm.
  1. Determine parameter K=number of nearest neighbors.
  2. Calculate the distance between the query-instance and all the training samples.
  3. Sort the distance and determine nearest neighbors based on the Kth minimum distance.
  4. Gather the category Y of the nearest neighbors.
  5. Use simple majority of the category of nearest neighbors as the prediction value of the query instance.
We will use again this simple example to calculate kNN by hand computation:

We have data from the questionnaires survey and objective testing with two attributes (acid durability and strength) to classify whether a special paper tissue is good or not. Here are four training samples:
X1=Acid Durability (seconds) X2=Strength (kg/m2) Y=Classification
7 7 Bad
7 4 Bad
3 4 Good
1 4 Good

Now the factory produces a new paper tissue that passed laboratory test with X1=3 and X2=7. Without another expensive survey, can we guess what the classification of this new tissue is?
  1. Determine parameter K=number of nearest neighbors.
    Suppose use K=3.

  2. Calculate the distance between the query-instance and all the training samples.

    Coordinate of query instance is (3,7), instead of calculating the distance we compute square distance which is faster to calculate (without square root).
    X1=Acid Durability (seconds) X2=Strength (kg/m2) Square distance to query instance (3,7)
    7 7 (7-3)2+(7-7)2=16
    7 4 (7-3)2+(4-7)2=25
    3 4 (3-3)2+(4-7)2=9
    1 4 (1-3)2+(4-7)2=13