knn algorithm finding nearest neighbors introduction -nearest neighbors (knnalgorithm is type of supervised ml algorithm which can be used for both classification as well as regression predictive problems howeverit is mainly used for classification predictive problems in industry the following two properties would define knn welllazy learning algorithmknn is lazy learning algorithm because it does not have specialized training phase and uses all the data for training while classification non-parametric learning algorithmknn is also non-parametric learning algorithm because it doesn' assume anything about the underlying data working of knn algorithm -nearest neighbors (knnalgorithm uses 'feature similarityto predict the values of new datapoints which further means that the new data point will be assigned value based on how closely it matches the points in the training set we can understand its working with the help of following stepsstep for implementing any algorithmwe need dataset so during the first step of knnwe must load the training as well as test data step nextwe need to choose the value of the nearest data points can be any integer step for each point in the test data do the following calculate the distance between test data and each row of training data with the help of any of the method namelyeuclideanmanhattan or hamming distance the most commonly used method to calculate distance is euclidean nowbased on the distance valuesort them in ascending order nextit will choose the top rows from the sorted array nowit will assign class to the test point based on most frequent class of these rows step end example the following is an example to understand the concept of and working of knn algorithmsuppose we have dataset which can be plotted as follows