COSINE_SIMILARITY
Returns the cosine similarity of two n-dimensional vectors. Cosine similarity is the cosine of the angle between two non-zero vectors. A returned value of 1 means that the vectors are identical, a value of 0 means the vectors are orthogonal, and a value of -1 means that the vectors are opposites.
Returns the cosine similarity of two n-dimensional vectors. Cosine similarity is the cosine of the angle between two non-zero vectors. A returned value of 1 means that the vectors are identical, a value of 0 means the vectors are orthogonal, and a value of -1 means that the vectors are opposites.
Behavior type
ImmutableSyntax
COSINE_SIMILARITY( vector_arr1, vector_arr2 )
Arguments
vector_arr1- N-dimensional vector, type ARRAY of INT, FLOAT, NUMERIC
vector_arr2- N-dimensional vector, type ARRAY of INT, FLOAT, NUMERIC
Note
NULL and NaN values are treated equivalently. If any element in the input array is NULL or NaN, the function returns NULL.For input vector arrays that have a norm of 0, the following logic is used:
- If both vectors are 0, then a value of 1 is returned as they are identical.
- If only one of the vectors is 0, then a value of 0 is returned as the vectors are orthogonal (zero similarity).
Returns
FLOAT
Examples
The following example computes the cosine similarity for two vector arrays:
SELECT id, vector_arr1, vector_arr2, COSINE_SIMILARITY(vector_arr1, vector_arr2) AS similarity FROM vectors_array;
id | vector_arr1 | vector_arr2 | similarity
----+---------------+---------------+-------------------
1 | [1.0,2.0,3.0] | [4.0,5.0,6.0] | 0.974631846197076
2 | [1.5,2.5,3.5] | [4.5,5.5,6.5] | 0.985871023540107
3 | [0.0,1.0,0.0] | [0.0,0.0,1.0] | 0
4 | [2.0,0.0,0.0] | [0.0,2.0,0.0] | 0
5 | [1.0,1.0,1.0] | [1.0,1.0,1.0] | 1
(5 rows)
The following example computes the cosine similarity for a 5D vector:
SELECT COSINE_SIMILARITY(Array[1.0, 2.0, 3.0, 4.0, 5.0], Array[1.0, 2.0, 3.0, 3.0, 4.0]);
COSINE_SIMILARITY
-------------------
0.99321708930132
(1 row)