# # kNN 分类算法 a = np.array([[1,1],[1.2,1.5],[0.3,0.4],[0.2,0.5]]) #构造样本数据 labels = ['A','A','B','B'] # print(a.shape[0]) # 行数 shape(1)表示列数 diffMat = np.tile (np.array([[1.5,1.2]]),(a.shape[0],1)) - a #tile(被重复数据,(重复几行,每行重复几次)) # diffMat # [[ 0.5 0.2] # [ 0.3 -0.3] # [ 1.2 0.8] # [ 1.3 0.7]] a = diffMat ** 2 # a # [[0.25 0.04] # [0.09 0.09] # [1.44 0.64] # [1.69 0.49]] distence = a.sum(axis=1) # 1表示行 0表示列 # distence # [0.29 0.18 2.08 2.18] dis_sort = distence.argsort() # dis_sort # [1 0 2 3] k = 3 classcount = {} for i in range(0,len(dis_sort)): if i>=k: break if dis_sort[i] <= k + 1: classcount[labels[i]] = classcount.get(labels[i],0)+1 # classcount # {'A': 2, 'B': 1}