知识卡片

普通读书笔记卡 · 6668.e · 生成模型:GPT-6

余弦计算应处理零向量和维数

理解等级:中级

零向量没有可定义的方向,直接除以其范数会除零;未知词查询可能恰好变成零向量。手写zip点积还会对不等长输入静默截短,字典迭代又默认得到键;需验证特征契约,并明确零向量的拒答或评分约定。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • · 查看原书依据

      >>> import math >>> def cosine_sim(vec1, vec2): ... dot_prod = 0 ... for x1, x2 in zip(vec1, vec2): ... dot_prod += x1 * x2 ... ... mag_1 = math.sqrt(sum([x1**2 for x1 in vec1])) ... mag_2 = math.sqrt(sum([x2**2 for x2 in vec2])) ... ... return dot_prod / (mag_1 * mag_2) You need to take the dot product of two of your vectors in question—multiply the elements

    参考来源

    - 位置:《Natural Language Processing in Action, Second Edition》第03章《词频与逆文档频率向量》(来源项目:a19c7933-4eda-4176-aebd-30fe7e1ad292;源文件:extracted/Natural Language Processing in Action, Second Edition/_epub-src/OEBPS/Text/chapter-3.html) - 结论依据:摘录中关于余弦计算应处理零向量和维数的定义、比较或条件支撑卡片;原书表述不严谨的范围已在覆盖记录逐项限定,不把案例结果外推为普遍结论。 - 原始内容:>>> import math >>> def cosine_sim(vec1, vec2): ... dot_prod = 0 ... for x1, x2 in zip(vec1, vec2): ... dot_prod += x1 * x2 ... ... mag_1 = math.sqrt(sum([x1**2 for x1 in vec1])) ... mag_2 = math.sqrt(sum([x2**2 for x2 in vec2])) ... ... return dot_prod / (mag_1 * mag_2) You need to take the dot product of two of your vectors in question—multiply the elements