How KNN algorithm works
KNN or K Nearest Neighbor is a non parametric or lazy learning algorithm which works while testing the data. It memorizes the unseen test data by comparing the observation with nearest training observations which are also time consuming while testing each data point.
The accuracy of this algorithm depends on the value of k set to compare the data points to classify the test data.
KNN can be explained with the prediction of voter based on geo location of nearest neighbor. Let us take the value of k=5 and see how it performs
In this algorithm, KNN predicts that the party will vote for party 2 because 3 votes are selected by the algorithm within the neighbor. Only 1 vote is selected by Party 1 and Party 3. In this way, the vote will be predicted for Party 2 and this is how KNN works.