cosine_similarity: Cosine similarity between two vectors

Description Usage Arguments Details Value References See Also

View source: R/nearest_point.R

Description

This function computes the cosine similarity between two vectors, defined by

cos(θ) = (x . y) / (|x| |y|).

Usage

1

Arguments

x

A numeric vector

y

A numeric vector, the same length as x

Details

Cosine similarity is related to Euclidean distance by

|x - y|^2 = 2(1 - cos(θ)).

So

cos(θ) = 1 - 0.5 * |x - y|^2,

assuming x and y have been normalised to be unit vectors. Therefore, if we want to maximise cosine similarity, we can minimise Euclidean distance and then make the conversion. See nearest_point().

Value

Generally, a scalar between -1 and 1. Or, if x and y are non-negative, a value between 0 and 1.

References

https://en.wikipedia.org/wiki/Cosine_similarity

See Also

nearest_cosine(), cos(), acos()


Selbosh/scrooge documentation built on May 5, 2019, 8 p.m.