第十八章 番外 余弦相似度

余弦相似度(Cosine Similarity)是一种衡量两个非零向量之间角度的度量方式,用于评估它们之间的相似性。它的值范围从 -1 到 1,其中 1 表示完全相同的方向(即向量完全相同),0 表示正交(没有相似性),而 -1 表示完全相反的方向。

假设我们有两个向量 A 和 B,它们的余弦相似度可以通过以下公式计算:

\\text{similarity} = \\cos(\\theta) = \\frac{\\mathbf{A} \\cdot \\mathbf{B}}{\|\\mathbf{A}\| \|\\mathbf{B}\|}

其中:

  • \\mathbf{A} \\cdot \\mathbf{B} 是向量 A 和 B 的点积(内积)。
  • \|\\mathbf{A}\| 和 和 和 \|\\mathbf{B}\| 分别是向量 A 和 B 的模长(长度)。

具体来说:

  • 点积(内积) \\mathbf{A} \\cdot \\mathbf{B} = \\sum_{i=1}\^{n} A_i B_i ,其中 (n) 是向量的维度。
  • 模长(长度) \|\\mathbf{A}\| = \\sqrt{\\sum_{i=1}{n} A_i\^2}

公式可以进一步展开为:

\\text{similarity} = \\frac{\\sum\\limits_{i=1}\^{n} A_i B_i}{\\sqrt{\\sum\\limits_{i=1}\^{n} A_i\^2} \\sqrt{\\sum\\limits_{i=1}\^{n} B_i\^2}}

示例计算

假设我们有两个向量 A 和 B,其中:

  • \\mathbf{A} = \[1, 2, 3\]
  • \\mathbf{B} = \[4, 5, 6\]

我们可以按照上述公式计算它们之间的余弦相似度:

  1. 点积
    \\mathbf{A} \\cdot \\mathbf{B} = 14 + 25 + 3\*6 = 4 + 10 + 18 = 32
  2. 模长
    • \|\\mathbf{A}\| = \\sqrt{12 + 22 + 3\^2} = \\sqrt{1 + 4 + 9} = \\sqrt{14}
    • \|\\mathbf{B}\| = \\sqrt{42 + 52 + 6\^2} = \\sqrt{16 + 25 + 36} = \\sqrt{77}
  3. 余弦相似度
    \\text{similarity} = \\frac{32}{\\sqrt{14} \\sqrt{77}} = \\frac{32}{\\sqrt{1078}}

我们可以使用 Python 来计算这个值:

python 复制代码
import numpy as np

# 定义两个向量
vector_a = np.array([1, 2, 3])
vector_b = np.array([4, 5, 6])

# 计算点积
dot_product = np.dot(vector_a, vector_b)

# 计算模长
norm_a = np.linalg.norm(vector_a)
norm_b = np.linalg.norm(vector_b)

# 计算余弦相似度
cosine_similarity = dot_product / (norm_a * norm_b)

print("Cosine similarity:", cosine_similarity)
相关推荐
cnxy1887 小时前
围棋对弈Python程序开发完整指南:步骤4 - 提子逻辑和劫争规则实现
开发语言·python·机器学习
BOF_dcb10 小时前
【无标题】
pytorch·深度学习·机器学习
咚咚王者10 小时前
人工智能之核心基础 机器学习 第一章 基础概述
人工智能·机器学习
人工智能培训12 小时前
深度学习—卷积神经网络(1)
人工智能·深度学习·神经网络·机器学习·cnn·知识图谱·dnn
云天徽上12 小时前
【机器学习】Kaggle案例之Rossmann连锁药店销售额预测:时间序列与机器学习完美融合的实战指南
机器学习·数据挖掘·kaggle
啊巴矲13 小时前
小白从零开始勇闯人工智能:机器学习初级篇(贝叶斯算法与SVM算法)
人工智能·机器学习·支持向量机
智算菩萨14 小时前
【Python机器学习】交叉验证与超参数调优:自动化寻优之旅
人工智能·深度学习·机器学习
小鸡吃米…14 小时前
机器学习 - Python 库
人工智能·python·机器学习
Brduino脑机接口技术答疑14 小时前
TDCA 算法在 SSVEP 场景中的 Padding 技术:原理、应用与工程实现
人工智能·算法·机器学习·数据分析·脑机接口
智算菩萨14 小时前
【Python机器学习】Bagging 与 Boosting:集成学习的两种风格
机器学习·集成学习·boosting