Algorithm 1-KNN algorithm and its matlab implementation
Algorithm 1-KNN algorithm and its matlab implementation is what, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
I. Overview of algorithms
1. KNN algorithm is also called k-nearest neighbor classification (k-nearest neighbor classification) algorithm. The kNN algorithm finds the k records closest to the new data from the training set, and then determines the category of the new data according to their main classification. The algorithm involves three main factors: training set, distance or similar measure, and the size of k.
The calculation steps are as follows:
1) calculate the distance: given the test object, calculate its distance from each object in the training set
2) find neighbors: circle the nearest k training objects as the nearest neighbors of the test subjects.
3) Classification: the test objects are classified according to the main categories of the k nearest neighbors.
2. Measurement of distance or similarity
What is the appropriate distance measure? The closer the distance should mean the more likely the two points belong to the same category. Distance measurement includes Euclidean distance, angle cosine and so on.
3. Determination of category
It is decided by vote that the minority is subordinate to the majority, and which category of points in the nearest neighbor is the most divided into this category.
Weighted voting method: the nearest neighbor's vote is weighted according to the distance, and the closer the distance is, the greater the weight is (the weight is the reciprocal of the square of the distance).
Clear;clc;K=3;trainData = [1.0cr 2.0; 1.2 recital 0.1; 0.1 recorder 1.4; 0.3 recorder 3.5]; trainClass = [1 meme 1 rectory 2 recorder 2]; testData = [0.5 Person2.3]
[NMagol M] = size (trainData);% calculate the Euclidean distance between the training data set and the test data distdist=zeros (NMague 1); for iQuery 1 dist N dist (trainData (iscore:)-testData); end% ranks dist from small to large [YMagol I] = sort (dist,1);% trainClass=trainClass (I) the category corresponding to the training data and the ranking result of the training data. % determine the occurrence frequency of the category in which the first K points are located: classNum=length (unique (trainClass));% take the number of single-valued elements in the set labels=zeros (1 classNum); for ionomer 1trainClass K j=trainClass (I); labels (j) = labels (j) + 1trader% return the category with the highest frequency in the previous K points as the prediction classification of the test data [~, idx] = max (labels); fprintf ('the test data belongs to class% d\ nThe category IDX)
%% f method II simpler labels=trainClass;idx=mode (labels (1mode K));% mode function to find the mode fprintf ('the test data belongs to class% d', idx); after reading the above, have you mastered the algorithm 1-KNN algorithm and its matlab implementation method? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!