简体   繁体   English

计算所有节点neo4j之间的相似度-为节点对获取不同的值

[英]Computing similarity between all nodes neo4j - getting different values for a node pair

I have two kinds of nodes in my database: 我的数据库中有两种节点:

  1. USER 用户
  2. MEDIA 媒体

And one relationship - "LIKES" 还有一种关系-“喜欢”

The relationship between the two nodes is described like so: 两个节点之间的关系描述如下:

(:USER)-[:LIKES]->(:MEDIA) (:USER)-[:LIKES]->(:MEDIA)

I'm trying to compute the similarity between all the "USER" nodes based on the number of media shared between each node pair (Jaccard Similarity) 我正在尝试根据每个节点对之间共享的媒体数来计算所有“ USER”节点之间的相似度(Jaccard相似度)

This similarity is then stored as a "ISSIMILAR" relationship. 然后将这种相似性存储为“ ISSIMILAR”关系。 The "ISSIMILAR" relationship has an attribute called "similarity" which stores the similarity between nodes “ ISSIMILAR”关系具有一个称为“相似性”的属性,该属性存储节点之间的相似性

Here's my query: 这是我的查询:

Match(u:User)

WITH COLLECT(u) as users

UNWIND users as user

MATCH(user:User{id:user.id})-[:LIKES]->(common_media:Media)<-[:LIKES]-(other:User)

WITH user,other,count(common_media) AS intersection, COLLECT(common_media.name) as i

MATCH(user)-[:LIKES]->(user_media:Media)

WITH user,other,intersection,i, COLLECT(user_media.name) AS s1

MATCH(other)-[:LIKES]->(other_media:Media)

WITH user,other,intersection,i,s1, COLLECT(other_media.name) AS s2

WITH user,other,intersection,s1,s2

WITH user,other,intersection,s1+filter(x IN s2 WHERE NOT x IN s1) AS union, s1,s2

WITH ((1.0*intersection)/SIZE(union)) as jaccard,user,other

MERGE(user)-[:ISSIMILAR{similarity:jaccard}]-(other)

Running this query, I have two issues: 运行此查询,我有两个问题:

  1. I expect only one "ISSIMILAR" relationship between a pair of nodes. 我希望一对节点之间只有一个“ ISSIMILAR”关系。 But it creates two. 但是它创造了两个。
  2. This "ISSIMILAR" relationship "similar" attributes have different values.The values should be the same 此“ ISSIMILAR”关系的“相似”属性具有不同的值。这些值应相同

Here's a visualization of the issue: 这是问题的可视化:

MATCH(user:User)-[r]-(o:User) return o,user,r limit 4

在此处输入图片说明

在此处输入图片说明

Thanks in advance 提前致谢

Problems with two similarity relationships arise because you do not exclude the previously constructed similarity relations. 出现两个相似关系的问题是因为您不排除先前构造的相似关系。 You can avoid this by doing: 您可以通过执行以下操作来避免这种情况:

...
UNWIND users as user
  UNWIND users as other 
    WITH user, other WHERE ID(user) > ID(other)
    MATCH(user)-[:LIKES]->(common_media:Media)<-[:LIKES]-(other) 
...

And the final query can be made more clear: 最后的查询可以变得更加清晰:

MATCH (u:User) WITH COLLECT(u) AS users
UNWIND users AS user
UNWIND users AS other

MATCH (user)-[:LIKES]->(common_media:Media)<-[:LIKES]-(other) WHERE ID(other) > ID(user)
WITH user, other, COLLECT(common_media) AS intersection

MATCH (user)-[:LIKES]->(user_media:Media)
WITH user, other, intersection, 
     COLLECT(user_media) AS s1

MATCH (other)-[:LIKES]->(other_media:Media)
WITH user,other,intersection, s1, 
     COLLECT(other_media) AS s2

RETURN user, other,
       (1.0 * SIZE(intersection)) / (SIZE(s1) + SIZE(s2) - SIZE(intersection)) AS jaccard

MERGE (user)-[:ISSIMILAR {similarity: jaccard}]->(other)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算Neo4j中节点之间的相似性 - Compute Similarity between nodes in Neo4j Neo4j-需要聚合同一对节点之间不同关系上的值 - Neo4j - Need to aggregate values on different relationships between same pair of nodes 在Neo4j浏览器中将所有节点连接到给定节点,导致Neo4j崩溃 - Getting all nodes connected to the given node in Neo4j browser causing neo4j crash Neo4J深度(如果两个节点之间有一个节点) - Neo4J Depth if there a node between 2 nodes 如何查找特定 label 的单个节点与 neo4j 中不同 label 中的所有其他节点之间的不同节点的计数? - How to find the count of distinct nodes between a single node of particular label to all other nodes in a different label in neo4j? 如何在neo4j中从起始节点获取所有可到达的节点(以及它们之间的所有关系?) - how to get all reachable nodes from a starting node (and optionally all relationships between them?) in neo4j Neo4j 图算法/节点相似度 - Neo4j graph algorithm / Node similarity Neo4j 密码查询获取开始和结束节点之间的所有节点,包括 - Neo4j cypher query get all nodes between a start and end node including 根据Neo4j中节点属性的Jaccard相似度创建节点之间的关系? - Create relationship between nodes based on Jaccard similarity of the nodes attributes in Neo4j? Neo4j密码使用ALL关系获取与另一个节点相关的节点 - Neo4j cypher getting nodes that are related to another node using ALL relations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM