biosppy.metrics

biosppy.metrics

This module provides pairwise distance computation methods.

copyright:
  1. 2015-2026 by Instituto de Telecomunicacoes

license:

BSD 3-clause, see LICENSE for more details.

Functions

cdist(XA, XB[, metric])

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

pcosine(u, v)

Computes the Cosine distance (positive space) between 1-D arrays.

pdist(X[, metric])

Pairwise distances between observations in n-dimensional space.

squareform(X[, force, checks])

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

biosppy.metrics.cdist(XA, XB, metric='euclidean', **kwargs)[source]

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

Wraps scipy.spatial.distance.cdist.

Parameters:
  • XA (array) – An m_A by n array of m_A original observations in an n-dimensional space.

  • XB (array) – An m_B by n array of m_B original observations in an n-dimensional space.

  • metric (str, function, optional) – The distance metric to use; the distance can be ‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice’, ‘euclidean’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘pcosine’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’.

  • kwargs (Possible) –

    pfloat

    The p-norm to apply (for Minkowski, weighted and unweighted).

    warray

    The weight vector (for weighted Minkowski).

    Varray

    The variance vector (for standardized Euclidean).

    VIarray

    The inverse of the covariance matrix (for Mahalanobis).

Returns:

Y (array) – An m_A by m_B distance matrix is returned. For each i and j, the metric dist(u=XA[i], v=XB[j]) is computed and stored in the ij th entry.

biosppy.metrics.pcosine(u, v)[source]

Computes the Cosine distance (positive space) between 1-D arrays.

The Cosine distance (positive space) between u and v is defined as

d(u, v) = 1 - abs \left( \frac{u \cdot v}{||u||_2 ||v||_2} \right)

where u \cdot v is the dot product of u and v.

Parameters:
  • u (array) – Input array.

  • v (array) – Input array.

Returns:

cosine (float) – Cosine distance between u and v.

biosppy.metrics.pdist(X, metric='euclidean', **kwargs)[source]

Pairwise distances between observations in n-dimensional space.

Wraps scipy.spatial.distance.pdist.

Parameters:
  • X (array) – An m by n array of m original observations in an n-dimensional space.

  • metric (str, function, optional) – The distance metric to use; the distance can be ‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’, ‘cosine’, ‘dice’, ‘euclidean’, ‘hamming’, ‘jaccard’, ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘minkowski’, ‘pcosine’, ‘rogerstanimoto’, ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘sqeuclidean’, ‘yule’.

  • kwargs (Possible) –

    pfloat

    The p-norm to apply (for Minkowski, weighted and unweighted).

    warray

    The weight vector (for weighted Minkowski).

    Varray

    The variance vector (for standardized Euclidean).

    VIarray

    The inverse of the covariance matrix (for Mahalanobis).

Returns:

Y (array) – Returns a condensed distance matrix Y. For each i and j (where i<j<n), the metric dist(u=X[i], v=X[j]) is computed and stored in entry ij.

biosppy.metrics.squareform(X, force='no', checks=True)[source]

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

Wraps scipy.spatial.distance.squareform.

Parameters:
  • X (array) – 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 checks is 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.

Returns:

Y (array) – 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.