1. Simple examples
Steps
1.1 Calculating the distance between known and calculated points
1.2 Ascending Sort by distance
1.3 Finding the category maximum from the nearest top K points as the target category
fromNumPyImport*ImportoperatordefCreatedateset (): Group= Array ([[1.0,1.1], [1.0,1.0], [0,0], [0,0.1]]) labels= ['A','A','B','B'] returnGroup, Labelsdefclassify0 (InX, DataSet, labels, k): Datasetsize=Dataset.shape[0] Diffmat= Tile (InX, (datasetsize,1))-DataSet Sqdiffmat= Diffmat * * 2sqdistances= Sqdiffmat.sum (Axis=1) Distance= sqdistances * * 0.5sortdistindices=distance.argsort () ClassCount= {} forIinchRange (k): Voteilable=Labels[sortdistindices[i]] classcount[voteilable]= Classcount.get (voteilable, 0) + 1Sortedclasscount=Sorted (Classcount.iteritems (), Key= Operator.itemgetter (1), reverse=True)returnSortedclasscount[0][0]if __name__=="__main__": Group, labels=createdateset () InX= [1.1, 0.2] K= 3Aimclass=classify0 (InX, group, labels, k)PrintAimclass
Syntax parsing
A. Shape () Gets the length of each dimension of the matrix
B. Tiles, for example
>>> a[1, 2]>>> tile (A, 2) array ([1, 2, 1, 2]) >>> tile (A, (2,2)) Array ([[1, 2, 1, 2], [1, 2, 1 , 2]]) >>> tile (A, (3, 2,2)) array ([[[1], 2, 1, 2], [1, 2, 1, 2], [[1, 2, 1, 2], [1, 2, 1 , 2], [[1] , 2, 1, 2], [1, 2, 1, 2]]
C. sortdistindices = Distance.argsort () Get the ranking, the bigger the position
D. sortedclasscount = sorted (Classcount.iteritems (), key = Operator.itemgetter (1), reverse=true) sorts the values of the dictionary in reverse (descending order)
K-Nearest Neighbor algorithm instance