修改密码

请输入密码
请输入密码 请输入8-64长度密码 和 email 地址不相同 至少包括数字、大写字母、小写字母、半角符号中的 3 个
请输入密码
提交

修改昵称

当前昵称:
提交

申请证书

证书详情

Please complete this required field.

  • Ultipa Graph V4

Standalone

Please complete this required field.

Please complete this required field.

服务器的MAC地址

Please complete this required field.

Please complete this required field.

取消
申请
ID
产品
状态
核数
申请天数
审批时间
过期时间
MAC地址
申请理由
审核信息
关闭
基础信息
  • 用户昵称:
  • 手机号:
  • 公司名称:
  • 公司邮箱:
  • 地区:
  • 语言:
修改密码
申请证书

当前未申请证书.

申请证书
Certificate Issued at Valid until Serial No. File
Serial No. Valid until File

Not having one? Apply now! >>>

ProductName CreateTime ID Price File
ProductName CreateTime ID Price File

No Invoice

搜索
    中文

      余弦相似度

      ✓ 文件回写 ✕ 属性回写 ✓ 直接返回 ✓ 流式返回 ✕ 统计值

      概述

      在余弦相似度(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 限制 top_list 的长度,-1 返回完整的 top_list

      本算法有两种计算模式:

      1. 配对:同时配置了 ids/uuidsids2/uuids2 时,将第一组与第二组中的节点两两配对(笛卡尔乘积),逐对计算相似度。
      2. 选拔:仅配置 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]
      }).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;product3:0.878858;product4:0.816876;
      product2,product1:0.986529;product3:0.934217;product4:0.881988;
      product3,product2:0.934217;product4:0.930153;product1:0.878858;
      product4,product3:0.930153;product2:0.881988;product1:0.816876;
      

      直接返回

      计算模式
      别名序号
      类型
      描述
      列名
      配对 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: "cosine"
      }) as cs
      return cs order by cs.similarity
      

      结果:cs

      node1 node2 similarity
      1 4 0.816876150267203
      1 3 0.878858407519654
      1 2 0.986529413529119
      algo(similarity).params({
        uuids: [1,2],
        type: "cosine",
        node_schema_property: [price,weight,width,height],
        top_limit: 1
      }) as top
      return top
      

      结果:top

      node top_list
      1 2:0.986529,
      2 1:0.986529,

      流式返回

      计算模式
      别名序号
      类型
      描述
      列名
      配对 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: "cosine"
      }).stream() as cs
      where cs.similarity > 0.9 
      return cs
      

      结果:cs

      node1 node2 similarity
      3 2 0.934216530725663
      3 4 0.930152895706265
      algo(similarity).params({
        uuids: [1,3],
        node_schema_property: [price,weight,width,height],
        type: "cosine",
        top_limit: 1
      }).stream() as top
      return top
      

      结果:top

      node top_list
      1 2:0.986529,
      3 2:0.934217,
      请完成以下信息后可下载此书
      *
      公司名称不能为空
      *
      公司邮箱必须填写
      *
      你的名字必须填写
      *
      你的电话必须填写
      *
      你的电话必须填写