COSINE_SIMILARITY()
All functions > ARRAY > COSINE_SIMILARITY()
Calculates the cosine similarity between two arrays.
Signatures
Returns: Cosine similarity between -1 and 1
COSINE_SIMILARITY(array1: ARRAY<DOUBLE>, array2: ARRAY<DOUBLE>) → DOUBLE sql
| Parameter | Type | Required | Description |
|---|---|---|---|
array1 | ARRAY<DOUBLE> | Yes | First vector (array of doubles) |
array2 | ARRAY<DOUBLE> | Yes | Second vector (array of doubles) |
Notes
- Measures angle between two vectors
- Formula: dot(x,y) / (||x|| * ||y||)
- Returns value between -1 (opposite) and 1 (same direction)
- Arrays must be same length
- Used in text similarity and recommendation systems
See also
Examples
FeatureQL
SELECT
f1 := COSINE_SIMILARITY(ARRAY(1.0E0, 0.0E0), ARRAY(1.0E0, 0.0E0)), -- Identical vectors have similarity 1
f2 := COSINE_SIMILARITY(ARRAY(1.0E0, 0.0E0), ARRAY(0.0E0, 1.0E0)), -- Orthogonal vectors have similarity 0
f3 := COSINE_SIMILARITY(ARRAY(1.0E0, 0.0E0), ARRAY(-1.0E0, 0.0E0)) -- Opposite vectors have similarity -1
;Result
| f1 VARCHAR | f2 VARCHAR | f3 VARCHAR |
|---|---|---|
| 1.0 | 0.0 | -1.0 |
On this page