mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-02-03 02:24:03 +08:00
315 lines
6.8 KiB
Plaintext
315 lines
6.8 KiB
Plaintext
{
|
||
"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
|
||
}
|