pairwise_cosine_distance

pairwise_cosine_distance#

class braintools.metric.pairwise_cosine_distance(X, Y=None, eps=1e-08)#

Compute the pairwise cosine distance matrix between samples in X and Y.

The cosine distance is defined as 1 - cosine_similarity and therefore ranges from 0 (identical direction) to 2 (opposite direction).

Parameters:
  • X (Array | ndarray | bool | number | bool | int | float | complex | Quantity) – Input array with shape (n_samples_X, n_features). brainunit.Quantity inputs are accepted; the result is always dimensionless.

  • Y (Array | ndarray | bool | number | bool | int | float | complex | Quantity | None) – Input array with shape (n_samples_Y, n_features). If None, computes pairwise distances within X.

  • eps (float) – Lower bound applied to each row norm to avoid division by zero. Pairs involving a zero vector therefore yield a distance of 1.

Returns:

Cosine distance matrix:

  • If Y is provided: shape (n_samples_X, n_samples_Y).

  • If Y is None: shape (n_samples_X, n_samples_X).

Return type:

Array

See also

pairwise_cosine_similarity

The underlying pairwise similarity matrix.

cosine_distance

Element-wise cosine distance between paired samples.

Examples

>>> import jax.numpy as jnp
>>> import braintools
>>> X = jnp.array([[1., 0.], [0., 1.], [1., 1.]])
>>> braintools.metric.pairwise_cosine_distance(X).shape
(3, 3)