diff --git a/aisp/utils/distance.py b/aisp/utils/distance.py index ec98b2b..078eae2 100644 --- a/aisp/utils/distance.py +++ b/aisp/utils/distance.py @@ -1,4 +1,4 @@ -"""Utility functions for normalized distance between arrays with numba decorators.""" +"""Utility functions for distance between arrays with numba decorators.""" import numpy as np import numpy.typing as npt @@ -61,10 +61,10 @@ def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> float64 @njit() def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64: - r"""Calculate the normalized Manhattan distance between two points. + r"""Calculate the Manhattan distance between two points. .. math:: - \frac{(|X_{1} - Y_{1}| + |X_{2} - Y_{2}| + \cdots + |X_{n} - Y_{n}|)}{n} + |X_{1} - Y_{1}| + |X_{2} - Y_{2}| + \cdots + |X_{n} - Y_{n}| Parameters ---------- @@ -76,13 +76,13 @@ def cityblock(u: npt.NDArray[float64], v: npt.NDArray[float64]) -> float64: Returns ------- float64 - Normalized Manhattan distance between two points. + Manhattan distance between two points. """ n = len(u) if n == 0: return float64(-1.0) - return float64(np.sum(np.abs(u - v)) / n) + return float64(np.sum(np.abs(u - v))) @njit() @@ -91,12 +91,10 @@ def minkowski( v: npt.NDArray[float64], p: float = 2.0 ) -> float64: - r"""Calculate the normalized Minkowski distance between two points. + r"""Calculate the Minkowski distance between two points. .. math:: - \frac{ - ((|X_{1} - Y_{1}|^p + |X_{2} - Y_{2}|^p + \cdots + |X_{n} - Y_{n}|^p)^\frac{1}{p}) - }{n} + (|X_{1} - Y_{1}|^p + |X_{2} - Y_{2}|^p + \cdots + |X_{n} - Y_{n}|^p)^\frac{1}{p} Parameters ---------- @@ -114,13 +112,13 @@ def minkowski( Returns ------- float64 - Normalized Minkowski distance between two points. + Minkowski distance between two points. """ n = len(u) if n == 0: return float64(-1.0) - return float64((np.sum(np.abs(u - v) ** p) ** (1 / p)) / n) + return float64(np.sum(np.abs(u - v) ** p) ** (1 / p)) @njit([(types.float64[:], types.float64[:], types.int32, types.float64)], cache=True) diff --git a/aisp/utils/tests/test_distance.py b/aisp/utils/tests/test_distance.py index cf8b878..db1e222 100644 --- a/aisp/utils/tests/test_distance.py +++ b/aisp/utils/tests/test_distance.py @@ -51,8 +51,8 @@ def test_hamming(u, v, expected_output): @pytest.mark.parametrize( "u, v, expected_output", [ - (np.array([1, 2, 3]), np.array([4, 5, 6]), 3), - (np.array([0, 0]), np.array([3, 4]), 3.5), + (np.array([1, 2, 3]), np.array([4, 5, 6]), 9), + (np.array([0, 0]), np.array([3, 4]), 7), ], ids=[ "Manhattan Distance - 3 dimensions", @@ -68,9 +68,9 @@ def test_cityblock(u, v, expected_output): @pytest.mark.parametrize( "u, v, p, expected_output", [ - (np.array([0, 0]), np.array([3, 4]), 1, 3.5), - (np.array([0, 0]), np.array([3, 4]), 2, 2.5), - (np.array([1, 2, 3]), np.array([4, 5, 6]), 3, 1.442), + (np.array([0, 0]), np.array([3, 4]), 1, 7), + (np.array([0.0, 0.0]), np.array([3.0, 4.0]), 2, 5), + (np.array([1, 2, 3]), np.array([4, 5, 6]), 3, 4.327), ], ids=[ "Minkowski p=1 - Manhattan",