Neo4j中根据节点属性的Jaccard相似度创建节点之间的关系?

问题描述 投票:0回答:1

我的 Neo4j 图中有多个节点。 我想在任意 2 个节点之间创建关系,当且仅当它们属性上的 Jaccard 相似度高于某个阈值 alpha。

考虑 2 个节点:

Node 1: {id:1, abc: 1.1, eww: -9.4, ssv: "likj"}
Node 2: {id:2, we2: 1, eww: 900}
Node 3: {id:3, kuku: -91, lulu: 383, ssv: "bubu"}

因此 Node1 和 Node2 Jaccard 在属性上的相似度为: (交集 =)2/(并集 =)5 = 0.4

如何在 Neo4j 中执行此操作?我知道有一个 Jaccard 相似度函数,但是如何配置它来处理节点的属性?

neo4j cypher neo4j-apoc
1个回答
1
投票

假设您的意思是属性存在的杰卡德相似度,那么您可以这样做

MATCH (a:Node)
MATCH (b:Node) WHERE id(b) > id(a)
WITH a, b, [prop IN keys(a) WHERE prop IN keys(b)] AS shared_properties // Find the properties that exist on both nodes using the IN operator
WITH a, b, size(shared_properties) AS shared_property_count // Get the number of shared properties 
WITH 1.0*shared_property_count / size(apoc.coll.union(keys(a), keys(b))) AS jaccard_similarity, a, b // Compute the Jaccard similarity as the intersection over the union
WHERE jaccard_similarity > $threshold // Make sure the similarity is higher than some threshold
CREATE (a)-[:SIMILAR_TO {jaccard: jaccard_similarity}]->(b) 

WITH
语句找到两个节点上都存在的属性并对它们进行计数,最后我们找到 Jaccard 相似度。

© www.soinside.com 2019 - 2024. All rights reserved.