Skip to content

Commit 16e1b75

Browse files
authored
Add files via upload
1 parent 2a203ab commit 16e1b75

25 files changed

+4232
-0
lines changed

LA_00_正文前_V2.pdf

535 KB
Binary file not shown.

LA_01_01_什么是向量.pdf

923 KB
Binary file not shown.

LA_01_02_坐标系.pdf

17.5 KB
Binary file not shown.

LA_01_03_向量加减法.pdf

21.4 KB
Binary file not shown.

LA_01_04_向量标量乘法.pdf

19.2 KB
Binary file not shown.

LA_01_05_01.ipynb

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 01\n",
9+
"\n",
10+
"# 单位向量\n",
11+
"《线性代数》 | 鸢尾花书:数学不难"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
17+
"metadata": {},
18+
"source": [
19+
"这段代码的核心目标是计算向量 $a$ 的 **L2 范数(即欧几里得范数)**,然后基于此计算单位向量,并最终验证单位向量与原始长度的关系。数学上,该过程涉及向量的归一化和数乘运算,具体分析如下:\n",
20+
"\n",
21+
"### 1. 计算向量 $a$ 的 L2 范数\n",
22+
"\n",
23+
"首先,给定二维向量:\n",
24+
"\n",
25+
"$$\n",
26+
"a = \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix}\n",
27+
"$$\n",
28+
"\n",
29+
"L2 范数(或欧几里得范数)定义如下:\n",
30+
"\n",
31+
"$$\n",
32+
"\\|a\\|_2 = \\sqrt{3^2 + 4^2} = \\sqrt{9 + 16} = \\sqrt{25} = 5\n",
33+
"$$\n",
34+
"\n",
35+
"代码中的 `np.linalg.norm(a)` 计算了该范数,并将其存储在变量 `norm_a` 中。\n",
36+
"\n",
37+
"### 2. 计算单位向量\n",
38+
"\n",
39+
"单位向量的定义是 **原始向量除以其范数**,即:\n",
40+
"\n",
41+
"$$\n",
42+
"\\hat{a} = \\frac{a}{\\|a\\|_2} = \\frac{1}{5} \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix} = \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix}\n",
43+
"$$\n",
44+
"\n",
45+
"代码中 `unit_a = a / norm_a` 实现了这一计算,得到向量 $\\hat{a}$,它的特点是方向与 $a$ 相同,但长度为 1:\n",
46+
"\n",
47+
"$$\n",
48+
"\\|\\hat{a}\\|_2 = \\sqrt{0.6^2 + 0.8^2} = \\sqrt{0.36 + 0.64} = \\sqrt{1} = 1\n",
49+
"$$\n",
50+
"\n",
51+
"### 3. 长度 $\\times$ 方向向量\n",
52+
"\n",
53+
"在向量运算中,如果将单位向量 $\\hat{a}$ 乘以原始向量的范数 $\\|a\\|_2$,理论上应该得到原向量:\n",
54+
"\n",
55+
"$$\n",
56+
"\\|a\\|_2 \\cdot \\hat{a} = 5 \\cdot \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix} = \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix} = a\n",
57+
"$$\n",
58+
"\n",
59+
"代码中 `norm_a * unit_a` 计算了这个结果,并验证了单位向量的正确性。\n",
60+
"\n",
61+
"### 结论\n",
62+
"\n",
63+
"1. `np.linalg.norm(a)` 计算向量 $a$ 的模长,结果为 $5$。\n",
64+
"2. `a / norm_a` 计算单位向量 $\\hat{a}$,即 **保持方向不变但长度为 1**。\n",
65+
"3. `norm_a * unit_a` 验证单位向量乘以原范数是否能复原向量 $a$,结果证明计算正确。\n",
66+
"\n",
67+
"这段代码展示了 **向量归一化** 的基本数学原理,在机器学习、计算机视觉和物理建模等领域都有重要应用,例如方向向量的计算、余弦相似度、梯度归一化等。"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"id": "9e5f832e-6b83-4973-88a7-52f63fe92b0e",
73+
"metadata": {},
74+
"source": [
75+
"## 初始化"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 4,
81+
"id": "2889d4f3-5ebd-46aa-885b-99bbf6e744aa",
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"import numpy as np"
86+
]
87+
},
88+
{
89+
"cell_type": "markdown",
90+
"id": "fd7c49d7-03ab-4b61-9326-535ba2eae184",
91+
"metadata": {},
92+
"source": [
93+
"## 定义向量 a"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": 6,
99+
"id": "b20fbb44-18d3-43f5-ae82-a44b37cf266b",
100+
"metadata": {},
101+
"outputs": [],
102+
"source": [
103+
"a = np.array([3, 4])\n",
104+
"a"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"id": "9633ae00-50e4-4d75-8cd4-7c20150ca580",
110+
"metadata": {},
111+
"source": [
112+
"## 计算向量的长度"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 8,
118+
"id": "78a0127a-255d-43b6-8931-68cb32bfaf76",
119+
"metadata": {},
120+
"outputs": [],
121+
"source": [
122+
"norm_a = np.linalg.norm(a)"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"id": "535e230f-6131-41d1-867e-2ade3ee63eb4",
128+
"metadata": {},
129+
"source": [
130+
"## 计算单位向量"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": 10,
136+
"id": "6bc32359-2ea2-4312-a760-89c370e868f0",
137+
"metadata": {},
138+
"outputs": [
139+
{
140+
"data": {
141+
"text/plain": [
142+
"array([0.6, 0.8])"
143+
]
144+
},
145+
"execution_count": 10,
146+
"metadata": {},
147+
"output_type": "execute_result"
148+
}
149+
],
150+
"source": [
151+
"unit_a = a / norm_a\n",
152+
"unit_a"
153+
]
154+
},
155+
{
156+
"cell_type": "markdown",
157+
"id": "ce0a2a1d-8fe1-40f0-9ef0-716fdced62fd",
158+
"metadata": {},
159+
"source": [
160+
"## 长度 $\\times$ 方向向量"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 12,
166+
"id": "954b7525-dcef-47cb-af13-40c741ca3891",
167+
"metadata": {},
168+
"outputs": [
169+
{
170+
"data": {
171+
"text/plain": [
172+
"array([3., 4.])"
173+
]
174+
},
175+
"execution_count": 12,
176+
"metadata": {},
177+
"output_type": "execute_result"
178+
}
179+
],
180+
"source": [
181+
"norm_a * unit_a"
182+
]
183+
},
184+
{
185+
"cell_type": "markdown",
186+
"id": "f48c7a73-872b-403c-9d99-86e7ef6635bd",
187+
"metadata": {},
188+
"source": [
189+
"作者\t**生姜DrGinger** \n",
190+
"脚本\t**生姜DrGinger** \n",
191+
"视频\t**崔崔CuiCui** \n",
192+
"开源资源\t[**GitHub**](https://github.com/Visualize-ML) \n",
193+
"平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
194+
"\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
195+
"\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466) "
196+
]
197+
}
198+
],
199+
"metadata": {
200+
"kernelspec": {
201+
"display_name": "Python [conda env:base] *",
202+
"language": "python",
203+
"name": "conda-base-py"
204+
},
205+
"language_info": {
206+
"codemirror_mode": {
207+
"name": "ipython",
208+
"version": 3
209+
},
210+
"file_extension": ".py",
211+
"mimetype": "text/x-python",
212+
"name": "python",
213+
"nbconvert_exporter": "python",
214+
"pygments_lexer": "ipython3",
215+
"version": "3.12.7"
216+
}
217+
},
218+
"nbformat": 4,
219+
"nbformat_minor": 5
220+
}

0 commit comments

Comments
 (0)