Distance
Utility functions for normalized distance between arrays with numba decorators.
def hamming(...)
def hamming(u: npt.NDArray, v: npt.NDArray) -> np.float64:
The function to calculate the normalized Hamming distance between two points.
Parameters:
- u (
npt.NDArray
): Coordinates of the first point. - v (
npt.NDArray
): Coordinates of the second point.
Returns:
- Distance (
float
) between the two points.
def euclidean(...)
def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> np.float64:
Function to calculate the normalized Euclidean distance between two points.
Parameters:
- u (
npt.NDArray
): Coordinates of the first point. - v (
npt.NDArray
): Coordinates of the second point.
Returns:
- Distance (
float
) between the two points.
def cityblock(...)
def cityblock(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> np.float64:
Function to calculate the normalized Manhattan distance between two points.
Parameters:
- u (
npt.NDArray
): Coordinates of the first point. - v (
npt.NDArray
): Coordinates of the second point.
Returns:
- Distance (
float
) between the two points.
def minkowski(...)
def minkowski(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64], p: float = 2.0):
Function to calculate the normalized Minkowski distance between two points.
Parameters:
- u (
npt.NDArray
): Coordinates of the first point. - v (
npt.NDArray
): Coordinates of the second point. - p float: The p parameter defines the type of distance to be calculated:
- 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:
- Distance (
float
) between the two points.
def compute_metric_distance(...)
def compute_metric_distance(
u: npt.NDArray[np.float64],
v: npt.NDArray[np.float64],
metric: int,
p: np.float64 = 2.0
) -> np.float64:
Function to calculate the distance between two points by the chosen metric
.
Parameters:
- u (
npt.NDArray
): Coordinates of the first point. - v (
npt.NDArray
): Coordinates of the second point. - metric (
int
): Distance metric to be used. Available options: [0 (Euclidean), 1 (Manhattan), 2 (Minkowski)] - p (
float
): Parameter for the Minkowski distance (used only ifmetric
is "minkowski").
Returns:
- Distance (
double
) between the two points with the selected metric.
def min_distance_to_class_vectors(...)
def min_distance_to_class_vectors(
x_class: npt.NDArray,
vector_x: npt.NDArray,
metric: int,
p: float = 2.0
) -> float:
Calculates the minimum distance between an input vector and the vectors of a class.
Parameters:
- x_class (
npt.NDArray
): Array containing the class vectors to be compared with the input vector. Expected shape: (n_samples, n_features). - vector_x (
npt.NDArray
): 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)] - p (
float
): Parameter for the Minkowski distance (used only ifmetric
is "minkowski").
Returns:
- float: The minimum distance calculated between the input vector and the class vectors.
- Returns -1.0 if the input dimensions are incompatible.
def get_metric_code(...)
def get_metric_code(metric: str) -> int:
Returns the numeric code associated with a distance metric.
Parameters:
- metric (str): Name of the metric. Can be "euclidean", "manhattan", "minkowski" or "hamming".
Raises
ValueError
: If the metric provided is not supported
Returns:
int
: Numeric code corresponding to the metric.