The K-nearest neighbor algorithm for Python learning

Source: Internet
Author: User

1. K-Nearest Neighbor algorithm

1 #Coding=utf-82  fromNumPyImport*3 Importoperator4 5 defCreateDataSet ():6Group = Array ([[[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])7Labels = ['A','A','B','B']8     returnGroup,labels9 Ten defclassify (inx,dataset,labels,k): OneDatasetsize =Dataset.shape[0] ADiffmat = Tile (InX, (datasetsize,1))-DataSet -Sqdiffmat = diffmat**2 -Sqdistances = Sqdiffmat.sum (Axis=1) thedistances = sqdistances**0.5 -     #the index where the return value resides -Sorteddistindices =Distances.argsort () -Classcount={} +      forIinchRange (k): -         #gets its corresponding category label according to the index +Voteilabel =Labels[sorteddistindices[i]] AClasscount[voteilabel] = Classcount.get (voteilabel,0) +1 at     PrintClassCount -Sortedclasscount = sorted (Classcount.iteritems (), Key=operator.itemgetter (1), reverse=True) -     returnSortedclasscount[0][0] -  - defMain (): -(group,labels) =CreateDataSet () in     PrintClassify ([0.8,1.5],group,labels,3) -  to if __name__=="__main__": +Main ()

The overall flow of the K-nearest neighbor algorithm is:

(1) Calculate the distance between the point in the data set of the known category and the current point;
(2) Sorting in ascending order of distance;
(3) Select K points with the minimum distance from the current point;
(4) Determine the frequency of occurrence of the category of the first k points;
(5) Return to the category with the highest frequency of the first K points as the forecast classification of the current point

The classify () function has 4 output parameters: The input vector used for the classification inx, the input training sample set dataset, the label vector labels, and the last parameter K for selecting the nearest neighboring number, where the number of elements of the input vector is the same as the number of rows in the matrix dataset. The distance between vector and matrix vector elements is computed first, then the results are sorted in order from small to large to determine the primary classification (input parameter K total positive integer) of the smallest element of the first k distance; Finally, the ClassCount dictionary is decomposed into a tuple list. Then use the second line of the program to import the Itemgetter method of the operator module, sorting the tuples in the order of the second element of the ClassCount dictionary. The sorting here is in reverse order, that is, order from maximum to minimum, and finally return the most frequently occurring element label, that is, the input vector belongs to the classification.

Recently is looking at "machine learning actual Combat", by the way a summary of the small examples, books are very good, recommend everyone to read!

The K-nearest neighbor algorithm for Python learning

Related Article

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.