This is documentation for an old release of SciPy (version 1.8.0). Read this page in the documentation of the latest stable release (version 1.14.1).

Distance computations (scipy.spatial.distance)#

Function reference#

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

pdist(X[, metric, out])

Pairwise distances between observations in n-dimensional space.

cdist(XA, XB[, metric, out])

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

squareform(X[, force, checks])

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

directed_hausdorff(u, v[, seed])

Compute the directed Hausdorff distance between two 2-D arrays.

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(D[, tol, throw, name, warning])

Return True if input array is a valid distance matrix.

is_valid_y(y[, warning, throw, name])

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

num_obs_dm(d)

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

num_obs_y(Y)

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

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(u, v[, w])

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

canberra(u, v[, w])

Compute the Canberra distance between two 1-D arrays.

chebyshev(u, v[, w])

Compute the Chebyshev distance.

cityblock(u, v[, w])

Compute the City Block (Manhattan) distance.

correlation(u, v[, w, centered])

Compute the correlation distance between two 1-D arrays.

cosine(u, v[, w])

Compute the Cosine distance between 1-D arrays.

euclidean(u, v[, w])

Computes the Euclidean distance between two 1-D arrays.

jensenshannon(p, q[, base, axis, keepdims])

Compute the Jensen-Shannon distance (metric) between two probability arrays.

mahalanobis(u, v, VI)

Compute the Mahalanobis distance between two 1-D arrays.

minkowski(u, v[, p, w])

Compute the Minkowski distance between two 1-D arrays.

seuclidean(u, v, V)

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

sqeuclidean(u, v[, w])

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

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(u, v[, w])

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

hamming(u, v[, w])

Compute the Hamming distance between two 1-D arrays.

jaccard(u, v[, w])

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

kulsinski(u, v[, w])

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

kulczynski1(u, v, *[, w])

Compute the Kulczynski 1 dissimilarity between two boolean 1-D arrays.

rogerstanimoto(u, v[, w])

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

russellrao(u, v[, w])

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

sokalmichener(u, v[, w])

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

sokalsneath(u, v[, w])

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

yule(u, v[, w])

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

hamming also operates over discrete numerical vectors.