Skip to content

Commit

Permalink
fixed function
Browse files Browse the repository at this point in the history
  • Loading branch information
rjvgupta committed Jul 12, 2024
1 parent 7b557c7 commit 668277b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions python-udfs/f_cosine_similarity(varchar,varchar)/function.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Purpose: This function use numpy to determine the similary between two vectors.
2021-08-08: written by rjvgupta
*/
--
CREATE OR REPLACE FUNCTION f_cosine_similarity (v1 VARCHAR, v2 VARCHAR)
RETURNS VARCHAR IMMUTABLE AS $$
import numpy as np
CREATE OR REPLACE FUNCTION f_cosine_similarity (v1 VARCHAR(MAX), v2 VARCHAR(MAX))
RETURNS FLOAT8 IMMUTABLE AS $$
import numpy,json
from numpy.linalg import norm
A = np.array(json.loads(v1))
B = np.array(json.loads(v2))
return np.dot(A,B)/(norm(A)*norm(B))
A = numpy.array(json.loads(v1))
B = numpy.array(json.loads(v2))
return numpy.dot(A,B)/(norm(A)*norm(B))
$$ LANGUAGE plpythonu;

0 comments on commit 668277b

Please sign in to comment.