spatial.distance

Function Reference

Distance matrix computation from a collection of raw observation vectors stored in a rectangular array.

pdist
cdist
squareform
directed_hausdorff

Predicates for checking the validity of distance matrices, both condensed and redundant. Also contained in this module are functions for computing the number of observations in a distance matrix.

is_valid_dm
is_valid_y
num_obs_dm
num_obs_y

Distance functions between two numeric vectors u and v. Computing distances over a large collection of vectors is inefficient for these functions. Use pdist for this purpose.

braycurtis
canberra
chebyshev
cityblock
correlation
cosine
euclidean
mahalanobis
minkowski
seuclidean
sqeuclidean
wminkowski

Distance functions between two boolean vectors (representing sets) u and v. As in the case of numerical vectors, pdist is more efficient for computing the distances between all pairs.

dice
hamming
jaccard
kulsinski
rogerstanimoto
russellrao
sokalmichener
sokalsneath
yule

hamming() also operates over discrete numerical vectors.

Module Contents

Functions

_args_to_kwargs_xdist(args,kwargs,metric,func_name) Convert legacy positional arguments to keyword arguments for pdist/cdist.
_copy_array_if_base_present(a) Copies the array if its base points to a parent array.
_correlation_cdist_wrap(XA,XB,dm,**kwargs)
_correlation_pdist_wrap(X,dm,**kwargs)
_convert_to_type(X,out_type)
_filter_deprecated_kwargs(kwargs,args_blacklist)
_nbool_correspond_all(u,v,w=None)
_nbool_correspond_ft_tf(u,v,w=None)
_validate_cdist_input(XA,XB,mA,mB,n,metric_name,**kwargs)
_validate_mahalanobis_kwargs(X,m,n,**kwargs)
_validate_minkowski_kwargs(X,m,n,**kwargs)
_validate_pdist_input(X,m,n,metric_name,**kwargs)
_validate_seuclidean_kwargs(X,m,n,**kwargs)
_validate_vector(u,dtype=None)
_validate_weights(w,dtype=None)
_validate_wminkowski_kwargs(X,m,n,**kwargs)
directed_hausdorff(u,v,seed=0) Computes the directed Hausdorff distance between two N-D arrays.
minkowski(u,v,p=2,w=None) Computes the Minkowski distance between two 1-D arrays.
wminkowski(u,v,p,w) Computes the weighted Minkowski distance between two 1-D arrays.
euclidean(u,v,w=None) Computes the Euclidean distance between two 1-D arrays.
sqeuclidean(u,v,w=None) Computes the squared Euclidean distance between two 1-D arrays.
correlation(u,v,w=None,centered=True) Computes the correlation distance between two 1-D arrays.
cosine(u,v,w=None) Computes the Cosine distance between 1-D arrays.
hamming(u,v,w=None) Computes the Hamming distance between two 1-D arrays.
jaccard(u,v,w=None) Computes the Jaccard-Needham dissimilarity between two boolean 1-D arrays.
kulsinski(u,v,w=None) Computes the Kulsinski dissimilarity between two boolean 1-D arrays.
seuclidean(u,v,V) Returns the standardized Euclidean distance between two 1-D arrays.
cityblock(u,v,w=None) Computes the City Block (Manhattan) distance.
mahalanobis(u,v,VI) Computes the Mahalanobis distance between two 1-D arrays.
chebyshev(u,v) Computes the Chebyshev distance.
braycurtis(u,v,w=None) Computes the Bray-Curtis distance between two 1-D arrays.
canberra(u,v,w=None) Computes the Canberra distance between two 1-D arrays.
yule(u,v,w=None) Computes the Yule dissimilarity between two boolean 1-D arrays.
matching(u,v,w=None) Computes the Hamming distance between two boolean 1-D arrays.
dice(u,v,w=None) Computes the Dice dissimilarity between two boolean 1-D arrays.
rogerstanimoto(u,v,w=None) Computes the Rogers-Tanimoto dissimilarity between two boolean 1-D arrays.
russellrao(u,v,w=None) Computes the Russell-Rao dissimilarity between two boolean 1-D arrays.
sokalmichener(u,v,w=None) Computes the Sokal-Michener dissimilarity between two boolean 1-D arrays.
sokalsneath(u,v,w=None) Computes the Sokal-Sneath dissimilarity between two boolean 1-D arrays.
pdist(X,metric=”euclidean”,*args,**kwargs) Pairwise distances between observations in n-dimensional space.
squareform(X,force=”no”,checks=True) Converts a vector-form distance vector to a square-form distance
is_valid_dm(D,tol=0.0,throw=False,name=”D”,warning=False) Returns True if input array is a valid distance matrix.
is_valid_y(y,warning=False,throw=False,name=None) Returns True if the input array is a valid condensed distance matrix.
num_obs_dm(d) Returns the number of original observations that correspond to a
num_obs_y(Y) Returns the number of original observations that correspond to a
cdist(XA,XB,metric=”euclidean”,*args,**kwargs) Computes distance between each pair of the two collections of inputs.
_args_to_kwargs_xdist(args, kwargs, metric, func_name)

