scipy.spatial.distance.

minkowski#

scipy.spatial.distance.minkowski(u, v, p=2, w=None)[source]#

Compute the Minkowski distance between two arrays.

The Minkowski distance between arrays u and v, is defined as

\[ \begin{align}\begin{aligned}{\|u-v\|}_p = (\sum{|u_i - v_i|^p})^{1/p}.\\\left(\sum{w_i(|(u_i - v_i)|^p)}\right)^{1/p}.\end{aligned}\end{align} \]
Parameters:
u(…, N) array_like

Input array.

v(…, N) array_like

Input array.

pscalar

The order of the norm of the difference \({\|u-v\|}_p\). Note that for \(0 < p < 1\), the triangle inequality only holds with an additional multiplicative factor, i.e. it is only a quasi-metric.

w(N,) array_like, optional

The weights for each value in u and v. Default is None, which gives each value a weight of 1.0

Returns:
minkowskifloat or ndarray

The Minkowski distance between vectors u and v.

Notes

Array API Standard Support

minkowski has support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.

Library

CPU

GPU

NumPy

✅

n/a

CuPy

n/a

✅

PyTorch

✅

✅

JAX

✅

✅

Dask

✅

n/a

See Support for the array API standard for more information.

Examples

>>> from scipy.spatial import distance
>>> distance.minkowski([1, 0, 0], [0, 1, 0], 1)
2.0
>>> distance.minkowski([1, 0, 0], [0, 1, 0], 2)
1.4142135623730951
>>> distance.minkowski([1, 0, 0], [0, 1, 0], 3)
1.2599210498948732
>>> distance.minkowski([1, 1, 0], [0, 1, 0], 1)
1.0
>>> distance.minkowski([1, 1, 0], [0, 1, 0], 2)
1.0
>>> distance.minkowski([1, 1, 0], [0, 1, 0], 3)
1.0