The MATLAB realization of KNN

Source: Internet
Author: User

For reference (http://blog.sina.com.cn/s/blog_8bdd25f80101d93o.html), the last few lines have been modified to

%k neighbor, take k=7, cross-validation method how to determine the value of K???? % Select 7 minimum values, use the simplest comparison method to test

M=[];

For i=1:210

M=[m Distance (x,y,xnew (i,1), Xnew (i,2))];

End

Mnew=sort (M);

For i=1:7 Array (i) =find (m==mnew (i)); End

Plot (Xnew (array,1), Xnew (array,2), ' R ')

The block point is the test point, and the corresponding 7 nearest neighbors are connected by a red line. Therefore, in order to get the classification result of the test point, we need to record the index of the nearest neighbor, and then we can find the category of nearest neighbor. The method is to first calculate the distance from the test point to all points, and then the order from small to large, you can get several minimum distances, and then use the Find function to find the position of these values in all distance data, that is, the corresponding nearest neighbor subscript. Further, the nearest neighbor category can be obtained. After simple expansion, we can get the classification success rate, evaluate the influence of the nearest neighbor number, and get more suitable parameters. Of course, MATLAB provides the command and code for the nearest neighbor category, which can be called directly.

The MATLAB realization of KNN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.