✓ 文件回写 ✕ 属性回写 ✓ 直接返回 ✓ 流式返回 ✕ 统计值
概述
在余弦相似度(Cosine Similarity)中,数据对象被视为矢量,它使用两个矢量之间夹角的余弦值来表示它们之间的相似性。在图中,指定节点的 N 个数值属性(特征)构成 N 维矢量,如果两个节点的矢量相似,则认为它们相似。
余弦相似度的取值范围 -1 到 1;1 意味着两个向量的方向完全一致,-1 意味着两个向量的方向正好相反。
在二维空间中,两个向量 A(a1, a2 和 B(b1, b2) 的余弦相似度计算公式为:
在三维空间中,两个向量 A(a1, a2, a3) 和 B(b1, b2, b3) 的余弦相似度计算公式为:
下图展示了 2D 和 3D 空间中矢量 A 和 B 之间的关系,以及它们之间夹角 θ:
推广到 N 维空间,余弦相似度的计算公式如下:
特殊说明
- 两个节点的余弦相似度理论上不依赖它们之间的连通性。
- 余弦相似度的值与向量长度无关,仅与向量方向相关。
语法
- 命令:
algo(similarity)
- 参数:
名称 |
类型 |
规范 |
默认 |
可选 |
描述 |
---|---|---|---|---|---|
ids / uuids | []_id / []_uuid |
/ | / | 否 | 待计算的第一组节点的 ID / UUID |
ids2 / uuids2 | []_id / []_uuid |
/ | / | 是 | 待计算的第二组节点的 ID / UUID |
type | string | cosine |
cosine |
是 | 相似度衡量指标;计算余弦相似度时,保持此项为 cosine |
node_schema_property | []@<schema>?.<property> |
数值类型,需 LTE | / | 否 | 指定至少两个点属性来构成向量 |
limit | int | >=-1 | -1 |
是 | 返回的结果条数,-1 返回所有结果 |
top_limit | int | >=-1 | -1 |
是 | 在选拔模式下,限制 ids /uuids 中每个节点返回的最大结果条数,-1 返回所有相似度大于 0 的结果;在配对模式下,此参数无效 |
本算法有两种计算模式:
- 配对:同时配置
ids
/uuids
和ids2
/uuids2
时,将ids
/uuids
中的每个节点分别与ids2
/uuids2
中的每个节点配对(忽略相同节点),逐对计算相似度。 - 选拔:仅配置
ids
/uuids
时,对于其中的每个节点,计算其与图中其他所有节点的相似度,返回所有或限定个数的与其相似度大于 0 的结果,并按相似度降序排列。
示例
示例图包含 4 个产品(忽略边),每个产品包含 price、weight、width 和 height 属性:
文件回写
配置项 | 回写内容 |
---|---|
filename | node1 ,node2 ,similarity |
algo(similarity).params({
uuids: [1],
uuids2: [2,3,4],
node_schema_property: ['price', 'weight', 'width', 'height']
}).write({
file:{
filename: 'cs_result'
}
})
结果:文件 cs_result
product1,product2,0.986529
product1,product3,0.878858
product1,product4,0.816876
algo(similarity).params({
uuids: [1,2,3,4],
node_schema_property: ['price', 'weight', 'width', 'height'],
type: 'cosine'
}).write({
file:{
filename: 'list'
}
})
结果:文件 list
product1,product2,0.986529
product1,product3,0.878858
product1,product4,0.816876
product2,product1,0.986529
product2,product3,0.934217
product2,product4,0.881988
product3,product2,0.934217
product3,product4,0.930153
product3,product1,0.878858
product4,product3,0.930153
product4,product2,0.881988
product4,product1,0.816876
直接返回
别名序号 |
类型 |
描述 |
列名 |
---|---|---|---|
0 | []perNodePair | 各点对及相似度 | node1 , node2 , similarity |
algo(similarity).params({
uuids: [1,2],
uuids2: [2,3,4],
node_schema_property: ['price', 'weight', 'width', 'height'],
type: 'cosine'
}) as cs
return cs
结果:cs
node1 | node2 | similarity |
---|---|---|
1 | 2 | 0.986529413529119 |
1 | 3 | 0.878858407519654 |
1 | 4 | 0.816876150267203 |
2 | 3 | 0.934216530725663 |
2 | 4 | 0.88198819302226 |
algo(similarity).params({
uuids: [1,2],
type: 'cosine',
node_schema_property: ['price', 'weight', 'width', 'height'],
top_limit: 1
}) as top
return top
结果:top
node1 | node2 | similarity |
---|---|---|
1 | 2 | 0.986529413529119 |
2 | 1 | 0.986529413529119 |
流式返回
别名序号 |
类型 |
描述 |
列名 |
---|---|---|---|
0 | []perNodePair | 各点对及相似度 | node1 , node2 , similarity |
algo(similarity).params({
uuids: [3],
uuids2: [1,2,4],
node_schema_property: ['@product.price', '@product.weight', '@product.width'],
type: 'cosine'
}).stream() as cs
where cs.similarity > 0.8
return cs
结果:cs
node1 | node2 | similarity |
---|---|---|
3 | 2 | 0.883292081301959 |
3 | 4 | 0.877834381494613 |
algo(similarity).params({
uuids: [1,3],
node_schema_property: ['price', 'weight', 'width', 'height'],
type: 'cosine',
top_limit: 1
}).stream() as top
return top
结果:top
node1 | node2 | similarity |
---|---|---|
1 | 2 | 0.986529413529119 |
3 | 2 | 0.934216530725663 |