Convert legacy positional arguments to keyword arguments for pdist/cdist.

_copy_array_if_base_present(a)

Copies the array if its base points to a parent array.

_correlation_cdist_wrap(XA, XB, dm, **kwargs)
_correlation_pdist_wrap(X, dm, **kwargs)
_convert_to_type(X, out_type)
_filter_deprecated_kwargs(kwargs, args_blacklist)
_nbool_correspond_all(u, v, w=None)
_nbool_correspond_ft_tf(u, v, w=None)
_validate_cdist_input(XA, XB, mA, mB, n, metric_name, **kwargs)
_validate_mahalanobis_kwargs(X, m, n, **kwargs)
_validate_minkowski_kwargs(X, m, n, **kwargs)
_validate_pdist_input(X, m, n, metric_name, **kwargs)
_validate_seuclidean_kwargs(X, m, n, **kwargs)
_validate_vector(u, dtype=None)
_validate_weights(w, dtype=None)
_validate_wminkowski_kwargs(X, m, n, **kwargs)
directed_hausdorff(u, v, seed=0)

Computes the directed Hausdorff distance between two N-D arrays.

Distances between pairs are calculated using a Euclidean metric.

u : (M,N) ndarray
Input array.
v : (O,N) ndarray
Input array.
seed : int or None
Local np.random.RandomState seed. Default is 0, a random shuffling of u and v that guarantees reproducibility.
d : double
The directed Hausdorff distance between arrays u and v,
index_1 : int
index of point contributing to Hausdorff pair in u
index_2 : int
index of point contributing to Hausdorff pair in v

Uses the early break technique and the random sampling approach described by [1]. Although worst-case performance is O(m * o) (as with the brute force algorithm), this is unlikely in practice as the input data would have to require the algorithm to explore every single point interaction, and after the algorithm shuffles the input points at that. The best case performance is O(m), which is satisfied by selecting an inner loop distance that is less than cmax and leads to an early break as often as possible. The authors have formally shown that the average runtime is closer to O(m).

New in version 0.19.0.

[1]A. A. Taha and A. Hanbury, “An efficient algorithm for calculating the exact Hausdorff distance.” IEEE Transactions On Pattern Analysis And Machine Intelligence, vol. 37 pp. 2153-63, 2015.

scipy.spatial.procrustes : Another similarity test for two data sets

Find the directed Hausdorff distance between two 2-D arrays of coordinates:

>>> from scipy.spatial.distance import directed_hausdorff
>>> u = np.array([(1.0, 0.0),
...               (0.0, 1.0),
...               (-1.0, 0.0),
...               (0.0, -1.0)])
>>> v = np.array([(2.0, 0.0),
...               (0.0, 2.0),
...               (-2.0, 0.0),
...               (0.0, -4.0)])
>>> directed_hausdorff(u, v)[0]
2.23606797749979
>>> directed_hausdorff(v, u)[0]
3.0

Find the general (symmetric) Hausdorff distance between two 2-D arrays of coordinates:

>>> max(directed_hausdorff(u, v)[0], directed_hausdorff(v, u)[0])
3.0

Find the indices of the points that generate the Hausdorff distance (the Hausdorff pair):

>>> directed_hausdorff(v, u)[1:]
(3, 3)
minkowski(u, v, p=2, w=None)

Computes the Minkowski distance between two 1-D arrays.

