Unit3-KNN-Examples
Unit3-KNN-Examples
The K-NN algorithm compares a new data entry to the values in a given data set (with different
classes or categories).
Based on its closeness or similarities in a given range (K) of neighbors, the algorithm assigns the
new data to a class or category in the data set (training data).
Let's break that down into steps:
Step #1 - Assign a value to K.
Step #2 - Calculate the distance between the new data entry and all other existing data entries
(you'll learn how to do this shortly). Arrange them in ascending order.
Step #3 - Find the K nearest neighbors to the new entry based on the calculated distances.
Step #4 - Assign the new data entry to the majority class in the nearest neighbors.
Don't worry if the steps above seem confusing at the moment. The examples in the sections that
follow will help you understand better.
The graph above represents a data set consisting of two classes — red and blue.
A new data entry has been introduced to the data set. This is represented by the green point in the
graph above.
We'll then assign a value to K which denotes the number of neighbors to consider before classifying
the new data entry. Let's assume the value of K is 3.
Since the value of K is 3, the algorithm will only consider the 3 nearest neighbors to the green point
(new entry). This is represented in the graph above.
Out of the 3 nearest neighbors in the diagram above, the majority class is red so the new entry will
be assigned to that class.
Distance #1
For the first row, d1: