一导入的库import numpy as np import collections as cl二随机设立feature和labeldatanp.array([ [154,1], [126,2], [70,2], [196,2], [161,2], [371,4] ])featuredata[:,0] labeldata[:,-1]三选定要预测的值predictPoint200四计算每个feature与P-Point的差值并且排序distancelist(map(lambda x:abs(predictPoint-x),feature)) sortindexnp.argsort(distance) sortedlabellabel[sortindex]五选定适合的样本空间k3六在空间中找到最常出现的labelpre_resultcl.Counter(sortedlabel[0:k]).most_common(1)[0][0] print(pre_result)