The Minkowski distance between 1-D arrays u and v, is defined as

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
p : int
The order of the norm of the difference .
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
minkowski : double
The Minkowski distance between vectors u and v.
wminkowski(u, v, p, w)

Computes the weighted Minkowski distance between two 1-D arrays. The weighted Minkowski distance between u and v, defined as

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
p : int
The order of the norm of the difference .
w : (N,) array_like
The weight vector.
wminkowski : double
The weighted Minkowski distance between vectors u and v.

wminkowski is DEPRECATED. It implements a definition where weights are powered. It is recommended to use the weighted version of minkowski instead. This function will be removed in a future version of scipy.

euclidean(u, v, w=None)

Computes the Euclidean distance between two 1-D arrays.

The Euclidean distance between 1-D arrays u and v, is defined as

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
euclidean : double
The Euclidean distance between vectors u and v.
sqeuclidean(u, v, w=None)

Computes the squared Euclidean distance between two 1-D arrays.

The squared Euclidean distance between u and v is defined as

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
sqeuclidean : double
The squared Euclidean distance between vectors u and v.
correlation(u, v, w=None, centered=True)

Computes the correlation distance between two 1-D arrays. The correlation distance between u and v, is defined as

where is the mean of the elements of u and is the dot product of and .

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
correlation : double
The correlation distance between 1-D array u and v.
cosine(u, v, w=None)

Computes the Cosine distance between 1-D arrays. The Cosine distance between u and v, is defined as

where is the dot product of and .

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
cosine : double
The Cosine distance between vectors u and v.
hamming(u, v, w=None)

Computes the Hamming distance between two 1-D arrays.

The Hamming distance between 1-D arrays u and v, is simply the proportion of disagreeing components in u and v. If u and v are boolean vectors, the Hamming distance is

where is the number of occurrences of and for .

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
hamming : double
The Hamming distance between vectors u and v.
jaccard(u, v, w=None)

Computes the Jaccard-Needham dissimilarity between two boolean 1-D arrays.

The Jaccard-Needham dissimilarity between 1-D boolean arrays u and v, is defined as

where is the number of occurrences of and for .

u : (N,) array_like, bool
Input array.
v : (N,) array_like, bool
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
jaccard : double
The Jaccard distance between vectors u and v.
kulsinski(u, v, w=None)

Computes the Kulsinski dissimilarity between two boolean 1-D arrays.

The Kulsinski dissimilarity between two boolean 1-D arrays u and v, is defined as

where is the number of occurrences of and for .

u : (N,) array_like, bool
Input array.
v : (N,) array_like, bool
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
kulsinski : double
The Kulsinski distance between vectors u and v.
seuclidean(u, v, V)

Returns the standardized Euclidean distance between two 1-D arrays.

The standardized Euclidean distance between u and v.

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
V : (N,) array_like
V is an 1-D array of component variances. It is usually computed among a larger collection vectors.
seuclidean : double
The standardized Euclidean distance between vectors u and v.
cityblock(u, v, w=None)

Computes the City Block (Manhattan) distance.

Computes the Manhattan distance between two 1-D arrays u and v, which is defined as

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
cityblock : double
The City Block (Manhattan) distance between vectors u and v.
mahalanobis(u, v, VI)

Computes the Mahalanobis distance between two 1-D arrays.

The Mahalanobis distance between 1-D arrays u and v, is defined as

where V is the covariance matrix. Note that the argument VI is the inverse of V.

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
VI : ndarray
The inverse of the covariance matrix.
mahalanobis : double
The Mahalanobis distance between vectors u and v.
chebyshev(u, v)

Computes the Chebyshev distance.

Computes the Chebyshev distance between two 1-D arrays u and v, which is defined as

u : (N,) array_like
Input vector.
v : (N,) array_like
Input vector.
chebyshev : double
The Chebyshev distance between vectors u and v.
braycurtis(u, v, w=None)

Computes the Bray-Curtis distance between two 1-D arrays.

Bray-Curtis distance is defined as

The Bray-Curtis distance is in the range [0, 1] if all coordinates are positive, and is undefined if the inputs are of length zero.

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
braycurtis : double
The Bray-Curtis distance between 1-D arrays u and v.
canberra(u, v, w=None)

