scipy.spatial.distance.

euclidean#

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

Computes the Euclidean distance between two arrays.

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

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

Input array.

v(…, N) array_like

Input array.

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:
euclideanfloat or ndarray

The Euclidean distance between vectors u and v.

Notes

Array API Standard Support

euclidean 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.euclidean([1, 0, 0], [0, 1, 0])
1.4142135623730951
>>> distance.euclidean([1, 1, 0], [0, 1, 0])
1.0