Theoretical basis of k-nearest neighbor algorithm

This paper introduces the K nearest neighbor algorithm from the aspects of theoretical basis, handwritten numeral recognition algorithm and handwritten numeral recognition examples.

The essence of K nearest neighbor algorithm is to classify the specified objects according to the known eigenvalues.

For example, if you see a father and son, you can know which is the father and which is the son by judging their age. This is divided by the eigenvalue of the age attribute.

The above example is the simplest classification based on a single feature dimension. In the actual scene, the situation may be more complicated, with multiple feature dimensions.

For example, classify a sports video and judge whether the video is a table tennis match or a football match.

In order to determine the classification, features need to be defined. Two characteristics are defined here, one is the athlete's "waving" action and the other is the athlete's "kicking" action. Of course, we can't classify the video as a "table tennis match" as soon as we see the action of "waving", because we know that some football players are used to communicating with their teammates by waving in the playground. Similarly, we can't classify the video as a "football match" as soon as we see playing football, because some table tennis players will express their feelings by playing football.

We counted the times of "waving" and "kicking" in the video in a certain period of time and found the following rules:

● In the video of table tennis match, the number of "waving" is far more than the number of "kicking".

● In the video of football match, the number of "kicking" is far more than the number of "swinging".

According to the analysis of a group of videos, the data shown in table 20- 1 are obtained.

To facilitate observation, the above data are plotted as a scatter plot, as shown in Figure 20- 1.

As can be seen from Figure 20- 1, the data points show aggregation characteristics:

● The data points in the video of table tennis match are gathered in the area of X-axis coordinates.

● The data points in the football match video are gathered in the area of Y-axis coordinates.

There will be a video test at this time. According to statistics, there are 2000 "waving" actions and 100 "kicking" actions. If its position is marked in Figure 20- 1, it can be found that the nearest neighbor of the video test is the video of table tennis match, so it can be judged that the video is the video of table tennis match.

The above example is an extreme example, either black or white, but there are often many parameters in the actual classification data, so it is not so simple to judge. Therefore, in order to improve the reliability of the algorithm, K adjacent points will be taken in the implementation, and which category these K points belong to, and then the points to be identified will be divided into which category. For the convenience of judgment, the value of k is usually odd, which is the same as the reason why board members are usually arranged as odd in order to get a clear voting result.

For example, a well-known twin artist A and B are known to look alike. If it is necessary to judge whether the character on the picture T is artist A or artist B, then the specific steps to realize it by using the K nearest neighbor algorithm are as follows:

The above is the basic idea of K nearest neighbor algorithm.