python求矩阵中两两行向量间距离:pdist 的vector-form distance vector 和 square-form distance matrix

浏览: 2102
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

推荐 0
本文由 safa 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册