mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
* Updated KNN and README * Update README.md * new * new * updated tests * updated knn coverage
KNN Algorithm
KNN stands for K Nearest Neighbors. KNN is a supervised Machine Learning algorithm. It's a classification algorithm, determining the class of a sample vector using a sample data.
The idea is to calculate the similarity between two data points on the basis of a distance metric. Euclidean distance is used mostly for this task. The algorithm is as follows -
- Check for errors like invalid data/labels.
- Calculate the euclidean distance of all the data points in training data with the classification point
- Sort the distances of points along with their classes in ascending order
- Take the initial "K" classes and find the mode to get the most similar class
- Report the most similar class
Here is a visualization for better understanding -
Here, as we can see, the classification of unknown points will be judged by their proximity to other points.
It is important to note that "K" is preferred to have odd values in order to break ties. Usually "K" is taken as 3 or 5.
