You are on page 1of 3

1

Example KNN

We suppose that we have a shop that sells car oils and we conducted a survey about their quality by
asking about the effectiveness of these oils, and let them be the type of oil. Let us express it in the
variable X1, and the duration of consumption is in kilometers and we express it with the variable X2, so
we collect this information.

The type of oil X1 Duration of consumption X2 Classification Y


5 7 BAD

7 7 BAD

7 4 BAD

3 4 GOOD

1 4 GOOD

2 3 GOOD

Because we have a new product in the shop for a new oil that has two characteristics: X1 = 3, X2 = 7, can
we predict the classification of this oil? Let's follow the steps of the algorithm:

1- We define a value for the variable that will express the number of neighboring k, and let the value of k
=3

2- We calculate the value of the distance between the new product and the examples in the dataset, in our
example we will rely on Euclidean Distance where the distance between two points p, q is calculated
from the equation:
2

The type of oil X1 Duration of consumption Distance


X2
5 7 √ =2
7 7 √ =4
7 4 √ =5
3 4 √ =3
1 4 √ = 3.6
2 3 √ = 4.123

3- We arrange the examples to get the neighbors, depending on the lowest distance calculated in the
previous step, and take from them the number of adjacent k, where k here is only 3 neighboring

The type of oil Duration of Distance Sort by the Will he join


X1 consumption smallest distance the
X2 neighbors?
5 7 √ =2 1 YES

7 7 √ =4 4 NO

7 4 √ =5 6 NO

3 4 √ =3 2 YES

1 4 √ = 3.6 3 YES

2 3 √ = 4.123 5 NO

4- We define the classification value for the neighboring, as in the last column in the following table

The type Duration of Distance Sort by the Will he join Rating


of oil X1 consumption smallest the value for
X2 distance neighbors? neighboring
Y?
5 7 √ =2 1 YES BAD

7 7 √ =4 4 NO BAD

7 4 √ =5 6 NO BAD

3 4 √ =3 2 YES GOOD

1 4 √ = 3.6 3 YES GOOD

2 3 √ = 4.123 5 NO GOOD
3

5- The majority of the neighboring class is GOOD, where we have three examples, two of them are
GOOD and one is only BAD, so the expected class for this new oil is GOOD

You might also like