0
votes

I have a Neo4j database with n- users. Each user is connected to their respective gender node(M and F), Age group node , Ethnicity node etc. I want to find similarity between two users based on their Gender, Age, Ethnicity etc.

This cypher query is calculating based only one attribute Movie

MATCH (p1:Person)-[x:RATED]->(m:Movie)<-[y:RATED]-(p2:Person)
WITH  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
MERGE (p1)-[s:SIMILARITY]-(p2)
SET   s.similarity = xyDotProduct / (xLength * yLength) 

I want to calculate based on multiple attributes like Gender, AgeGroup, Ethnicity, etc

1

1 Answers

0
votes

There is an APOC library for neo4j link here, which does that for you. Example for two features:

MATCH (p1:Person),(p2:Person) 
WITH p1,p2,apoc.algo.cosineSimilarity([p1.count,p1.age_nor],[p2.count,p2.age_nor]) as value
MERGE (p1)-[s:SIMILARITY]-(p2)
SET s.cosine = value

You can also check this blog post for more information.