cosine
First introduced in version: 3.00.5, 3.00.4.3
Syntax
cosine(X, Y)
Details
Compute the cosine similarity between X and Y. The cosine similarity between X and Y, is defined as (X · Y) / (||X|| × ||Y||).
Note:
Similar to the scipy.spatial.distance.cosine function in
SciPy, the main difference lies in the following:
- DolphinDB's
cosinefunction calculates cosine similarity (the larger the value, the more similar), while SciPy'sscipy.spatial.distance.cosinecalculates cosine distance (the smaller the value, the more similar). The relationship between them is: cosine distance = 1 - cosine similarity. cosineonly accepts parameters X and Y, and does not support the weight parameter w found inscipy.spatial.distance.cosine.
Parameters
X and Y are numeric vectors with the same number of elements.
Returns
A DOUBLE scalar.
Examples
x = [1, 2, 3]
y = [4, 5, 6]
cosine(x, y);
// output: 0.9746318
Related functions: euclidean, minkowski, seuclidean, mahalanobis
