scipy.spatial.distance.

canberra#

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

Compute the Canberra distance between two arrays.

The Canberra distance is defined as

\[d(u,v) = \sum_i \frac{|u_i-v_i|} {|u_i|+|v_i|}.\]
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:
canberrafloat or array

The Canberra distance between vectors u and v.

Notes

When u[i] and v[i] are 0 for given i, then the fraction 0/0 = 0 is used in the calculation.

Array API Standard Support

canberra 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 Array API coverage for more information.

Examples

>>> from scipy.spatial import distance
>>> distance.canberra([1, 0, 0], [0, 1, 0])
2.0
>>> distance.canberra([1, 1, 0], [0, 1, 0])
1.0