skspatial.objects.Vector.cosine_similarity

Vector.cosine_similarity(other: Union[numpy.ndarray, Sequence]) numpy.float64[source]

Return the cosine similarity of the vector with another.

This is the cosine of the angle between the vectors.

Parameters
otherarray_like

Other vector.

Returns
np.float64

Cosine similarity.

Raises
ValueError

If either vector has a magnitude of zero.

Examples

>>> from skspatial.objects import Vector
>>> Vector([1, 0]).cosine_similarity([0, 1])
0.0
>>> Vector([30, 0]).cosine_similarity([0, 20])
0.0
>>> Vector([1, 0]).cosine_similarity([-1, 0])
-1.0
>>> Vector([1, 0]).cosine_similarity([1, 1]).round(3)
0.707
>>> Vector([0, 0]).cosine_similarity([1, 1])
Traceback (most recent call last):
...
ValueError: The vectors must have non-zero magnitudes.