博主按照KNN原理手写的算法,水平有限,如有错误可在评论区指正,谢谢。
def getdistance(item):
return item[0]
def knn(point, datas, kind, k=3):
distance = []
for i in datas:
distance.append([abs(i[0] - point[0] + i[1] - point[1]), i[2]])
distance.sort(key=getdistance)
sum = []
for i in range(kind):
sum.append(i)
for i in range(k):
sum[distance[i][1]] += 1
return sum.index(max(sum))
if __name__ == '__main__':
p = [1.0, 1.0]
datas = [[2.0, 2.0, 0], [3.0, 3.0, 0], [4.0, 4.0, 1], [-1.0, -1.0, 0], [5.0, 5.0, 1]]
result = knn(p, datas, 2)
print(result)
|