SciPy

scipy.spatial.distance.cosine

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

Compute the Cosine distance between 1-D arrays.

The Cosine distance between u and v, is defined as

\[1 - \frac{u \cdot v} {||u||_2 ||v||_2}.\]

where \(u \cdot v\) is the dot product of \(u\) and \(v\).

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:

cosine : double

The Cosine distance between vectors u and v.

Examples

>>> from scipy.spatial import distance
>>> distance.cosine([1, 0, 0], [0, 1, 0])
1.0
>>> distance.cosine([100, 0, 0], [0, 1, 0])
1.0
>>> distance.cosine([1, 1, 0], [0, 1, 0])
0.29289321881345254