|
| 1 | +--- |
| 2 | +last_update: |
| 3 | + date: 2025/08/28 |
| 4 | + author: João Paulo |
| 5 | +--- |
| 6 | + |
| 7 | +# Distance |
| 8 | + |
| 9 | +Utility functions for normalized distance between arrays with numba decorators. |
| 10 | + |
| 11 | +## def hamming(...) |
| 12 | + |
| 13 | +```python |
| 14 | +def hamming(u: npt.NDArray, v: npt.NDArray) -> np.float64: |
| 15 | +``` |
| 16 | + |
| 17 | +The function to calculate the normalized Hamming distance between two points. |
| 18 | + |
| 19 | +$((x₁ ≠ x₂) + (y₁ ≠ y₂) + ... + (yn ≠ yn)) / n$ |
| 20 | + |
| 21 | + |
| 22 | +**Parameters:** |
| 23 | +* u (``npt.NDArray``): Coordinates of the first point. |
| 24 | +* v (``npt.NDArray``): Coordinates of the second point. |
| 25 | + |
| 26 | +**Returns:** |
| 27 | +* Distance (``float``) between the two points. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## def euclidean(...) |
| 32 | + |
| 33 | +```python |
| 34 | +def euclidean(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> np.float64: |
| 35 | +``` |
| 36 | + |
| 37 | +Function to calculate the normalized Euclidean distance between two points. |
| 38 | + |
| 39 | +$√( (x₁ – x₂)² + (y₁ – y₂)² + ... + (yn – yn)²)$ |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +**Parameters:** |
| 44 | +* u (``npt.NDArray``): Coordinates of the first point. |
| 45 | +* v (``npt.NDArray``): Coordinates of the second point. |
| 46 | + |
| 47 | +**Returns:** |
| 48 | +* Distance (``float``) between the two points. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## def cityblock(...) |
| 53 | + |
| 54 | +```python |
| 55 | +def cityblock(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64]) -> np.float64: |
| 56 | +``` |
| 57 | + |
| 58 | +Function to calculate the normalized Manhattan distance between two points. |
| 59 | + |
| 60 | +$(|x₁ – x₂| + |y₁ – y₂| + ... + |yn – yn|) / n$ |
| 61 | + |
| 62 | + |
| 63 | +**Parameters:** |
| 64 | +* u (``npt.NDArray``): Coordinates of the first point. |
| 65 | +* v (``npt.NDArray``): Coordinates of the second point. |
| 66 | + |
| 67 | +**Returns:** |
| 68 | +* Distance (``float``) between the two points. |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## def minkowski(...) |
| 73 | + |
| 74 | +```python |
| 75 | +def minkowski(u: npt.NDArray[np.float64], v: npt.NDArray[np.float64], p: float = 2.0): |
| 76 | +``` |
| 77 | + |
| 78 | +Function to calculate the normalized Minkowski distance between two points. |
| 79 | + |
| 80 | +$(( |X₁ – Y₁|p + |X₂ – Y₂|p + ... + |Xn – Yn|p) ¹/ₚ) / n$ |
| 81 | + |
| 82 | + |
| 83 | +**Parameters:** |
| 84 | +* u (``npt.NDArray``): Coordinates of the first point. |
| 85 | +* v (``npt.NDArray``): Coordinates of the second point. |
| 86 | +* p float: The p parameter defines the type of distance to be calculated: |
| 87 | + - p = 1: **Manhattan** distance — sum of absolute differences. |
| 88 | + - p = 2: **Euclidean** distance — sum of squared differences (square root). |
| 89 | + - p > 2: **Minkowski** distance with an increasing penalty as p increases. |
| 90 | + |
| 91 | +**Returns:** |
| 92 | +* Distance (``float``) between the two points. |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## def compute_metric_distance(...) |
| 97 | + |
| 98 | +```python |
| 99 | +def compute_metric_distance( |
| 100 | + u: npt.NDArray[np.float64], |
| 101 | + v: npt.NDArray[np.float64], |
| 102 | + metric: int, |
| 103 | + p: np.float64 = 2.0 |
| 104 | +) -> np.float64: |
| 105 | +``` |
| 106 | + |
| 107 | +Function to calculate the distance between two points by the chosen ``metric``. |
| 108 | + |
| 109 | +**Parameters:** |
| 110 | +* u (``npt.NDArray``): Coordinates of the first point. |
| 111 | +* v (``npt.NDArray``): Coordinates of the second point. |
| 112 | +* metric (``int``): Distance metric to be used. Available options: [0 (Euclidean), 1 (Manhattan), 2 (Minkowski)] |
| 113 | +* p (``float``): Parameter for the Minkowski distance (used only if `metric` is "minkowski"). |
| 114 | + |
| 115 | +**Returns:** |
| 116 | +* Distance (``double``) between the two points with the selected metric. |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## def min_distance_to_class_vectors(...) |
| 121 | + |
| 122 | +```python |
| 123 | +def min_distance_to_class_vectors( |
| 124 | + x_class: npt.NDArray, |
| 125 | + vector_x: npt.NDArray, |
| 126 | + metric: int, |
| 127 | + p: float = 2.0 |
| 128 | +) -> float: |
| 129 | +``` |
| 130 | + |
| 131 | +Calculates the minimum distance between an input vector and the vectors of a class. |
| 132 | + |
| 133 | + |
| 134 | +**Parameters:** |
| 135 | +* x_class (``npt.NDArray``): Array containing the class vectors to be compared with the input vector. Expected shape: (n_samples, n_features). |
| 136 | +* vector_x (``npt.NDArray``): Vector to be compared with the class vectors. Expected shape: (n_features,). |
| 137 | +* metric (``int``): Distance metric to be used. Available options: [0 (Euclidean), 1 (Manhattan), 2 (Minkowski)] |
| 138 | +* p (``float``): Parameter for the Minkowski distance (used only if `metric` is "minkowski"). |
| 139 | + |
| 140 | +**Returns:** |
| 141 | +* float: The minimum distance calculated between the input vector and the class vectors. |
| 142 | +* Returns -1.0 if the input dimensions are incompatible. |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +## def get_metric_code(...) |
| 147 | + |
| 148 | +```python |
| 149 | +def get_metric_code(metric: str) -> int: |
| 150 | +``` |
| 151 | +Returns the numeric code associated with a distance metric. |
| 152 | + |
| 153 | +**Parameters:** |
| 154 | +* metric (str): Name of the metric. Can be "euclidean", "manhattan", "minkowski" or "hamming". |
| 155 | + |
| 156 | +**Raises** |
| 157 | +---------- |
| 158 | +* ``ValueError``: If the metric provided is not supported |
| 159 | + |
| 160 | +**Returns:** |
| 161 | +* ``int``: Numeric code corresponding to the metric. |
| 162 | + |
| 163 | +--- |
0 commit comments