2019-01-16
阅读量:
875
python实现欧几里得距离
寻找欧几里德距离
两个向量x和y的广义欧几里德公式是这样的:
距离= sqrt {(x_ {1} -y_ {1})^ 2 +(x_ {2} -y_ {2})^ 2 + ... +(x_ {n} -y_ {n})^ 2}
在代码中:
filter_none
brightness_4def EuclideanDistance(x, y):
# The sum of the squared
# differences of the elements
S = 0;
for key in x.keys():
S += math.pow(x[key]-y[key], 2);
# The square root of the sum
return math.sqrt(S);
def CalculateNeighborsClass(neighbors, k):
count = {};
for i in range(k):
if(neighbors[i][1] not in count):
# The class at the ith index
# is not in the count dict.
# Initialize it to 1.
count[neighbors[i][1]] = 1;
else:
# Found another item of class
# c[i]. Increment its counter.
count[neighbors[i][1]] += 1;
return count;
0.0000
0
5
关注作者
收藏
评论(0)
发表评论
暂无数据
推荐帖子
0条评论
0条评论
0条评论

