import numpy as np
from scipy.spatial.distance import pdist, squareform
points = np.array([[0,1],[1,1],[3,5], [15, 5]])
points一行是一个observation,一列是一个维度
去冗余的向量形距离:
dist = pdist(points)
array([ 1. , 5. , 15.5241747 , 4.47213595,
14.56021978, 12. ])
方形的距离:
dist_square = squareform(dist)
array([[ 0. , 1. , 5. , 15.5241747 ],
[ 1. , 0. , 4.47213595, 14.56021978],
[ 5. , 4.47213595, 0. , 12. ],
[ 15.5241747 , 14.56021978, 12. , 0. ]])
某两个向量间距离:
dist_square[2, 0]
5.0
等价于:
np.linalg.norm(points[2] - points[0])
5.0
参考:
https://webcache.googleusercontent.com/search?q=cache:dX6OWIQjvh8J:https://code.i-harness.com/zh-CN/q/c7940b+&cd=4&hl=en&ct=clnk&gl=sg
https://blog.csdn.net/pipisorry/article/details/48814183