Computes the Canberra distance between two 1-D arrays.

The Canberra distance is defined as

u : (N,) array_like
Input array.
v : (N,) array_like
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
canberra : double
The Canberra distance between vectors u and v.

When u[i] and v[i] are 0 for given i, then the fraction 0/0 = 0 is used in the calculation.

yule(u, v, w=None)

Computes the Yule dissimilarity between two boolean 1-D arrays.

The Yule dissimilarity is defined as

where is the number of occurrences of and for and .

u : (N,) array_like, bool
Input array.
v : (N,) array_like, bool
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
yule : double
The Yule dissimilarity between vectors u and v.
matching(u, v, w=None)

Computes the Hamming distance between two boolean 1-D arrays.

This is a deprecated synonym for hamming().

dice(u, v, w=None)

Computes the Dice dissimilarity between two boolean 1-D arrays.

The Dice dissimilarity between u and v, is

where is the number of occurrences of and for .

u : (N,) ndarray, bool
Input 1-D array.
v : (N,) ndarray, bool
Input 1-D array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
dice : double
The Dice dissimilarity between 1-D arrays u and v.
rogerstanimoto(u, v, w=None)

Computes the Rogers-Tanimoto dissimilarity between two boolean 1-D arrays.

The Rogers-Tanimoto dissimilarity between two boolean 1-D arrays u and v, is defined as

where is the number of occurrences of and for and .

u : (N,) array_like, bool
Input array.
v : (N,) array_like, bool
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
rogerstanimoto : double
The Rogers-Tanimoto dissimilarity between vectors u and v.
russellrao(u, v, w=None)

Computes the Russell-Rao dissimilarity between two boolean 1-D arrays.

The Russell-Rao dissimilarity between two boolean 1-D arrays, u and v, is defined as

where is the number of occurrences of and for .

u : (N,) array_like, bool
Input array.
v : (N,) array_like, bool
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
russellrao : double
The Russell-Rao dissimilarity between vectors u and v.
sokalmichener(u, v, w=None)

Computes the Sokal-Michener dissimilarity between two boolean 1-D arrays.

The Sokal-Michener dissimilarity between boolean 1-D arrays u and v, is defined as

where is the number of occurrences of and for , and .

u : (N,) array_like, bool
Input array.
v : (N,) array_like, bool
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
sokalmichener : double
The Sokal-Michener dissimilarity between vectors u and v.
sokalsneath(u, v, w=None)

Computes the Sokal-Sneath dissimilarity between two boolean 1-D arrays.

The Sokal-Sneath dissimilarity between u and v,

where is the number of occurrences of and for and .

u : (N,) array_like, bool
Input array.
v : (N,) array_like, bool
Input array.
w : (N,) array_like, optional
The weights for each value in u and v. Default is None, which gives each value a weight of 1.0
sokalsneath : double
The Sokal-Sneath dissimilarity between vectors u and v.
pdist(X, metric="euclidean", *args, **kwargs)

Pairwise distances between observations in n-dimensional space.

See Notes for common calling conventions.

X : ndarray
An m by n array of m original observations in an n-dimensional space.
metric : str or function, optional
The distance metric to use. The distance function can be ‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice’, ‘euclidean’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’.
*args : tuple. Deprecated.
Additional arguments should be passed as keyword arguments
**kwargs : dict, 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

Y : ndarray
Returns a condensed distance matrix Y. For each and (where ),where m is the number of original observations. The metric dist(u=X[i], v=X[j]) is computed and stored in entry ij.
squareform : converts between condensed distance matrices and
square distance matrices.

See squareform for information on how to calculate the index of this entry or to convert the condensed distance matrix to a redundant square matrix.

The following are common calling conventions.

  1. Y = pdist(X, 'euclidean')

    Computes the distance between m points using Euclidean distance (2-norm) as the distance metric between the points. The points are arranged as m n-dimensional row vectors in the matrix X.

  2. Y = pdist(X, 'minkowski', p=2.)

    Computes the distances using the Minkowski distance (p-norm) where .

  3. Y = pdist(X, 'cityblock')

    Computes the city block or Manhattan distance between the points.

  4. Y = pdist(X, 'seuclidean', V=None)

    Computes the standardized Euclidean distance. The standardized Euclidean distance between two n-vectors u and v is

    V is the variance vector; V[i] is the variance computed over all the i’th components of the points. If not passed, it is automatically computed.

  5. Y = pdist(X, 'sqeuclidean')

    Computes the squared Euclidean distance between the vectors.

  6. Y = pdist(X, 'cosine')

    Computes the cosine distance between vectors u and v,

    where is the 2-norm of its argument *, and is the dot product of u and v.

  7. Y = pdist(X, 'correlation')

    Computes the correlation distance between vectors u and v. This is

    where is the mean of the elements of vector v, and is the dot product of and .

  8. Y = pdist(X, 'hamming')

    Computes the normalized Hamming distance, or the proportion of those vector elements between two n-vectors u and v which disagree. To save memory, the matrix X can be of type boolean.

  9. Y = pdist(X, 'jaccard')

    Computes the Jaccard distance between the points. Given two vectors, u and v, the Jaccard distance is the proportion of those elements u[i] and v[i] that disagree.

  10. Y = pdist(X, 'chebyshev')

Computes the Chebyshev distance between the points. The Chebyshev distance between two n-vectors u and v is the maximum norm-1 distance between their respective elements. More precisely, the distance is given by

  1. Y = pdist(X, 'canberra')

Computes the Canberra distance between the points. The Canberra distance between two points u and v is

  1. Y = pdist(X, 'braycurtis')

Computes the Bray-Curtis distance between the points. The Bray-Curtis distance between two points u and v is

  1. Y = pdist(X, 'mahalanobis', VI=None)
Computes the Mahalanobis distance between the points. The Mahalanobis distance between two points u and v is where (the VI variable) is the inverse covariance. If VI is not None, VI will be used as the inverse covariance matrix.
  1. Y = pdist(X, 'yule')
Computes the Yule distance between each pair of boolean vectors. (see yule function documentation)
  1. Y = pdist(X, 'matching')
Synonym for ‘hamming’.
  1. Y = pdist(X, 'dice')
Computes the Dice distance between each pair of boolean vectors. (see dice function documentation)
  1. Y = pdist(X, 'kulsinski')
Computes the Kulsinski distance between each pair of boolean vectors. (see kulsinski function documentation)
  1. Y = pdist(X, 'rogerstanimoto')
Computes the Rogers-Tanimoto distance between each pair of boolean vectors. (see rogerstanimoto function documentation)
  1. Y = pdist(X, 'russellrao')
Computes the Russell-Rao distance between each pair of boolean vectors. (see russellrao function documentation)
  1. Y = pdist(X, 'sokalmichener')
Computes the Sokal-Michener distance between each pair of boolean vectors. (see sokalmichener function documentation)
  1. Y = pdist(X, 'sokalsneath')
Computes the Sokal-Sneath distance between each pair of boolean vectors. (see sokalsneath function documentation)
  1. Y = pdist(X, 'wminkowski', p=2, w=w)
Computes the weighted Minkowski distance between each pair of vectors. (see wminkowski function documentation)
  1. Y = pdist(X, f)

Computes the distance between all pairs of vectors in X using the user supplied 2-arity function f. For example, Euclidean distance between the vectors could be computed as follows:

dm = pdist(X, lambda u, v: np.sqrt(((u-v)**2).sum()))

Note that you should avoid passing a reference to one of the distance functions defined in this library. For example,:

dm = pdist(X, sokalsneath)

would calculate the pair-wise distances between the vectors in X using the Python function sokalsneath. This would result in sokalsneath being called times, which is inefficient. Instead, the optimized C version is more efficient, and we call it using the following syntax.:

dm = pdist(X, 'sokalsneath')
squareform(X, force="no", checks=True)

Converts a vector-form distance vector to a square-form distance matrix, and vice-versa.

X : ndarray
Either a condensed or redundant distance matrix.
force : str, optional
As with MATLAB(TM), if force is equal to 'tovector' or 'tomatrix', the input will be treated as a distance matrix or distance vector respectively.
checks : bool, optional
If set to False, no checks will be made for matrix symmetry nor zero diagonals. This is useful if it is known that X - X.T1 is small and diag(X) is close to zero. These values are ignored any way so they do not disrupt the squareform transformation.
Y : ndarray
If a condensed distance matrix is passed, a redundant one is returned, or if a redundant one is passed, a condensed distance matrix is returned.
  1. v = squareform(X)

    Given a square d-by-d symmetric distance matrix X, v = squareform(X) returns a d * (d-1) / 2 (or ) sized vector v.

