求距离3——distance.pdist()方法,返回距离值

与求距离2——distance.cdist()方法类似,但有区别

cdist 与pdist 区别
  distance.cdist()方法 distance.pdist()方法
输入 输入两个距离(X、Y),计算方法是X中的点一次与Y中的点求距离。 输入一个数组点(X),里面的元素反复对比求距离(i -> i+1距离,i -> i+2距离,i -> i+3 距离…)
输出 返回 X形状*Y形状的矩阵 返回一个列表

求欧几里得距离 ,例题:

from scipy.spatial import distance

coords = [(1, 1),
          (2, 2),
          (3, 3),
          (4, 4)]

distance.pdist(coords, 'euclidean')

>>

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

 

API : scipy.spatial.distance.pdist(Xmetric='euclidean'*args**kwargs)

参考:https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html#scipy.spatial.distance.pdist

参数:

Xndarray

An m by n array of m original observations in an n-dimensional space.

metricstr or function, optional

The distance metric to use. 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’, ‘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(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

Returns

Yndarray

Returns a condensed distance matrix Y. For each i and j (where idist(u=X[i], v=X[j]) is computed and stored in entry ij.

 

 

 

 

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