Skip to content

Metric

metametric.core.metric.Metric

Bases: Generic[T]

The basic metric interface.

Here a metric is defined as a function \(\phi: T \times T \to \mathbb{R}_{\ge 0}\) that takes two objects and returns a non-negative number that quantifies their similarity. It follows the common usage in machine learning and NLP literature, as in the phrase "evaluation metrics". This is not the metric in the mathematical sense, where it is a generalization of distances.

contramap(f)

Returns a new metric \(\phi^\prime\) by first preprocessing the objects by a given function \(f: S \to T\).

\[ \phi^\prime(x, y) = \phi(f(x), f(y)) \]

Parameters:

Name Type Description Default
f Callable[[S], T]

A preprocessing function.

required

Returns:

Type Description
Metric[S]

A new metric \(\phi^\prime\).

from_function(f) staticmethod

Create a metric from a function \(f: T \times T \to \mathbb{R}_{\ge 0}\).

Parameters:

Name Type Description Default
f `Callable[[T, T], float]`

A function that takes two objects and returns a float. This is the function that derives the metric.

required

Returns:

Type Description
Metric[T]

Metric[T]: A metric that uses the function to score two objects.

gram_matrix(xs, ys)

Computes the Gram matrix of the metric given two collections of objects.

Parameters:

Name Type Description Default
xs Collection[T]

A collection of objects \(\{x_1, \ldots, x_n\}\).

required
ys Collection[T]

A collection of objects \(\{y_1, \ldots, y_m\}\).

required

Returns:

Type Description
ndarray

A Gram matrix \(G\) where \(G = \begin{bmatrix} \phi(x_1, y_1) & \cdots & \phi(x_1, y_m) \\ \vdots & \ddots & \vdots \\ \phi(x_n, y_1) & \cdots & \phi(x_n, y_m) \end{bmatrix}\).

score(x, y) abstractmethod

Scores two objects using this metric: \(\phi(x, y)\).

score_self(x)

Scores an object against itself: \(\phi(x, x)\).

In many cases there is a faster way to compute this than the general pair case. In such cases, please override this function.