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

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

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.

 

 

 

 

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值