Paste the Python code below
knnclassify.py
1 fromNumPyImport*2 Importoperator3 4 defCreatdataset ():5Group = Array ([[[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]])6Labels = ['A','A','B','B']7 returnGroup, Labels8 9 defclassify (inx,dataset,labels,k):TenNumSamples =Dataset.shape[0] OneDiffmat = Tile (InX, (numsamples,1))-DataSet ASqdiffmat = diffmat**2 -Sqdistances = sqdiffmat.sum (Axis = 1) -distances = sqdistances * * 0.5 theSorteddistindicies =Distances.argsort () -ClassCount = {} - forIinchxrange (k): -Voteilabel =Labels[sorteddistindicies[i]] +Classcount[voteilabel] = Classcount.get (voteilabel,0) + 1 -Maxlabel =Sorted (Classcount.iteritems (), +Key = Operator.itemgetter (1), reverse =True) A returnMaxlabel[0][0] at - if __name__=="__main__": -G,l =Creatdataset () -Labelofinx = Classify ([1.0,1.2],g,l,1) - PrintLabelofinx
K-Nearest Neighbor algorithm and its Python implementation