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.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
u | npt.NDArray[np.bool_] | - | Coordinates of the first point. |
v | npt.NDArray[np.bool_] | - | Coordinates of the second point. |
Returns
| Type | Description |
|---|---|
float64 | Hamming 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.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
u | npt.NDArray[float64] | - | Coordinates of the first point. |
v | npt.NDArray[float64] | - | Coordinates of the second point. |
Returns
| Type | Description |
|---|---|
float64 | Euclidean distance between two points. |
cityblock
@njit()
def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64:
...
Calculate the Manhattan distance between two points.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
u | npt.NDArray[float64] | - | Coordinates of the first point. |
v | npt.NDArray[float64] | - | Coordinates of the second point. |
Returns
| Type | Description |
|---|---|
float64 | Manhattan 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.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
u | npt.NDArray[float64] | - | Coordinates of the first point. |
v | npt.NDArray[float64] | - | Coordinates of the second point. |
p | float | 2.0 | The 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
| Type | Description |
|---|---|
float64 | Minkowski 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
| Name | Type | Default | Description |
|---|---|---|---|
u | npt.NDArray[float64] | - | Coordinates of the first point. |
v | npt.NDArray[float64] | - | Coordinates of the second point. |
metric | int | - | Distance metric to be used. Available options: 0 (Euclidean), 1 (Manhattan), 2 (Minkowski) |
p | float | 2.0 | The p parameter defines the type of distance to be calculated. |
Returns
| Type | Description |
|---|---|
float64 | Distance 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
| Name | Type | Default | Description |
|---|---|---|---|
x_class | npt.NDArray[float64] | - | Array containing the class vectors to be compared with the input vector. Expected shape: (n_samples, n_features). |
vector_x | npt.NDArray[float64] | - | Vector to be compared with the class vectors. Expected shape: (n_features,). |
metric | int | - | Distance metric to be used. Available options: 0 ("euclidean"), 1 ("manhattan"), 2 ("minkowski") or ("hamming") |
p | float | 2.0 | Parameter for the Minkowski distance (used only if metric is "minkowski"). |
Returns
| Type | Description |
|---|---|
float | The 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
| Name | Type | Default | Description |
|---|---|---|---|
metric | str | - | Name of the metric. Can be "euclidean", "manhattan", "minkowski" or "hamming". |
Returns
| Type | Description |
|---|---|
int | Numeric code corresponding to the metric. |
Raises
| Exception | Description |
|---|---|
ValueError | If the metric provided is not supported. |