Self-writing code:
1 #Author Chenglong Qian2 3 fromNumPyImport*#Scientific Computing Module4 Importoperator#operator Module5 6 defCreatedaraset ():7Group=array ([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])#Create an array of 4 rows and 2 columns8labels=['A',"A",'B','B']#List of tags9 returnGroup,labelsTen Onegroup,labels=Createdaraset () A - " "K-Nearest neighbor algorithm" " - defClassify0 (inx,dataset,labels,k):#InX: Vectors to be categorized, DataSet: Training sample, Labels: tag, K: Near Number the " "Ask for distance" " -DATASETSIZE=DATASET.SHAPE[0]#Number of sample data rows, i.e. number of samples -Diffmat=tile (InX, (datasetsize,1))-dataset#(from NumPy) tile: Repeating array, repeating inx datasetsize rows, 1 columns, and getting the difference (xi-x,yi-y) for each set of data -Sqdiffmat=diffmat**2#Ask the square +Sqdistances=sqdiffmat.sum (Axis=1)#sum (axis=1) matrix adds each row, sum (axis=0) adds each column -distances=sqdistances**0.5#Open Radicals +Sorteddistindicies=distances.argsort ()#The Argsort () function arranges the elements in X from small to large, extracts their corresponding index (index), and then outputs to Y. AClasscount={} at " "Sort" " - forIinchRange (k): -Voteilabel=labels[sorteddistindicies[i]]#Sorteddistindicies[i] I+1 Index of the small element -Classcount[voteilabel]=classcount.get (voteilabel,0) +1#classcount.get (voteilabel,0) Returns the value of the Voteilabel element in the dictionary classcount, if none, it is set to 0 - #This means that the number of a label is recorded -Sortedclasscount = sorted (Classcount.items (), Key=operator.itemgetter (1), reverse=true)#Sorted (list,key= custom sort order needs to be sorted, whether to reverse sort results) in #items returns the dictionary as a list (none in python3.5: Iteritems returns the dictionary as an iterator) - #The itemgetter function is used to get the data of the first dimension of the object Operator.itemgetter (1) using the second element to sort to returnSortedclasscount[0][0] + - the " "a parser for converting text records into matrix NumPy" " * defFile2matrix (filename): $Hrsopen (filename)Panax NotoginsengArrayolines=fr.readlines ()#readlines (): Returns a list of the remaining text (rows) in the file -Numberoflines=len (Arrayolines)#returns the length of an object theReturnmat=zeros ((numberoflines,3)) +Classlabelvector=[] Aindex=0 the forLineinchArrayolines: +Line=line.strip ()#The Strip () method removes the character specified by the string's Kinsoku (by default, a space or line break) or a sequence of characters. -Listfromline=line.split ('\ t')#split () slices a string by specifying a delimiter $Returnmat[index,:]=listfromline[0:3] $Classlabelvector.append (int (listfromline[-1])) -Index+=1 - returnReturnmat,classlabelvector
Library code
1 fromSklearnImportNeighbors2 fromSklearnImportDatasets3 4KNN =neighbors. Kneighborsclassifier ()5 6Iris =Datasets.load_iris ()7 8 PrintIris9 Ten Knn.fit (Iris.data, Iris.target) One APredictedlabel = Knn.predict ([[0.1, 0.2, 0.3, 0.4]]) - Print "Hello" - #print ("Predictedlabel is:" + Predictedlabel) the PrintPredictedlabel
Nearest Neighbor Rule classification (k-nearest Neighbor) KNN algorithm