Skip to content

Instantly share code, notes, and snippets.

@LeonardoMarrancone
Last active July 14, 2020 16:06
Show Gist options
  • Save LeonardoMarrancone/ceffa166a0480ea85f6fe85519fd5015 to your computer and use it in GitHub Desktop.
Save LeonardoMarrancone/ceffa166a0480ea85f6fe85519fd5015 to your computer and use it in GitHub Desktop.
cosine similarity with rating
MATCH (p1:User {name: "Leonardo", surname:"Marrancone"})-[x:WATCH]->(m:Show)<-[y:WATCH]-(p2:User)
WITH COUNT(m) AS numbershows, SUM(x.rating * y.rating) AS xyDotProduct,
SQRT(REDUCE(xDot = 0.0, a IN COLLECT(x.rating) | xDot + a^2)) AS xLength,
SQRT(REDUCE(yDot = 0.0, b IN COLLECT(y.rating) | yDot + b^2)) AS yLength,
p1, p2
RETURN p1.name as SelectedUser, p2.name as SimilarUser, xyDotProduct / (xLength * yLength) AS sim
ORDER BY sim DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment