Professional Documents
Culture Documents
Example KNN: The Type of Oil X1 Duration of Consumption X2
Example KNN: The Type of Oil X1 Duration of Consumption X2
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.
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
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
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
7 7 √ =4 4 NO BAD
7 4 √ =5 6 NO BAD
3 4 √ =3 2 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