Build a classifier based on relative distance measurements
Among them, the most representative is the KNN algorithm
Reference: "Principles of Statistical Learning Li Hang"
1. Introduction to KNN algorithm
among them is the feature vector of the instance, Is the category of the instance,
Output: exampleThe class to which belongs .
- According to the given distance vector, in the training setFound in and nearestkpoints, covering this pointsThe neighborhood of is written as ;
- in is determined according to the classification decision rule category ;
In the k-nearest neighbor method, when the training set, distance measurement (such as Euclidean distance>, k value and classification decision rules (such as majority voting) are determined, for any new input instance, the class it belongs to is uniquely determined. It is equivalent to dividing the feature space into some subspaces based on the above-mentioned elements, and determining the class to which each point in the space belongs. This fact can be clearly seen from the nearest neighbor algorithm.
In the feature space, for each training instance point , all points closer to this point than other points form an area, called unit (ce11). Each training instance point has a unit, and the units of all training instance points constitute a division of the feature space. Examples of Nearest Neighborsthe type as the class label of all points in its cell. In this way, the category of the instance point of each unit is determined.
For a feature space, the distance between two instance points is a reflection of the similarity of the two instance points.
Euclidean distance is generally used, of course there areDistance and Minkowski distance.
Choice of k value
The choice of k value will have a significant impact on the results of the k-nearest neighbor method.
If you choose a smaller value of k, it is equivalent to predicting with training examples in a smaller neighborhood, the approximation error of "learning" will be reduced, and only training examples that are closer to the input instance (similar) will be used for prediction. The prediction results play a role. But the disadvantage is that the estimation error of "learning" will increase, and the prediction result will be very sensitive to the neighboring instance points. If the neighboring instance points happen to be noise, the prediction will be wrong. In other words, the decrease of the k value means that the overall model becomes complicated, and it is prone to overfitting.
If you choose a larger value of k, it is equivalent to predicting with the training examples in the larger neighborhood. The advantage is that it can reduce the estimation error of learning, but the disadvantage is that the approximate error of learning will increase. At this time, the <unsimilar> training example far away from the input example will also play a role in the prediction. The increase of the k value to make the prediction error means that the overall model becomes simple.
In application, the value of k generally takes a relatively small value. The cross-validation method is usually used to select the optimal k value.
KNN classification rule is the majority rule
The loss function is 0-1 loss function, and the classification rules are:
Then for the set of k adjacent training instance points , the error (misclassification) rate is:
To minimize the misclassification rate, it is required that the probability of correctness be maximized, so the rule that the minority obeys the majority can just meet the empirical risk minimization.
When implementing KNN, the main consideration is how to perform fast K-nearest neighbor search on training data. If the dimensionality of the feature space is large or the training data capacity is large, then data storage is a big problem. The simplest way to implement KNN is linear scanning. At this time, when the data set is large, the calculation is very time-consuming. In order to improve the efficiency of this search, a special structure is used to store training data-kd tree. The kd tree is a tree data structure that stores points in a k-dimensional space for quick search. In essence, the kd tree is a binary tree, which represents a division of the k-dimensional space.