KNN (K-Nearest Neighbors) is one of the simplest classification algorithms.
You intend to find out the class of the blue star (BS) . BS can either be RC or GS and nothing else. The “K” is KNN algorithm is the nearest neighbors we wish to take vote from. Let’s say K = 3. Hence, we will now make a circle with BS as center just as big as to enclose only three data points on the plane. Refer to following diagram for more details:
The three closest points to BS is all RC. Hence, with good confidence level we can say that the BS should belong to the class RC. Here, the choice became very obvious as all three votes from the closest neighbor went to RC.
![](https://i-blog.csdnimg.cn/blog_migrate/823cfbf8638e639628498c708e1847b6.png)
Those 3 distance functions are only valid for continuous data, if you meet the categorical variables, you have to use Hamming Distance:
The choice of the parameter K is very crucial in this algorithm. Too small K value will overfit the data, too high K value will underfit the data.
Draw the above graph, you will able to tell which K value is best for your model.