统计学习方法[第3章 k近邻法 KNN](李航大神版)
手工实现
import math
from itertools import combinations
def Distance(x, y, p=2):
if len(x) == len(y) and len(x) > 1:
sum = 0;
for i in range(len(x)):
sum += math.pow(abs(x[i] - y[i]), p)
return math.pow(sum, 1 / p);
else:
return 0;
x1 = [1, 1]
x2 = [5, 1]
x3 = [4, 4]
for i in range(1, 5):
r = {'1-{}'.format(c): Distance(x1, c, p=i) for c in [x2, x3]}
print(min(zip(r.values(), r.keys())))
posted on 2019-07-22 14:34 Indian_Mysore 阅读(186) 评论(1) 编辑 收藏 举报