is the distance between points i and j. If X is non-square or asymmetric, an error is returned.
  1. X = squareform(v)
Given a d*(d-1)/2 sized v for some integer d >= 2 encoding distances as described, X = squareform(v) returns a d by d distance matrix X. The X[i, j] and X[j, i] values are set to and all diagonal elements are zero.

In Scipy 0.19.0, squareform stopped casting all input types to float64, and started returning arrays of the same dtype as the input.

is_valid_dm(D, tol=0.0, throw=False, name="D", warning=False)

Returns True if input array is a valid distance matrix.

Distance matrices must be 2-dimensional numpy arrays. They must have a zero-diagonal, and they must be symmetric.

D : ndarray
The candidate object to test for validity.
tol : float, optional
The distance matrix should be symmetric. tol is the maximum difference between entries ij and ji for the distance metric to be considered symmetric.
throw : bool, optional
An exception is thrown if the distance matrix passed is not valid.
name : str, optional
The name of the variable to checked. This is useful if throw is set to True so the offending variable can be identified in the exception message when an exception is thrown.
warning : bool, optional
Instead of throwing an exception, a warning message is raised.
valid : bool
True if the variable D passed is a valid distance matrix.

Small numerical differences in D and D.T and non-zeroness of the diagonal are ignored if they are within the tolerance specified by tol.

is_valid_y(y, warning=False, throw=False, name=None)

Returns True if the input array is a valid condensed distance matrix.

Condensed distance matrices must be 1-dimensional numpy arrays. Their length must be a binomial coefficient for some positive integer n.

y : ndarray
The condensed distance matrix.
warning : bool, optional
Invokes a warning if the variable passed is not a valid condensed distance matrix. The warning message explains why the distance matrix is not valid. name is used when referencing the offending variable.
throw : bool, optional
Throws an exception if the variable passed is not a valid condensed distance matrix.
name : bool, optional
Used when referencing the offending variable in the warning or exception message.
num_obs_dm(d)

Returns the number of original observations that correspond to a square, redundant distance matrix.

d : ndarray
The target distance matrix.
num_obs_dm : int
The number of observations in the redundant distance matrix.
num_obs_y(Y)

Returns the number of original observations that correspond to a condensed distance matrix.

Y : ndarray
Condensed distance matrix.
n : int
The number of observations in the condensed distance matrix Y.
cdist(XA, XB, metric="euclidean", *args, **kwargs)

Computes distance between each pair of the two collections of inputs.

See Notes for common calling conventions.

XA : ndarray
An by array of original observations in an -dimensional space. Inputs are converted to float type.
XB : ndarray
An by array of original observations in an -dimensional space. Inputs are converted to float type.
metric : str 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’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘wminkowski’, ‘yule’.
*args : tuple. Deprecated.
Additional arguments should be passed as keyword arguments
**kwargs : dict, 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

Y : ndarray
A by distance matrix is returned. For each and , the metric dist(u=XA[i], v=XB[j]) is computed and stored in the th entry.
ValueError
An exception is thrown if XA and XB do not have the same number of columns.

The following are common calling conventions:

  1. Y = cdist(XA, XB, 'euclidean')

    Computes the distance between points using Euclidean distance (2-norm) as the distance metric between the points. The points are arranged as -dimensional row vectors in the matrix X.

  2. Y = cdist(XA, XB, 'minkowski', p=2.)

    Computes the distances using the Minkowski distance (-norm) where .

  3. Y = cdist(XA, XB, 'cityblock')

    Computes the city block or Manhattan distance between the points.

  4. Y = cdist(XA, XB, 'seuclidean', V=None)

    Computes the standardized Euclidean distance. The standardized Euclidean distance between two n-vectors u and v is

    V is the variance vector; V[i] is the variance computed over all the i’th components of the points. If not passed, it is automatically computed.

  5. Y = cdist(XA, XB, 'sqeuclidean')

    Computes the squared Euclidean distance between the vectors.

  6. Y = cdist(XA, XB, 'cosine')

    Computes the cosine distance between vectors u and v,

    where is the 2-norm of its argument *, and is the dot product of and .

  7. Y = cdist(XA, XB, 'correlation')

    Computes the correlation distance between vectors u and v. This is

    where is the mean of the elements of vector v, and is the dot product of and .

  8. Y = cdist(XA, XB, 'hamming')

    Computes the normalized Hamming distance, or the proportion of those vector elements between two n-vectors u and v which disagree. To save memory, the matrix X can be of type boolean.

  9. Y = cdist(XA, XB, 'jaccard')

    Computes the Jaccard distance between the points. Given two vectors, u and v, the Jaccard distance is the proportion of those elements u[i] and v[i] that disagree where at least one of them is non-zero.

  10. Y = cdist(XA, XB, 'chebyshev')

