Manhattan (L1) distance function.

Functions

Func

manhattan_distance

Line 5
manhattan_distance(x1: np.ndarray, x2: np.ndarray) -> float

Compute Manhattan (L1) distance between two points.

The Manhattan distance is the sum of absolute differences of their coordinates. Also known as city block distance or taxicab distance.

Theory

d(x, y) = \sum_{i=1}^n |x_i - y_i|

This is a special case of the Minkowski distance with p = 1.

Parameters

x1
np.ndarray of shape (n_features,)
First point.
x2
np.ndarray of shape (n_features,)
Second point.

Returns

dist
float
Manhattan distance.

Notes

Complexity:

  • Time: O(n) where n is the number of features.

Compute distance between two 2D points:

python
>>> import numpy as np
>>> from tuiml.algorithms.clustering.distance import manhattan_distance
>>> x1 = np.array([0, 0])
>>> x2 = np.array([3, 4])
>>> manhattan_distance(x1, x2)
7.0
Func

manhattan_pairwise

Line 54
manhattan_pairwise(X: np.ndarray, Y: np.ndarray=None) -> np.ndarray

Compute pairwise Manhattan distances.

Parameters

X
np.ndarray of shape (n_samples_X, n_features)
First set of samples.
Y
np.ndarray of shape (n_samples_Y, n_features) = None
Second set of samples. Defaults to X.

Returns

dist_matrix
np.ndarray of shape (n_samples_X, n_samples_Y)
Distance matrix.