求距离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.

 

 

 

 

你可能感兴趣的:(笔记心得)