introduce

KNN algorithm is called K-nearest Neighbor in Chinese and K-nearest Neighbor algorithm in Chinese. KNN is a classification algorithm.

The main algorithm process is as follows:

1. Given a training set data, and each training set data has been classified. 2. Set a test data A, calculate the Euclidean distance from all the data in the training set to A, and sort it. 3. Take k nearest data. 4. Select the category with the most variety from the k samples. 5. The classification result is the classification of the final test data A.Copy the code

Creating a training set

Define the KNN function

1. Calculate the distance between a new sample and all the data in the datasetCopy the code

2. Sort by sizeCopy the code

3. Select K samples with the smallest distanceCopy the code

4. Select the category with the most variety from the k samplesCopy the code

conclusion

github.com/fuqiuai