知识卡片

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

余弦相似度去除向量长度影响

理解等级:中级

非零向量的余弦相似度为点积除以两者L2范数乘积。方向相同的正倍数得到1,原计数无需相同;文本长短带来的整体缩放因而被消除,但重复某些词造成方向变化仍会影响结果。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • · 查看原书依据

      >>> cos_similarity_between_A_and_B = np.cos(angle_between_A_and_B) \ ... = A.dot(B) / (np.linalg.norm(A) * np.linalg.norm(B)) In linear algebra notation, this becomes equation 3.2: Equation 3.2 Cosine similarity between two vectors Or in pure Python without numpy, it looks like the code in the following listing. Listing 3.11 Computing cosine similarity in Python >>> import math >>> def cosine_sim(vec1, vec2): ... dot_prod = 0 ... for x1, x2 in

    参考来源

    - 位置:《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) - 结论依据:摘录中关于余弦相似度去除向量长度影响的定义、比较或条件支撑卡片;原书表述不严谨的范围已在覆盖记录逐项限定,不把案例结果外推为普遍结论。 - 原始内容:>>> cos_similarity_between_A_and_B = np.cos(angle_between_A_and_B) \ ... = A.dot(B) / (np.linalg.norm(A) * np.linalg.norm(B)) In linear algebra notation, this becomes equation 3.2: Equation 3.2 Cosine similarity between two vectors Or in pure Python without numpy, it looks like the code in the following listing. Listing 3.11 Computing cosine similarity in Python >>> import math >>> def cosine_sim(vec1, vec2): ... dot_prod = 0 ... for x1, x2 in