Minkowski distance function.

Functions

Func

minkowski_distance

Line 5
minkowski_distance(x1: np.ndarray, x2: np.ndarray, p: float=2) -> float

Compute Minkowski distance between two points.

The Minkowski distance is a generalized metric that encompasses L_1, L_2, and L_\infty distances as special cases.

Theory

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

Special cases:

  • p=1: Manhattan distance
  • p=2: Euclidean distance
  • p \to \infty: Chebyshev distance

Parameters

x1
np.ndarray of shape (n_features,)
First point.
x2
np.ndarray of shape (n_features,)
Second point.
p
float = 2
Order of the norm.

Returns

dist
float
Minkowski distance.

Notes

Complexity:

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

Euclidean distance (p=2) and Manhattan distance (p=1):

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

minkowski_pairwise

Line 65
minkowski_pairwise(X: np.ndarray, Y: np.ndarray=None, p: float=2) -> np.ndarray

Compute pairwise Minkowski 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.
p
float = 2
Order of the norm.

Returns

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