----of machine learning--KNN

Source: Internet
Author: User

************** written in the front **************

This article is a summary of the learning process and some personal ideas, only recorded, continuous update ...

thin only sparse, I implore you if you find any problems please be sure to exchange a positive message Oh ~ ~

Personal opinion: The supervision study in machine learning can be divided into regression and fitting, more methods, it is necessary to learn from the basic simple step by step, although not necessarily for each formula are deduced, but at least to know the principle, because the next more difficult algorithm is based on the basis of the algorithm to increase and upgrade. The patchwork study still feels not solid. Of course I'm still a little white.

Note: From the Book Machine learning Combat

The basic principle of KNN:

K-Nearest Neighbor algorithm, to achieve the classification function, where k is the choice and the target K distance nearest point to determine what kind of target. The distance uses the European distance.

Note: A. Use a dictionary; B. Using iterators

The following code from the machine learning related to the section, this is only to write their own implementation

#-*-coding:utf-8-*-"" "aiming to achive the KNN algorithm" "" Import NumPy as NP import operator de F knnclassfy (Test_x,train_x,train_y,k): M=train_x.shape[0] Diffmat=np.tile (test_x, (m,1))-train_x SqDiffMat=dif FMAT**2 # European Distance Sqdistance=sqdiffmat.sum (Axis=1) # Row Add distance=sqdistance**0.5 sorteddistanceindex=distance. 
        Argsort () # from small to large sort classcount={} for I in range (0,k): # Select before K Votelabel=train_y[sorteddistanceindex[i]] Classcount[votelabel]=classcount.get (votelabel,0) +1 sortedclasscount=sorted (Classcount.iteritems (), Key=operat
    Or.itemgetter (1), reverse=true) # from large to small sort return sortedclasscount[0][0] if __name__== ' __main__ ': # the DataSet Train_x=np.array ([[[0,0],[0.1,0.1],[1.0,1.0],[1.1,1.4]]]) train_y=np.array ([' A ', ' a ', ' B ', ' B ']) test_x=np.array ([[0]. 2,0.1]] test_y=knnclassfy (test_x,train_x,train_y,3) print ("The classsfied result of a KNN is%s"% (test_y)) 


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.