使用列表List作为样本点表示的欧氏距离计算方法:
import <a href="https://www.gaodaima.com/tag/math" title="查看更多关于math的文章" target="_blank">math</a> # 计算两点之间的距离 def eucliDist(A,B): return math.sqrt(sum([(a - b)**2 for (a,b) in zip(A,B)])) X = [1,2,3,4] Y = [0,1,2,3] print(eucliDist(X,Y))
www#gaodaima.com来源gaodaimacom搞#代%码网搞代码
使用np.array作为样本点表示的欧氏距离计算方法:
import numpy as np # 计算两点之间的距离 def eucliDist(A,B): return np.sqrt(sum(np.power((A - B), 2))) # return math.sqrt(sum([(a - b)**2 for (a,b) in zip(A,B)])) X = np.array([1,2,3,4]) Y = np.array([0,1,2,3]) print(eucliDist(X,Y))
众多python相关教程,尽在搞代码网,欢迎在线学习!
来源:搞代码网:原文地址:https://www.gaodaima.com