求距离2——distance.cdist()方法,返回距离值

本文详细介绍如何使用Python的scipy库计算不同类型的点间距离,包括欧几里得距离和曼哈顿距离等,适用于二维及三维空间,并提供代码实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的求具体的距离,距离类型有:

The distance metric to use. If a string, the distance function can be ‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’(曼哈顿距离), ‘correlation’, ‘cosine’, ‘dice’, ‘euclidean’(欧几里得距离), ‘hamming’, ‘jaccard’, ‘jensenshannon’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘wminkowski’, ‘yule’.

使用:求欧几里得距离

from scipy.spatial import distance #导包
import numpy as np

#在二维数据点中求欧几里得距离
py.spatial.distance.cdist
coords = [(1, 1),
          (2, 2),
          (3, 3),
          (4, 4)]
print(distance.cdist(coords, coords, 'euclidean'))

>>

array([[0.        , 1.41421356, 2.82842712, 4.24264069],
       [1.41421356, 0.        , 1.41421356, 2.82842712],
       [2.82842712, 1.41421356, 0.        , 1.41421356],
       [4.24264069, 2.82842712, 1.41421356, 0.        ]])
#若求coords里面样本到目标的距离为(0,0)的点
distance.cdist(coords,[(0,0)], 'euclidean')

>>

array([[1.41421356],
       [2.82842712],
       [4.24264069],
       [5.65685425]])

 

#从3D数据点中找到曼哈顿距离,目标是X=a,Y=b  X是对象,Y是目标
a = np.array([[0, 0, 0],
              [0, 0, 1],
              [0, 1, 0],
              [0, 1, 1],
              [1, 0, 0],
              [1, 0, 1],
              [1, 1, 0],
              [1, 1, 1]])
b = np.array([[ 0.1,  0.2,  0.4]])
distance.cdist(a, b, 'cityblock')

>>

array([[0.7],
       [0.9],
       [1.3],
       [1.5],
       [1.5],
       [1.7],
       [2.1],
       [2.3]])

API:scipy.spatial.distance.cdist(XAXBmetric='euclidean'*args**kwargs)

详情参考:https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html

 

参数:

XAndarray

An mA by n array of mA original observations in an n-dimensional space. Inputs are converted to float type.

XBndarray

An mB by n array of mB original observations in an n-dimensional space. Inputs are converted to float type.

metricstr or callable, optional

The distance metric to use. If a string, the distance function can be ‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice’, ‘euclidean’, ‘hamming’, ‘jaccard’, ‘jensenshannon’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘wminkowski’, ‘yule’.

*argstuple. Deprecated.

Additional arguments should be passed as keyword arguments

**kwargsdict, optional

Extra arguments to metric: refer to each metric documentation for a list of all possible arguments.

Some possible arguments:

p : scalar The p-norm to apply for Minkowski, weighted and unweighted. Default: 2.

w : ndarray The weight vector for metrics that support weights (e.g., Minkowski).

V : ndarray The variance vector for standardized Euclidean. Default: var(vstack([XA, XB]), axis=0, ddof=1)

VI : ndarray The inverse of the covariance matrix for Mahalanobis. Default: inv(cov(vstack([XA, XB].T))).T

out : ndarray The output array If not None, the distance matrix Y is stored in this array. Note: metric independent, it will become a regular keyword arg in a future scipy version

Returns

Yndarray

A mA by mB distance matrix is returned. For each i and j, the metricdist(u=XA[i], v=XB[j]) is computed and stored in the ij th entry.

 

 

 

 

### 使用Python实现点云之间单向豪斯多夫距离 为了计算两个点云之间的单向豪斯多夫距离,可以采用遍历的方式找到一个集合中的每一点到另一个集合中最短距离的最大。具体来说,在给定两组点集 \( A \) 和 \( B \),对于每一个属于 \( A \) 的点寻找其最近邻于 \( B \) 中的距离,并记录这些最小距离里的最大者作为从 \( A \) 到 \( B \) 方向上的豪斯多夫距离。 下面是一个简单的 Python 实现来完成这项工作: ```python import numpy as np from scipy.spatial.distance import cdist def one_way_hausdorff_distance(A, B): """ 计算A到B的单向豪斯多夫距离 参数: A (numpy.ndarray): 第一个点云集,形状为(N,d) B (numpy.ndarray): 第二个点云集,形状为(M,d) 返回: float: 单向豪斯多夫距离 """ distances = cdist(A, B).min(axis=1) # 对每个a∈A找b∈B使得||a-b||最小化 hausdorff_dist = distances.max() # 取上述最小得最大即为所 return hausdorff_dist # ^[1] # 示例数据 points_A = np.array([[0., 0.], [1., 1.]]) points_B = np.array([[1., 0.], [0., 1.]]) result = one_way_hausdorff_distance(points_A, points_B) print(f"One-way Hausdorff distance from A to B is {result}") ``` 此代码片段定义了一个函数 `one_way_hausdorff_distance` 来接收两个 NumPy 数组形式表示的点云并返回它们间的单向豪斯多夫距离。这里利用 SciPy 库中的 `cdist()` 函数快速有效地计算任意两点间欧几里得距离矩阵;接着通过 `.min(axis=1)` 获取每一行(对应着来自第一个点云的一个样本)沿列方向上取得的最小——也就是该点至另一点集中最接近它的那个成员之距;最后再取所有这样的极小当中的极大就得到了最终的结果[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值