Haven't written a blog for a long time, whim. To write a just learn the KNN algorithm, in fact, is more than the similarity, by the high similarity
Nonsense, no code.
From numpy import *import operator #创建初始矩阵group = Array ([[1.0,1.1],[1.0,1.0],[0,0],[0,0.1]]) label = [' A ', ' a ', ' B ', ' B ']def classfy (inx,dataset,labes,k): datasetsize = dataset.shape[0] #获取矩阵的维度, which is the number of rows in the matrix Diffmat = Tile (InX, ( datasetsize,1))-dataset# the relative distance Sqdiffmat = diffmat**2 sqdistance = sqdiffmat.sum (axis=1) distance = sqdistance**0.5# distance between two points formula is not interpreted sorteddistinicies = Distance.argsort () #得到排序后的下标 "2,3,1,0" ClassCount = {} For I in range (k): Voteilabel = labes[sorteddistinicies[i]] #此处就和K相关了 Classcount[voteilabel] = Classcount.get (voteilabel,0) +1# record occurrences sortedclasscount = sorted (Classcount.iteritems (), Key=operator.itemgetter (1), Reverse=true) return sortedclasscount[0][0] #返回最终的结果classfy ([0,0],group,label,3)
It's all in the code annotations, OK.
KNN algorithm Python implementation