Skip to main content
Version: 0.5.x

distance

Utility functions for distance between arrays with numba decorators.

Module: aisp.utils.distance
Import: from aisp.utils import distance

Functions

hamming

@njit([(types.boolean[:], types.boolean[:])], cache=True)
def hamming(u: npt.NDArray[np.bool_], v: npt.NDArray[np.bool_]) -> float64:
...

Calculate the Hamming distance between two points.

(x1y1)+(x2y2)++(xnyn)n\frac{(x_1 \neq y_1) + (x_2 \neq y_2) + \cdots + (x_n \neq y_n)}{n}

Parameters

NameTypeDefaultDescription
unpt.NDArray[np.bool_]-Coordinates of the first point.
vnpt.NDArray[np.bool_]-Coordinates of the second point.

Returns

TypeDescription
float64Hamming distance between two points.

euclidean

@njit()
def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> float64:
...

Calculate the Euclidean distance between two points.

(X1X1)2+(Y2Y2)2++(YnYn)2\sqrt{(X_{1} - X_{1})^2 + (Y_{2} - Y_{2})^2 + \cdots + (Y_{n} - Y_{n})^2}

Parameters

NameTypeDefaultDescription
unpt.NDArray[float64]-Coordinates of the first point.
vnpt.NDArray[float64]-Coordinates of the second point.

Returns

TypeDescription
float64Euclidean distance between two points.

cityblock

@njit()
def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64:
...

Calculate the Manhattan distance between two points.

X1Y1+X2Y2++XnYn|X_{1} - Y_{1}| + |X_{2} - Y_{2}| + \cdots + |X_{n} - Y_{n}|

Parameters

NameTypeDefaultDescription
unpt.NDArray[float64]-Coordinates of the first point.
vnpt.NDArray[float64]-Coordinates of the second point.

Returns

TypeDescription
float64Manhattan distance between two points.

minkowski

@njit()
def minkowski(
u: npt.NDArray[float64],
v: npt.NDArray[float64],
p: float = 2.0
) -> float64:
...

Calculate the Minkowski distance between two points.

(X1Y1p+X2Y2p++XnYnp)1p(|X_{1} - Y_{1}|^p + |X_{2} - Y_{2}|^p + \cdots + |X_{n} - Y_{n}|^p)^\frac{1}{p}|

Parameters

NameTypeDefaultDescription
unpt.NDArray[float64]-Coordinates of the first point.
vnpt.NDArray[float64]-Coordinates of the second point.
pfloat2.0The p parameter defines the type of distance to be calculated.
Parameter p
  • p = 1: Manhattan distance - sum of absolute differences.
  • p = 2: Euclidean distance - sum of squared differences (square root).
  • p > 2: Minkowski distance with an increasing penalty as p increases.

Returns

TypeDescription
float64Minkowski distance between two points.

compute_metric_distance

@njit([(types.float64[:], types.float64[:], types.int32, types.float64)], cache=True)
def compute_metric_distance(
u: npt.NDArray[float64],
v: npt.NDArray[float64],
metric: int,
p: float = 2.0
) -> float64:
...

Calculate the distance between two points by the chosen metric.

Parameters

NameTypeDefaultDescription
unpt.NDArray[float64]-Coordinates of the first point.
vnpt.NDArray[float64]-Coordinates of the second point.
metricint-Distance metric to be used. Available options: 0 (Euclidean), 1 (Manhattan), 2 (Minkowski)
pfloat2.0The p parameter defines the type of distance to be calculated.

Returns

TypeDescription
float64Distance between the two points with the selected metric.

min_distance_to_class_vectors

@njit([(types.float64[:, :], types.float64[:], types.int32, types.float64)], cache=True)
def min_distance_to_class_vectors(
x_class: npt.NDArray[float64],
vector_x: npt.NDArray[float64],
metric: int,
p: float = 2.0,
) -> float:
...

Calculate the minimum distance between an input vector and the vectors of a class.

Parameters

NameTypeDefaultDescription
x_classnpt.NDArray[float64]-Array containing the class vectors to be compared with the input vector. Expected shape: (n_samples, n_features).
vector_xnpt.NDArray[float64]-Vector to be compared with the class vectors. Expected shape: (n_features,).
metricint-Distance metric to be used. Available options: 0 ("euclidean"), 1 ("manhattan"), 2 ("minkowski") or ("hamming")
pfloat2.0Parameter for the Minkowski distance (used only if metric is "minkowski").

Returns

TypeDescription
floatThe minimum distance calculated between the input vector and the class vectors. Returns -1.0 if the input dimensions are incompatible.

get_metric_code

def get_metric_code(metric: str) -> int:
...

Get the numeric code associated with a distance metric.

Parameters

NameTypeDefaultDescription
metricstr-Name of the metric. Can be "euclidean", "manhattan", "minkowski" or "hamming".

Returns

TypeDescription
intNumeric code corresponding to the metric.

Raises

ExceptionDescription
ValueErrorIf the metric provided is not supported.