Computes the Chebyshev distance between the points. The Chebyshev distance between two n-vectors u and v is the maximum norm-1 distance between their respective elements. More precisely, the distance is given by

  1. Y = cdist(XA, XB, 'canberra')

Computes the Canberra distance between the points. The Canberra distance between two points u and v is

  1. Y = cdist(XA, XB, 'braycurtis')

Computes the Bray-Curtis distance between the points. The Bray-Curtis distance between two points u and v is

  1. Y = cdist(XA, XB, 'mahalanobis', VI=None)
Computes the Mahalanobis distance between the points. The Mahalanobis distance between two points u and v is where (the VI variable) is the inverse covariance. If VI is not None, VI will be used as the inverse covariance matrix.
  1. Y = cdist(XA, XB, 'yule')
Computes the Yule distance between the boolean vectors. (see yule function documentation)
  1. Y = cdist(XA, XB, 'matching')
Synonym for ‘hamming’.
  1. Y = cdist(XA, XB, 'dice')
Computes the Dice distance between the boolean vectors. (see dice function documentation)
  1. Y = cdist(XA, XB, 'kulsinski')
Computes the Kulsinski distance between the boolean vectors. (see kulsinski function documentation)
  1. Y = cdist(XA, XB, 'rogerstanimoto')
Computes the Rogers-Tanimoto distance between the boolean vectors. (see rogerstanimoto function documentation)
  1. Y = cdist(XA, XB, 'russellrao')
Computes the Russell-Rao distance between the boolean vectors. (see russellrao function documentation)
  1. Y = cdist(XA, XB, 'sokalmichener')
Computes the Sokal-Michener distance between the boolean vectors. (see sokalmichener function documentation)
  1. Y = cdist(XA, XB, 'sokalsneath')
Computes the Sokal-Sneath distance between the vectors. (see sokalsneath function documentation)
  1. Y = cdist(XA, XB, 'wminkowski', p=2., w=w)
Computes the weighted Minkowski distance between the vectors. (see wminkowski function documentation)
  1. Y = cdist(XA, XB, f)

Computes the distance between all pairs of vectors in X using the user supplied 2-arity function f. For example, Euclidean distance between the vectors could be computed as follows:

dm = cdist(XA, XB, lambda u, v: np.sqrt(((u-v)**2).sum()))

Note that you should avoid passing a reference to one of the distance functions defined in this library. For example,:

dm = cdist(XA, XB, sokalsneath)

would calculate the pair-wise distances between the vectors in X using the Python function sokalsneath. This would result in sokalsneath being called times, which is inefficient. Instead, the optimized C version is more efficient, and we call it using the following syntax:

dm = cdist(XA, XB, 'sokalsneath')

Find the Euclidean distances between four 2-D coordinates:

>>> from scipy.spatial import distance
>>> coords = [(35.0456, -85.2672),
...           (35.1174, -89.9711),
...           (35.9728, -83.9422),
...           (36.1667, -86.7833)]
>>> distance.cdist(coords, coords, 'euclidean')
array([[ 0.    ,  4.7044,  1.6172,  1.8856],
       [ 4.7044,  0.    ,  6.0893,  3.3561],
       [ 1.6172,  6.0893,  0.    ,  2.8477],
       [ 1.8856,  3.3561,  2.8477,  0.    ]])

Find the Manhattan distance from a 3-D point to the corners of the unit cube:

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