turicreate.toolkits.distances.cosine

turicreate.toolkits.distances.cosine(x, y)

Compute the cosine distance between between two dictionaries or two lists of equal length. Suppose x and y each contain \(d\) variables:

\[D(x, y) = 1 - \frac{\sum_i^d x_i y_i} {\sqrt{\sum_i^d x_i^2}\sqrt{\sum_i^d y_i^2}}\]
Parameters:
x : dict or list

First input vector.

y : dict or list

Second input vector.

Returns:
out : float

Cosine distance between x and y.

Notes

  • If the input vectors are in dictionary form, keys missing in one of the two dictionaries are assumed to have value 0.
  • Cosine distance is not a metric. This means the ball tree cannot be used to compute nearest neighbors based on this distance.

References

Examples

>>> tc.distances.cosine([1, 2, 3], [4, 5, 6])
0.025368153802923787
...
>>> tc.distances.cosine({'a': 2, 'c': 4}, {'b': 3, 'c': 12})
0.13227816872537534