✓ 文件回写 ✕ 属性回写 ✓ 直接返回 ✓ 流式返回 ✕ 统计值
概述
皮尔森相关系数(Pearson Correlation Coefficient)是衡量两个可定量的变量之间线性关系强度和方向的最常用方法。在图中,节点可由它们的 N 个数值属性(特征)来量化。
皮尔森相关系数(r)的定义为两个变量 X = (x1, x2, ..., xn) 和 Y = (y1, y2, ..., yn) 之间的协方差和两者标准差乘积的比值:
皮尔森相关系数的取值范围是 [-1,1]:
皮尔森相关系数 |
关系类型 |
解释 |
---|---|---|
0 < r ≤ 1 | 正相关 | 一个变量值变大,另一个变量值也会变大 |
r = 0 | 没有线性相关性 | (但可能存在其他相关性) |
-1 ≤ r < 0 | 负相关 | 一个变量值变大,另一个变量值反而会变小 |
特殊说明
- 两个节点的皮尔森相关系数理论上不依赖它们之间的连通性。
语法
- 命令:
algo(similarity)
- 参数:
名称 |
类型 |
规范 |
默认 |
可选 |
描述 |
---|---|---|---|---|---|
ids / uuids | []_id / []_uuid |
/ | / | 否 | 待计算的第一组节点的 ID / UUID |
ids2 / uuids2 | []_id / []_uuid |
/ | / | 是 | 待计算的第二组节点的 ID / UUID |
type | string | pearson |
cosine |
否 | 相似度衡量指标;计算皮尔森相关系数时,保持此项为 pearson |
node_schema_property | []@<schema>?.<property> |
必须 LTE | / | 否 | 必须指定至少两个数值类的点属性来量化表示节点 |
limit | int | >=-1 | -1 |
是 | 返回的结果条数,-1 返回所有结果 |
top_limit | int | >=-1 | -1 |
是 | 限制 top_list 的长度,-1 返回完整的 top_list |
本算法有两种计算模式:
- 配对:同时配置了
ids/uuids
和ids2/uuids2
时,将第一组与第二组中的节点两两配对(笛卡尔乘积),逐对计算相似度。 - 选拔:仅配置
ids/uuids
时,对于每一个节点,计算其与图中其他所有节点的相似度,目的是选拔与其最相似的节点,返回的top_list
包含所有与其相似度大于 0 的结果并按相似度降序排列。
示例
示例图包含 4 个产品(忽略边),每个产品包含 price、weight、width 和 height 属性:
文件回写
计算模式 | 配置项 | 回写内容 |
---|---|---|
配对 | filename | node1 ,node2 ,similarity |
选拔 | filename | node ,top_list |
algo(similarity).params({
uuids: [1],
uuids2: [2,3,4],
node_schema_property: [price,weight,width,height],
type: "pearson"
}).write({
file:{
filename: "pearson"
}
})
结果:文件 pearson
product1,product2,0.998785
product1,product3,0.474384
product1,product4,0.210494
algo(similarity).params({
uuids: [1,2,3,4],
node_schema_property: [price,weight,width,height],
type: "pearson"
}).write({
file:{
filename: "list"
}
})
结果:文件 list
product1,product2:0.998785;product3:0.474384;product4:0.210494;
product2,product1:0.998785;product3:0.507838;product4:0.253573;
product3,product2:0.507838;product1:0.474384;product4:0.474021;
product4,product3:0.474021;product2:0.253573;product1:0.210494;
直接返回
计算模式 |
别名序号 |
类型 |
描述 |
列名 |
---|---|---|---|---|
配对 | 0 | []perNodePair | 各点对及相似度 | node1 , node2 , similarity |
选拔 | 0 | []perNode | 各点及其选拔结果 | node , top_list |
algo(similarity).params({
uuids: [1],
uuids2: [2,3,4],
node_schema_property: [price,weight,width,height],
type: "pearson"
}) as p
return p order by p.similarity
结果:p
node1 | node2 | similarity |
---|---|---|
1 | 4 | 0.210494150169583 |
1 | 3 | 0.474383803132863 |
1 | 2 | 0.998785121601255 |
algo(similarity).params({
uuids: [1,2],
type: "pearson",
node_schema_property: [price,weight,width,height],
top_limit: 1
}) as top
return top
结果:top
node | top_list |
---|---|
1 | 2:0.998785, |
2 | 1:0.998785, |
流式返回
计算模式 |
别名序号 |
类型 |
描述 |
列名 |
---|---|---|---|---|
配对 | 0 | []perNodePair | 各点对及相似度 | node1 , node2 , similarity |
选拔 | 0 | []perNode | 各点及其选拔结果 | node , top_list |
algo(similarity).params({
uuids: [3],
uuids2: [1,2,4],
node_schema_property: [price,weight,width,height],
type: "pearson"
}).stream() as p
where p.similarity > 0.5
return p
结果:p
node1 | node2 | similarity |
---|---|---|
3 | 2 | 0.50783775659896 |
algo(similarity).params({
uuids: [1,3],
node_schema_property: [price,weight,width,height],
type: "pearson",
top_limit: 1
}).stream() as top
return top
结果:top
node | top_list |
---|---|
1 | 2:0.998785, |
3 | 2:0.507838, |