Add files via upload

This commit is contained in:
Iris Series: Visualize Math -- From Arithmetic Basics to Machine Learning
2025-02-01 17:06:45 +08:00
committed by GitHub
parent f985536e82
commit 79be5dda7d
47 changed files with 10428 additions and 0 deletions

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,198 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# L2范数\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "ab88dee3-1a2e-4357-b6b5-136abf3073c5",
"metadata": {},
"source": [
"此代码定义了两个二维列向量 `a` 和 `b`,并计算它们的L2范数。L2范数也称为欧几里得范数,用于测量向量的长度。公式如下:\n",
"\n",
"$$\n",
"\\| a \\|_2 = \\sqrt{a_1^2 + a_2^2}\n",
"$$ \n",
"\n",
"对于向量 `a` 和 `b`,L2范数分别为:\n",
"\n",
"$$\n",
"\\| a \\|_2 = \\sqrt{4^2 + 3^2} = 5\n",
"$$\n",
"\n",
"$$\n",
"\\| b \\|_2 = \\sqrt{(-3)^2 + 4^2} = 5\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "2a36cd49-a4e6-4321-a7e8-7f9a34c6aaef",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "01a128eb-3998-4172-bd8a-ec95a23bbc83",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "8ad8b4b0-856b-47a7-adb8-b020180d0c30",
"metadata": {},
"source": [
"## 定义两个列向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "212d3a51-f2c6-4785-8c9a-40fa93ae0566",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4],\n",
" [3]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[4], [3]]) # 定义向量a,值为[4, 3]\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "8d5f6350-fddc-4633-946e-f21797b6f5e8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-3],\n",
" [ 4]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([[-3], [4]]) # 定义向量b,值为[-3, 4]\n",
"b"
]
},
{
"cell_type": "markdown",
"id": "a3ba8142-26a8-43d6-b777-685207499ca9",
"metadata": {},
"source": [
"## 计算L2范数"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1954434f-cb3e-44d3-b985-45b73af8a815",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_L2_norm = np.linalg.norm(a) # 计算向量a的L2范数\n",
"a_L2_norm"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8110a0d6-c941-4825-bf60-bc989324f592",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b_L2_norm = np.linalg.norm(b) # 计算向量b的L2范数\n",
"b_L2_norm"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,314 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 向量加减法\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "5727cda1-e9bd-4483-b50f-9b9ef36574ae",
"metadata": {},
"source": [
"这段代码定义了两个二维列向量 $a$ 和 $b$,并计算了它们的加法与减法。具体来说,向量 $a$ 定义为:\n",
"\n",
"$$\n",
"a = \\begin{bmatrix} -2 \\\\ 5 \\end{bmatrix}\n",
"$$\n",
"\n",
"向量 $b$ 定义为:\n",
"\n",
"$$\n",
"b = \\begin{bmatrix} 5 \\\\ -1 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码首先计算了 $a$ 和 $b$ 的向量加法,得到结果向量 $a + b$,其计算过程为:\n",
"\n",
"$$\n",
"a + b = \\begin{bmatrix} -2 \\\\ 5 \\end{bmatrix} + \\begin{bmatrix} 5 \\\\ -1 \\end{bmatrix} = \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix}\n",
"$$\n",
"\n",
"此外,代码还展示了向量减法 $a - b$ 和 $b - a$,分别为:\n",
"\n",
"$$\n",
"a - b = \\begin{bmatrix} -2 \\\\ 5 \\end{bmatrix} - \\begin{bmatrix} 5 \\\\ -1 \\end{bmatrix} = \\begin{bmatrix} -7 \\\\ 6 \\end{bmatrix}\n",
"$$\n",
"\n",
"$$\n",
"b - a = \\begin{bmatrix} 5 \\\\ -1 \\end{bmatrix} - \\begin{bmatrix} -2 \\\\ 5 \\end{bmatrix} = \\begin{bmatrix} 7 \\\\ -6 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码中分别使用了直接运算(如 `a + b`)和 `NumPy` 函数 `np.add` 与 `np.subtract` 来完成加法和减法操作。这种处理方式展示了向量的基本线性代数操作。"
]
},
{
"cell_type": "markdown",
"id": "88825576-ff58-4b2e-8a6b-770f593c57bb",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "61e4349a-f354-4e33-a9a0-0b43ab26eb3b",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "ea419dfa-5bf7-4c35-b465-8ff0416ce120",
"metadata": {},
"source": [
"## 定义两个列向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ee8dadac-372a-43a9-8a21-f2e54d27b4c6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2],\n",
" [ 5]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[-2], [5]]) # 定义向量a,值为[-2, 5]\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1749cae5-c1ac-491b-ba00-0e93066130ac",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 5],\n",
" [-1]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([[5], [-1]]) # 定义向量b,值为[5, -1]\n",
"b"
]
},
{
"cell_type": "markdown",
"id": "68440eab-d48e-450b-ad71-43adcc4c30b3",
"metadata": {},
"source": [
"## 计算向量加法"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7c6885fd-59eb-457b-967f-fbeebee26a96",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3],\n",
" [4]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_plus_b = a + b # 使用直接加法计算a和b的和\n",
"a_plus_b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2bbd2e21-91c4-43b5-b18d-597011c73214",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3],\n",
" [4]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_plus_b_2 = np.add(a, b) # 使用np.add函数计算a和b的和\n",
"a_plus_b_2"
]
},
{
"cell_type": "markdown",
"id": "54f2167f-edeb-48f4-81e4-89e27e459fd8",
"metadata": {},
"source": [
"## 计算向量减法"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f6e97112-bfc4-44e9-9a9d-d168d4ba030e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-7],\n",
" [ 6]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_minus_b = a - b # 计算a减去b的差\n",
"a_minus_b"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "a1b483cd-6bfc-450c-a883-d86a89ea897b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-7],\n",
" [ 6]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_minus_b_2 = np.subtract(a, b) # 使用np.subtract函数计算a减去b的差\n",
"a_minus_b_2"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "b1b4beab-4cab-48b1-b90d-065ee5971e9e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 7],\n",
" [-6]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b_minus_a = b - a # 计算b减去a的差\n",
"b_minus_a"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "2a010763-e0f1-457f-acf9-e8e0182d271d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 7],\n",
" [-6]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b_minus_a_2 = np.subtract(b, a) # 使用np.subtract函数计算b减去a的差\n",
"b_minus_a_2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 标量乘法\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "9fcf085a-8ecf-4003-b0b1-76f2e95c1063",
"metadata": {},
"source": [
"\n",
"\n",
"此代码定义了一个二维列向量 $a$,其值为:\n",
"\n",
"$$\n",
"a = \\begin{bmatrix} 2 \\\\ 2 \\end{bmatrix}\n",
"$$\n",
"\n",
"接下来,代码通过数乘操作生成两个新向量 $b$ 和 $c$。具体而言,$b$ 是 $a$ 的 2 倍:\n",
"\n",
"$$\n",
"b = 2 \\times a = 2 \\times \\begin{bmatrix} 2 \\\\ 2 \\end{bmatrix} = \\begin{bmatrix} 4 \\\\ 4 \\end{bmatrix}\n",
"$$\n",
"\n",
"$c$ 是 $a$ 的 $-1.5$ 倍:\n",
"\n",
"$$\n",
"c = -1.5 \\times a = -1.5 \\times \\begin{bmatrix} 2 \\\\ 2 \\end{bmatrix} = \\begin{bmatrix} -3 \\\\ -3 \\end{bmatrix}\n",
"$$\n",
"\n",
"此过程展示了向量的数乘操作,通过将标量与向量相乘来改变向量的大小和方向。"
]
},
{
"cell_type": "markdown",
"id": "7f2b0f70-8b54-49c7-a58c-736dd1838f39",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "457dd7c5-1ef8-4272-9608-01802512d831",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "a2882d5b-db0c-4d22-96ba-76123168e589",
"metadata": {},
"source": [
"## 定义一个列向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dc48b150-34ed-4cc8-a5c3-ae59f58fbcdf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2],\n",
" [2]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([[2], [2]]) # 定义向量a,值为[2, 2]\n",
"a"
]
},
{
"cell_type": "markdown",
"id": "0e89b43f-c8e1-4af6-a3aa-eb966479608b",
"metadata": {},
"source": [
"## 进行向量的数乘"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "352700c8-fc4c-4b7c-8f8b-2ee1a5905878",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4],\n",
" [4]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = 2 * a # 计算b,为向量a的2倍\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a7e94dac-9695-4bcc-b09b-3dc7e823fe3b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-3.],\n",
" [-3.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = -1.5 * a # 计算c,为向量a的-1.5倍\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,213 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 向量内积\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "90d57742-54b5-4967-9de6-2b97f3ff13ce",
"metadata": {},
"source": [
"该代码定义了两个二维向量 $a$ 和 $b$,并计算它们的内积。向量 $a$ 和 $b$ 的定义分别为:\n",
"\n",
"$$\n",
"a = \\begin{bmatrix} 4 & 3 \\end{bmatrix}, \\quad b = \\begin{bmatrix} 5 & -2 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码首先通过 `np.inner` 函数计算行向量的内积,其计算公式为:\n",
"\n",
"$$\n",
"a \\cdot b = 4 \\cdot 5 + 3 \\cdot (-2) = 20 - 6 = 14\n",
"$$\n",
"\n",
"接着,代码将 $a$ 和 $b$ 定义为列向量形式 $a_2$ 和 $b_2$,并通过矩阵转置与矩阵乘法计算内积:\n",
"\n",
"$$\n",
"a_2^T \\cdot b_2 = \\begin{bmatrix} 4 & 3 \\end{bmatrix} \\cdot \\begin{bmatrix} 5 \\\\ -2 \\end{bmatrix} = 14\n",
"$$\n",
"\n",
"该过程展示了内积计算的不同实现方式,包括使用 `np.inner` 和转置矩阵乘法。"
]
},
{
"cell_type": "markdown",
"id": "a869bcd3-b5b0-48a6-8473-e1af9efa124c",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "52854f8c-f415-452c-8b0f-ba216cedb4a3",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "a895dedd-943b-4b5e-a9d8-6c958e1ec18e",
"metadata": {},
"source": [
"## 定义两个行向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "13d6cc5a-824f-4537-8d80-5e4a0c6146a7",
"metadata": {},
"outputs": [],
"source": [
"a = np.array([[4, 3]]) # 定义向量a,值为[4, 3]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "89d1799e-2c61-4410-83fd-401db971b8fd",
"metadata": {},
"outputs": [],
"source": [
"b = np.array([[5, -2]]) # 定义向量b,值为[5, -2]"
]
},
{
"cell_type": "markdown",
"id": "3277e077-e975-4354-b79a-5bc9e77d5666",
"metadata": {},
"source": [
"## 计算内积"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e4008c0e-94ff-4cb5-8139-432ed0048692",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[14]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_dot_b = np.inner(a, b) # 使用np.inner计算a和b的内积\n",
"a_dot_b"
]
},
{
"cell_type": "markdown",
"id": "b6a1c7e0-b5e4-4303-b2a8-19e7a0d72cbb",
"metadata": {},
"source": [
"## 定义两个列向量"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c30e546b-1f82-43c7-b4d5-163394f406c3",
"metadata": {},
"outputs": [],
"source": [
"a_2 = np.array([[4], [3]]) # 定义列向量a_2,值为[4, 3]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "41ee49f6-1d62-4f0e-895e-671ae32ad301",
"metadata": {},
"outputs": [],
"source": [
"b_2 = np.array([[5], [-2]]) # 定义列向量b_2,值为[5, -2]"
]
},
{
"cell_type": "markdown",
"id": "d6a74fd1-5de1-486c-98fb-ff21a16d5158",
"metadata": {},
"source": [
"## 计算转置后内积"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ce1247f9-be4e-40aa-be27-9b9b4d8f9b03",
"metadata": {},
"outputs": [],
"source": [
"a_dot_b_2 = a_2.T @ b_2 # 使用矩阵乘法计算a_2转置和b_2的内积"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[14]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_dot_b_2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,166 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 矩阵乘积\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "98f9659f-671c-40ba-bf01-26b02f299f65",
"metadata": {},
"source": [
"\n",
"\n",
"此代码定义了两个 $2 \\times 2$ 的矩阵 $A$ 和 $B$,并计算它们的矩阵乘积。矩阵 $A$ 和 $B$ 的定义分别为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 2 & 3 \\\\ 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 3 & 4 \\\\ 5 & 6 \\end{bmatrix}\n",
"$$\n",
"\n",
"矩阵乘积 $A @ B$ 的计算公式是:\n",
"\n",
"$$\n",
"A @ B = \\begin{bmatrix} 2 \\cdot 3 + 3 \\cdot 5 & 2 \\cdot 4 + 3 \\cdot 6 \\\\ 3 \\cdot 3 + 4 \\cdot 5 & 3 \\cdot 4 + 4 \\cdot 6 \\end{bmatrix} = \\begin{bmatrix} 21 & 26 \\\\ 29 & 38 \\end{bmatrix}\n",
"$$\n"
]
},
{
"cell_type": "markdown",
"id": "b7d099d5-b19e-4dc1-bcbc-c6f27ff10d63",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "fe40fe29-6b65-47eb-a82d-c9739d4f17b1",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "972ceb2f-dfc9-406b-aa4d-759d8a3c5ceb",
"metadata": {},
"source": [
"## 定义两个矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "022dfa07-a9e4-4ba6-9456-f7dd6b861bcd",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[2, 3], # 定义矩阵A\n",
" [3, 4]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "eba046cd-9051-403b-86a4-5586226e9c86",
"metadata": {},
"outputs": [],
"source": [
"B = np.array([[3, 4], # 定义矩阵B\n",
" [5, 6]])"
]
},
{
"cell_type": "markdown",
"id": "97150b17-9793-47aa-b9c1-aa61baf34bde",
"metadata": {},
"source": [
"## 计算矩阵点积"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "dec0b7d8-6d24-4d13-8bf2-0786a4d1e8e6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[21, 26],\n",
" [29, 36]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_dot_B = np.dot(A, B) # 使用np.dot计算A和B的矩阵乘积\n",
"A_dot_B"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[21, 26],\n",
" [29, 36]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 可以直接用A @ B作为矩阵乘法的简写\n",
"A @ B"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 向量化内积\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "6f5b0126-e220-42a5-9c76-10b5c77dd35c",
"metadata": {},
"source": [
"此代码定义了两个 $2 \\times 2$ 的矩阵 $A$ 和 $B$,并使用 `np.vdot` 计算它们的向量化内积。`np.vdot` 会将矩阵元素展平成一维向量,然后执行逐元素的内积计算。\n",
"\n",
"矩阵 $A$ 和 $B$ 展开后为:\n",
"\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 & 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 3 & 4 & 5 & 6 \\end{bmatrix}\n",
"$$\n",
"\n",
"因此,内积的计算为:\n",
"\n",
"$$\n",
"1 \\cdot 3 + 2 \\cdot 4 + 3 \\cdot 5 + 4 \\cdot 6 = 3 + 8 + 15 + 24 = 50\n",
"$$\n",
"\n",
"代码使用 `np.vdot` 执行此向量化的内积计算,而不仅是逐元素矩阵乘法。"
]
},
{
"cell_type": "markdown",
"id": "004f7421-698b-4eb3-9d22-1517683ab9bc",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f0b0c3a1-f413-487e-9f5f-9d8a1a37248f",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "f3ab1198-85f2-47d5-9425-46145141e92e",
"metadata": {},
"source": [
"## 定义两个矩阵"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "138d24b7-39e5-4ccc-9d5c-f554db5c2eb9",
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 2], # 定义矩阵A\n",
" [3, 4]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c79531e6-89d7-4c4c-91ae-afe500cec8bb",
"metadata": {},
"outputs": [],
"source": [
"B = np.array([[3, 4], # 定义矩阵B\n",
" [5, 6]])"
]
},
{
"cell_type": "markdown",
"id": "438b8825-fda5-4905-890a-6f690b7c11dd",
"metadata": {},
"source": [
"## 计算矩阵的向量内积"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8505b1bc-d105-424b-a163-25205e0f5e94",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A_dot_B = np.vdot(A, B) # 使用np.vdot计算A和B的向量内积\n",
"A_dot_B\n",
"# [1,2,3,4]*[3,4,5,6].T # 向量化的点积计算"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,215 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 向量夹角\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "8bb390c4-26af-44db-bf6a-d0edb2d14b1e",
"metadata": {},
"source": [
"该代码定义了两个二维列向量 $a$ 和 $b$,并计算了它们之间夹角的余弦值、弧度和度数。\n",
"\n",
"### 计算公式\n",
"\n",
"向量 $a$ 和 $b$ 分别为:\n",
"\n",
"$$\n",
"a = \\begin{bmatrix} 4 \\\\ 3 \\end{bmatrix}, \\quad b = \\begin{bmatrix} 5 \\\\ -2 \\end{bmatrix}\n",
"$$\n",
"\n",
"余弦夹角公式为:\n",
"\n",
"$$\n",
"\\cos \\theta = \\frac{a \\cdot b}{\\|a\\|_2 \\|b\\|_2} = \\frac{a^T b}{\\|a\\|_2 \\|b\\|_2}\n",
"$$\n",
"\n",
"接着,使用反余弦函数计算角度的弧度值:\n",
"\n",
"$$\n",
"\\theta = \\arccos(\\cos \\theta)\n",
"$$\n",
"\n",
"最后将弧度值转换为角度:\n",
"\n",
"$$\n",
"\\theta_{\\text{degree}} = \\theta \\times \\frac{180}{\\pi}\n",
"$$\n",
"\n",
"代码中的运算展示了向量间夹角的计算方法,包括余弦、弧度和度数转换。"
]
},
{
"cell_type": "markdown",
"id": "01c7be45-8810-4f12-aca9-ecc099054269",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "53d8013b-3c6e-40da-bc98-e5787ed25f79",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "6c657ddd-c8a1-4ddc-825f-e9e82bcade44",
"metadata": {},
"source": [
"## 定义两个列向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8e262906-50d0-44ee-b31b-4d6569b1b5b9",
"metadata": {},
"outputs": [],
"source": [
"a, b = np.array([[4], [3]]), np.array([[5], [-2]]) # 定义向量a和b"
]
},
{
"cell_type": "markdown",
"id": "ff9561ac-02f3-4b67-82e7-be3dddfb4460",
"metadata": {},
"source": [
"## 计算余弦值"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "50fba935-3aa3-41f7-8aed-f29544822233",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.51994695]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cos_theta = (a.T @ b) / (np.linalg.norm(a, 2) * np.linalg.norm(b, 2)) \n",
"# 计算向量a和b之间夹角的余弦值\n",
"cos_theta"
]
},
{
"cell_type": "markdown",
"id": "d65329e6-0d9e-4005-b6b0-a8c46fdb2c04",
"metadata": {},
"source": [
"## 计算夹角的弧度值"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a94cfe3e-0338-4fb4-99d2-0b2e5e08d198",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.02400749]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cos_radian = np.arccos(cos_theta) # 计算余弦值对应的弧度\n",
"cos_radian"
]
},
{
"cell_type": "markdown",
"id": "56bc6d0e-831a-4f2f-ab59-9ff9fd558a41",
"metadata": {},
"source": [
"## 将弧度转换为度数"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f018e489-013e-4f38-bdb4-291023e46179",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[58.67130713]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cos_degree = cos_radian * (180 / np.pi) # 将弧度转换为度数\n",
"cos_degree"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,411 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 余弦距离\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "a4d2d126-5db0-4038-a1dc-9a83708d2220",
"metadata": {},
"source": [
"此代码加载了鸢尾花数据集,并提取了数据集中的几个特定数据点以计算它们之间的余弦距离和夹角余弦值。选取的特征包含所有特征,而数据点分别为第1、2、51、101个观测。\n",
"\n",
"### 计算公式\n",
"代码使用余弦距离和余弦相似度来度量两个向量之间的相似性。\n",
"\n",
"1. 余弦距离公式:\n",
"\n",
"$$\n",
"\\text{cosine\\_distance} = 1 - \\frac{x_1 \\cdot x_2}{\\|x_1\\| \\|x_2\\|}\n",
"$$\n",
"\n",
"2. 向量间夹角的余弦值:\n",
"\n",
"$$\n",
"\\cos \\theta = \\frac{x_1 \\cdot x_2}{\\|x_1\\| \\|x_2\\|}\n",
"$$\n",
"\n",
"余弦距离反映了两个向量间的角度差异,而夹角余弦值则用于计算向量的相似性。"
]
},
{
"cell_type": "markdown",
"id": "6aec7dfe-a3d4-4cb9-b921-b3a137455aca",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "83ff9c7b-6adb-4fe1-91ef-4325850851b2",
"metadata": {},
"outputs": [],
"source": [
"from scipy.spatial import distance # 导入SciPy库,用于计算向量间的距离\n",
"from sklearn import datasets # 导入sklearn数据集模块\n",
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "4723d4a7-92b7-4c8d-8dcc-e678a7daded0",
"metadata": {},
"source": [
"## 导入鸢尾花数据集"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f4654f70-41cf-40b9-ad1d-5e39c337a40a",
"metadata": {},
"outputs": [],
"source": [
"iris = datasets.load_iris() # 加载鸢尾花数据集"
]
},
{
"cell_type": "markdown",
"id": "a7d90350-e6b8-4f35-a30f-f17e3042bbfa",
"metadata": {},
"source": [
"## 使用前两个特征:萼片长度和萼片宽度"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "111624e1-5071-43b5-ad7f-f91c1ea86f1f",
"metadata": {},
"outputs": [],
"source": [
"X = iris.data[:, :] # 提取所有特征数据"
]
},
{
"cell_type": "markdown",
"id": "08b685c0-3e21-4c99-8f19-d15fc50ba309",
"metadata": {},
"source": [
"## 提取4个数据点"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "af961fce-c345-4d97-a8d8-dc1fd27f75e6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([5.1, 3.5, 1.4, 0.2])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1_data = X[0, :] # 第一个数据点\n",
"x1_data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "98593e09-12b0-43a1-81c0-97751c3f44cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([4.9, 3. , 1.4, 0.2])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x2_data = X[1, :] # 第二个数据点\n",
"x2_data"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "261efd1f-06bc-4361-996b-62a2f621234a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([7. , 3.2, 4.7, 1.4])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x51_data = X[50, :] # 第51个数据点\n",
"x51_data"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "637692dd-8df7-472c-97e5-c1be1475c891",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([6.3, 3.3, 6. , 2.5])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x101_data = X[100, :] # 第101个数据点\n",
"x101_data"
]
},
{
"cell_type": "markdown",
"id": "a14578f6-7866-4fea-8354-2633c0a50df7",
"metadata": {},
"source": [
"## 计算余弦距离和夹角余弦值"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d73204a7-6235-4449-a3ea-90d677c04fe6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0014208364959781283"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1_x2_cos_dist = distance.cosine(x1_data, x2_data) \n",
"# 计算x1和x2的余弦距离\n",
"x1_x2_cos_dist"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1b3df27e-df51-4564-a192-b3d0544fd820",
"metadata": {},
"outputs": [],
"source": [
"x1_norm = np.linalg.norm(x1_data) # 计算x1的L2范数"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "efb9a6c3-ccfa-4000-8135-677fdd68c81d",
"metadata": {},
"outputs": [],
"source": [
"x2_norm = np.linalg.norm(x2_data) # 计算x2的L2范数"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "06c48d91-451f-404c-8e11-6006943c4a38",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"37.489999999999995"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1_dot_x2 = x1_data.T @ x2_data # 计算x1和x2的点积\n",
"x1_dot_x2"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "5818da3f-985c-4c48-8fc9-159ec8067f2e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9985791635040218"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1_x2_cos = x1_dot_x2 / x1_norm / x2_norm # 计算x1和x2的夹角余弦值\n",
"x1_x2_cos"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "f3e0969d-ffbf-4c10-95ee-69898c57dc0c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0014208364959782394"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1 - x1_x2_cos"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "62c0e4a9-c455-4290-9389-a2ee56c2adc1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0014208364959781283"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1_x2_cos_dist = distance.cosine(x1_data, x2_data) # 计算x1和x2的余弦距离\n",
"x1_x2_cos_dist"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "46e0f1db-9989-4908-89de-ffe0f0a8f431",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.07161964128508791"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1_x51_cos_dist = distance.cosine(x1_data, x51_data) # 计算x1和x51的余弦距离\n",
"x1_x51_cos_dist"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "0f516143-46a8-42ba-8e31-fe4b08d428de",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.1399186683412712"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1_x101_cos_dist = distance.cosine(x1_data, x101_data) # 计算x1和x101的余弦距离\n",
"x1_x101_cos_dist"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,193 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 向量积\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "873da06f-c90c-4a2a-abd3-7f8711feca02",
"metadata": {},
"source": [
"此代码定义了两个三维向量 $a$ 和 $b$,并计算了它们的叉积(向量积)。首先定义了行向量形式的 $a$ 和 $b$,然后分别计算了行向量和列向量的叉积。\n",
"\n",
"### 叉积公式\n",
"对于三维向量 $a = \\begin{bmatrix} a_1 \\\\ a_2 \\\\ a_3 \\end{bmatrix}$ 和 $b = \\begin{bmatrix} b_1 \\\\ b_2 \\\\ b_3 \\end{bmatrix}$,叉积定义为:\n",
"\n",
"$$\n",
"a \\times b = \\begin{bmatrix} a_2 b_3 - a_3 b_2 \\\\ a_3 b_1 - a_1 b_3 \\\\ a_1 b_2 - a_2 b_1 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码中计算了行向量和列向量形式的叉积。结果表示两个向量的垂直方向,并可用于三维空间中的法向量计算。"
]
},
{
"cell_type": "markdown",
"id": "4e369506-697f-450e-a731-76945415ed9c",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "dbb6f14c-fcb4-499f-9618-d6ca9c8b36ab",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "5d9fa082-6e75-4a30-9ff1-b119c73a3a25",
"metadata": {},
"source": [
"## 定义两个行向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "081b4fe5-be16-4854-914b-5c795f937fa8",
"metadata": {},
"outputs": [],
"source": [
"a = np.array([-2, 1, 1]) # 定义向量a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "9a902aa8-799a-4461-a5c0-459f2550272a",
"metadata": {},
"outputs": [],
"source": [
"b = np.array([1, -2, -1]) # 定义向量b"
]
},
{
"cell_type": "markdown",
"id": "b04a2e40-0ae4-44f1-a6bd-e691ee781d18",
"metadata": {},
"source": [
"## 计算行向量的叉积"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "95ea922a-d94c-42c3-8459-d8fc755b8df3",
"metadata": {},
"outputs": [],
"source": [
"a_cross_b = np.cross(a, b) # 计算a和b的叉积"
]
},
{
"cell_type": "markdown",
"id": "6de6f635-f9e9-4667-a12a-884ccaeb26f3",
"metadata": {},
"source": [
"## 定义两个列向量"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0d9f2b40-f6a0-4ca6-a70b-3910b69a9706",
"metadata": {},
"outputs": [],
"source": [
"a_col = np.array([[-2], [1], [1]]) # 定义列向量a_col"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "66e48d3a-7fa8-41a8-bf8b-61cfdf250200",
"metadata": {},
"outputs": [],
"source": [
"b_col = np.array([[1], [-2], [-1]]) # 定义列向量b_col"
]
},
{
"cell_type": "markdown",
"id": "c19ed8a3-f8e7-4c42-b6f7-bac01f31e016",
"metadata": {},
"source": [
"## 计算列向量的叉积"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9c019178-15f5-41f6-93d5-ceea5505684f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1],\n",
" [-1],\n",
" [ 3]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_cross_b_col = np.cross(a_col, b_col, axis=0) # 计算a_col和b_col的叉积,沿axis=0进行计算\n",
"a_cross_b_col"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,252 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "73bd968b-d970-4a05-94ef-4e7abf990827",
"metadata": {},
"source": [
"Chapter 02\n",
"\n",
"# 逐项积\n",
"Book_4《矩阵力量》 | 鸢尾花书:从加减乘除到机器学习 (第二版)"
]
},
{
"cell_type": "markdown",
"id": "ce4a4893-3f8c-402f-92c3-79d73b81d380",
"metadata": {},
"source": [
"\n",
"此代码定义了两个三维向量 $a$ 和 $b$,并计算了它们的逐元素积(即对应元素相乘)。代码中既使用了行向量形式也使用了列向量形式的 $a$ 和 $b$ 来演示逐元素乘法的计算。\n",
"\n",
"### 逐元素乘法公式\n",
"对于两个向量 $a = \\begin{bmatrix} a_1 \\\\ a_2 \\\\ a_3 \\end{bmatrix}$ 和 $b = \\begin{bmatrix} b_1 \\\\ b_2 \\\\ b_3 \\end{bmatrix}$,逐元素积定义为:\n",
"\n",
"$$\n",
"a \\odot b = \\begin{bmatrix} a_1 \\cdot b_1 \\\\ a_2 \\cdot b_2 \\\\ a_3 \\cdot b_3 \\end{bmatrix}\n",
"$$\n",
"\n",
"代码中的计算展示了逐元素乘法的两种实现方式:使用 `np.multiply` 函数和直接使用 `*` 操作符。结果得到一个新向量,其中每个元素为对应元素相乘的值。"
]
},
{
"cell_type": "markdown",
"id": "370c070f-d704-40b0-90c9-ef2e2bee22ce",
"metadata": {},
"source": [
"## 导入所需库"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "b92befcd-0d09-4301-aecc-cb3172ce2cd9",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # 导入NumPy库,用于数值计算"
]
},
{
"cell_type": "markdown",
"id": "25572211-3dd7-487d-ab16-d62682d66f56",
"metadata": {},
"source": [
"## 定义两个行向量"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "04b0208a-df81-48a4-8e85-f2ef347708c1",
"metadata": {},
"outputs": [],
"source": [
"a = np.array([-2, 1, 1]) # 定义向量a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "557a8b24-f13d-4995-9bc2-c8bc3fa19389",
"metadata": {},
"outputs": [],
"source": [
"b = np.array([1, -2, -1]) # 定义向量b"
]
},
{
"cell_type": "markdown",
"id": "4a95a30f-b3fc-4e2f-b2e7-ccd49cc48ed1",
"metadata": {},
"source": [
"## 计算行向量的逐元素积"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "29eca8ec-ed59-4e60-90bf-8405a4a084e3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-2, -2, -1])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_times_b = np.multiply(a, b) # 计算a和b的逐元素积\n",
"a_times_b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "91a581bf-ca56-44b2-8d21-8ae4a19ee309",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-2, -2, -1])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_times_b_2 = a * b # 使用*操作符计算a和b的逐元素积\n",
"a_times_b_2"
]
},
{
"cell_type": "markdown",
"id": "65964628-e9f9-4626-9a49-adc10d2f4ecf",
"metadata": {},
"source": [
"## 定义两个列向量"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "42eec7be-ccdc-4a9a-be68-0e074793c2a4",
"metadata": {},
"outputs": [],
"source": [
"a_col = np.array([[-2], [1], [1]]) # 定义列向量a_col"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b8757d8c-776a-400c-be13-0b7cac7ffdac",
"metadata": {},
"outputs": [],
"source": [
"b_col = np.array([[1], [-2], [-1]]) # 定义列向量b_col"
]
},
{
"cell_type": "markdown",
"id": "2269b17d-2fc2-47dc-b7eb-4f4ec992502f",
"metadata": {},
"source": [
"## 计算列向量的逐元素积"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c9f74e22-8432-4c40-bb9b-36f0a2d5e064",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2],\n",
" [-2],\n",
" [-1]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_times_b_col = np.multiply(a_col, b_col) # 计算a_col和b_col的逐元素积\n",
"a_times_b_col"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7cd5c6c6-b2e3-4027-8eaa-7656594db66b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2],\n",
" [-2],\n",
" [-1]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a_times_b_col_2 = a_col * b_col # 使用*操作符计算a_col和b_col的逐元素积\n",
"a_times_b_col_2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,100 @@
###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2025
###############
## 导入必要的库
import streamlit as st # 引入Streamlit库,用于创建交互式Web应用
import numpy as np # 引入Numpy库,用于数值计算
from plotly.subplots import make_subplots # 引入Plotly的子图模块,用于创建复杂布局
import plotly.graph_objects as go # 引入Plotly的图形对象模块,用于绘图
import plotly.express as px # 引入Plotly Express模块,用于快速绘图
## 定义自定义函数 bmatrix
# 该函数将一个numpy数组转换为LaTeX格式的bmatrix矩阵
def bmatrix(a):
"""返回一个LaTeX格式的bmatrix矩阵表示形式
:a: numpy数组
:returns: 返回字符串形式的LaTeX bmatrix矩阵
"""
if len(a.shape) > 2: # 如果数组维度大于2,抛出异常
raise ValueError('bmatrix最多只能展示二维数据')
lines = str(a).replace('[', '').replace(']', '').splitlines() # 移除括号并分行
rv = [r'\begin{bmatrix}'] # LaTeX矩阵开始标记
rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines] # 拼接矩阵中的元素
rv += [r'\end{bmatrix}'] # LaTeX矩阵结束标记
return '\n'.join(rv) # 返回完整的LaTeX矩阵字符串
## 侧边栏设置
with st.sidebar: # 创建侧边栏
# 创建滑块以选择a的行数,范围为3到6,默认值为3
num_a = st.slider('Number of rows, a:', 3, 6, step=1)
# 创建滑块以选择b的行数,范围为3到6,默认值为3
num_b = st.slider('Number of rows, b:', 3, 6, step=1)
# 随机生成矩阵a,值在0到1之间,形状为(num_a, 1)
a = np.random.uniform(0, 1, num_a).reshape((-1, 1))
a = np.round(a, 1) # 将矩阵a的值保留一位小数
# 随机生成矩阵b,值在0到1之间,形状为(num_b, 1)
b = np.random.uniform(0, 1, num_b).reshape((-1, 1))
b = np.round(b, 1) # 将矩阵b的值保留一位小数
show_number = False # 初始化显示数值的变量为False
with st.sidebar: # 在侧边栏中添加复选框
# 创建复选框用于控制是否显示矩阵数值
show_number = st.checkbox('Display values')
# 计算a与b转置的张量积
tensor_a_b = a @ b.T
## 数据可视化部分
# 在Streamlit中以LaTeX格式展示矩阵a
st.latex('a = ' + bmatrix(a))
# 在Streamlit中以LaTeX格式展示矩阵b
st.latex('b = ' + bmatrix(b))
# 展示张量积公式
st.latex('a \\otimes b = ab^{T}')
# 展示矩阵a与b的张量积计算结果
st.latex(bmatrix(a) + '@' + bmatrix(b.T) + ' = ' + bmatrix(tensor_a_b))
# 创建三个列布局
col1, col2, col3 = st.columns(3)
# 在第一列展示矩阵a的热图
with col1:
fig_a = px.imshow(a, text_auto=show_number, # 绘制热图,并根据show_number显示数值
color_continuous_scale='viridis', # 设置颜色映射
aspect='equal') # 设置宽高比为1:1
fig_a.update_layout(height=400, width=300) # 设置图形大小
fig_a.layout.coloraxis.showscale = False # 隐藏颜色条
st.plotly_chart(fig_a) # 在Streamlit中显示图形
# 在第二列展示矩阵b的热图
with col2:
fig_b = px.imshow(b, text_auto=show_number, # 绘制热图,并根据show_number显示数值
color_continuous_scale='viridis', # 设置颜色映射
aspect='equal') # 设置宽高比为1:1
fig_b.update_layout(height=400, width=300) # 设置图形大小
fig_b.layout.coloraxis.showscale = False # 隐藏颜色条
st.plotly_chart(fig_b) # 在Streamlit中显示图形
# 在第三列展示张量积矩阵的热图
with col3:
fig_ab = px.imshow(tensor_a_b, text_auto=show_number, # 绘制热图,并根据show_number显示数值
color_continuous_scale='viridis', # 设置颜色映射
aspect='equal') # 设置宽高比为1:1
fig_ab.update_layout(height=400, width=400) # 设置图形大小
fig_ab.layout.coloraxis.showscale = False # 隐藏颜色条
st.plotly_chart(fig_ab) # 在Streamlit中显示图形