以下內容轉自:https://blog.csdn.net/kancy110/article/details/75675574?utm_source=blogxgwz4
scipy.spatial.distance.cdist(XA, XB, metric='euclidean', p=None, V=None, VI=None, w=None),該函數用於計算兩個輸入集合的距離,通過metric參數指定計算距離的不同方式得到不同的距離度量值。
metric的取值如下:
chebyshev:切比雪夫距離
correlation:相關係數
cosine:餘弦夾角
euclidean:歐式距離
hamming:漢明距離
jaccard:傑卡德相似係數
mahalanobis:馬氏距離
minkowski:閔可夫斯基距離
seuclidean:標準化歐式距離
from scipy.spatial import distance
import numpy as np
x1 =np.array([(1,3),(2,4),(5,6)])
x2 =np.array([(3,7),(4,8),(6,9)])
x3 =(1,3)
x4 =(3,7)
#cdist中metric默認參數是歐式距離'euclidean'
y1=distance.cdist(x1,x2,metric='euclidean')
y2=distance.cdist(x1,x2)
#distance.euclidean()只能輸入一唯向量,輸出結果是float
y3=distance.euclidean(x3,x4)
print('y1:',y1,'type:',type(y1),'y2:',y2,'type:',type(y2),'y3:',y3,'type:',type(y3))
'''
Out[1]:
y1: [[4.47213595 5.83095189 7.81024968]
[3.16227766 4.47213595 6.40312424]
[2.23606798 2.23606798 3.16227766]] type: <class 'numpy.ndarray'>
y2: [[4.47213595 5.83095189 7.81024968]
[3.16227766 4.47213595 6.40312424]
[2.23606798 2.23606798 3.16227766]] type: <class 'numpy.ndarray'>
y3: 4.47213595499958 type: <class 'numpy.float64'>
'''