# Scipy 学习 第2篇：计算距离

Scipy中计算距离的模块是scipy.spatial.distance，最常用的方法是计算距离矩阵，换句话说，从存储在矩形数组中的观测向量集合中进行距离矩阵的计算。

## 一，两两距离

scipy.spatial.distance.pdist(X, metric='euclidean', **kwargs)

• X：ndarray类型，n维空间中m个观测值构成的 m行*n列 的数组
• metric：计算距离的函数，有效值是 ‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice’, ‘euclidean’, ‘hamming’, ‘jaccard’, ‘jensenshannon’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’
• **kwargs：dick类型，metric的额外参数，大致有：
• 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(X, axis=0, ddof=1)
• VI : ndarray The inverse of the covariance matrix for Mahalanobis. Default: inv(cov(X.T)).T
• out : ndarray. The output array If not None, condensed distance matrix Y is stored in this array. Note: metric independent, it will become a regular keyword arg in a future scipy version

## 二，配对记算距离

scipy.spatial.distance.cdist(XA, XB, metric='euclidean', *args, **kwargs)

XA和XB都是ndarray类型，在n维空间中，XA和XB进行配对，计算一队中两个数据集合之间的距离。

>>> 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]])

## 三，距离向量和距离矩阵的转换

scipy.spatial.distance.squareform(X, force='no', checks=True)

from scipy.spatial import distance as dist

a
array([[ 0,  2,  3,  4],
[ 2,  0,  7,  8],
[ 3,  7,  0, 12],
[ 4,  8, 12,  0]])

v=dist.squareform(a)
v
array([ 2,  3,  4,  7,  8, 12])

b=dist.squareform(v)
b
array([[ 0,  2,  3,  4],
[ 2,  0,  7,  8],
[ 3,  7,  0, 12],
[ 4,  8, 12,  0]])

scipy.spatial.distance

posted @ 2018-12-26 10:58  悦光阴  阅读(3523)  评论(0编辑  收藏  举报