Skip to content

Commit d087f17

Browse files
authored
Add files via upload
1 parent 7832859 commit d087f17

File tree

4 files changed

+854
-0
lines changed

4 files changed

+854
-0
lines changed

LA_01_02_01.ipynb

+301
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
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+
"# 使用numpy.meshgrid() 创建二维网格数组\n",
11+
"《线性代数》 | 鸢尾花书:数学不难"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
17+
"metadata": {},
18+
"source": [
19+
"这段代码的数学核心是创建一个二维网格,其中 $x_1$ 和 $x_2$ 分别表示平面上的两个变量。我们可以从数学角度详细描述其作用如下:\n",
20+
"\n",
21+
"首先,$x_1$ 和 $x_2$ 是从 $-4$ 到 $4$ 之间等间距取值的数列,步长为 $1$:\n",
22+
"\\[\n",
23+
"x_1 = x_2 = \\{-4, -3, -2, -1, 0, 1, 2, 3, 4\\}\n",
24+
"\\]\n",
25+
"这两个数列定义了一系列离散的坐标点,它们将用于构造一个网格。\n",
26+
"\n",
27+
"**网格生成(Meshgrid)**\n",
28+
"\n",
29+
"使用 $\\text{np.meshgrid}(x_1, x_2)$,代码生成了两个矩阵 $xx_1$ 和 $xx_2$,它们的形状均为 $(9, 9)$。这些矩阵的作用是形成一个二维坐标网格,其中:\n",
30+
"- $xx_1$ 的每一行都与 $x_1$ 相同,表示 $x_1$ 方向上的坐标重复扩展。\n",
31+
"- $xx_2$ 的每一列都与 $x_2$ 相同,表示 $x_2$ 方向上的坐标重复扩展。\n",
32+
"\n",
33+
"用数学表示,网格上的每个点 $(x_1, x_2)$ 由:\n",
34+
"\\[\n",
35+
"xx_1(i, j) = x_1(j), \\quad xx_2(i, j) = x_2(i)\n",
36+
"\\]\n",
37+
"其中 $i, j$ 分别是矩阵的行索引和列索引。\n",
38+
"\n",
39+
"最终,$xx_1$ 和 $xx_2$ 可以被看作是二维平面上 $x_1$ 和 $x_2$ 轴上的坐标网格,它们常用于绘制等高线图、三维曲面图或者计算某些二维函数值。\n",
40+
"\n",
41+
"**示例网格点**\n",
42+
"例如,$(x_1, x_2)$ 的部分取值:\n",
43+
"\\[\n",
44+
"\\begin{aligned}\n",
45+
"xx_1 &= \n",
46+
"\\begin{bmatrix}\n",
47+
"-4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 \\\\\n",
48+
"-4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 \\\\\n",
49+
"\\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots \\\\\n",
50+
"-4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4\n",
51+
"\\end{bmatrix} \\\\\n",
52+
"xx_2 &= \n",
53+
"\\begin{bmatrix}\n",
54+
"-4 & -4 & -4 & -4 & -4 & -4 & -4 & -4 & -4 \\\\\n",
55+
"-3 & -3 & -3 & -3 & -3 & -3 & -3 & -3 & -3 \\\\\n",
56+
"\\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots \\\\\n",
57+
"4 & 4 & 4 & 4 & 4 & 4 & 4 & 4 & 4\n",
58+
"\\end{bmatrix}\n",
59+
"\\end{aligned}\n",
60+
"\\]\n",
61+
"这些矩阵的每个元素对应于二维平面上的一个坐标点 $(x_1, x_2)$,用于后续的数学计算或可视化。\n",
62+
"\n",
63+
"总结来说,这段代码的作用是构建一个二维网格,它定义了在 $[-4,4] \\times [-4,4]$ 这个离散点集上的坐标点分布,为进一步计算函数值或绘图提供基础。"
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"id": "398d0a9c-4884-4750-a4eb-f234428eb21f",
69+
"metadata": {},
70+
"source": [
71+
"## 初始化"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 1,
77+
"id": "592d9f86-794b-4b36-89c3-099726910e65",
78+
"metadata": {},
79+
"outputs": [],
80+
"source": [
81+
"import numpy as np"
82+
]
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"id": "e3b97692-7fe2-4f0d-badf-48d46849d7d6",
87+
"metadata": {},
88+
"source": [
89+
"## 一维数组"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 2,
95+
"id": "ba06a502-610c-49bf-9200-02bb7c7736f1",
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"# 生成 x1 和 x2 的一维数组,范围 [-4, 4],步长为 1\n",
100+
"x1 = np.arange(-4, 5, 1)\n",
101+
"x2 = np.arange(-4, 5, 1)"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"id": "63975350-6c23-4a8e-981a-a8bd775c0710",
107+
"metadata": {},
108+
"source": [
109+
"## 二维网格数据"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 3,
115+
"id": "e87412bc-0d80-43d5-8998-133654a75faf",
116+
"metadata": {},
117+
"outputs": [],
118+
"source": [
119+
"# 生成网格\n",
120+
"xx1, xx2 = np.meshgrid(x1, x2)"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 4,
126+
"id": "954b7525-dcef-47cb-af13-40c741ca3891",
127+
"metadata": {},
128+
"outputs": [
129+
{
130+
"data": {
131+
"text/plain": [
132+
"array([[-4, -3, -2, -1, 0, 1, 2, 3, 4],\n",
133+
" [-4, -3, -2, -1, 0, 1, 2, 3, 4],\n",
134+
" [-4, -3, -2, -1, 0, 1, 2, 3, 4],\n",
135+
" [-4, -3, -2, -1, 0, 1, 2, 3, 4],\n",
136+
" [-4, -3, -2, -1, 0, 1, 2, 3, 4],\n",
137+
" [-4, -3, -2, -1, 0, 1, 2, 3, 4],\n",
138+
" [-4, -3, -2, -1, 0, 1, 2, 3, 4],\n",
139+
" [-4, -3, -2, -1, 0, 1, 2, 3, 4],\n",
140+
" [-4, -3, -2, -1, 0, 1, 2, 3, 4]])"
141+
]
142+
},
143+
"execution_count": 4,
144+
"metadata": {},
145+
"output_type": "execute_result"
146+
}
147+
],
148+
"source": [
149+
"xx1"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": 5,
155+
"id": "9a34e318-63f0-416c-b364-c35b1ed64a71",
156+
"metadata": {},
157+
"outputs": [
158+
{
159+
"data": {
160+
"text/plain": [
161+
"(9, 9)"
162+
]
163+
},
164+
"execution_count": 5,
165+
"metadata": {},
166+
"output_type": "execute_result"
167+
}
168+
],
169+
"source": [
170+
"xx1.shape"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": 6,
176+
"id": "eed8c293-8ede-4c9b-a715-049f9f24ff43",
177+
"metadata": {},
178+
"outputs": [
179+
{
180+
"data": {
181+
"text/plain": [
182+
"2"
183+
]
184+
},
185+
"execution_count": 6,
186+
"metadata": {},
187+
"output_type": "execute_result"
188+
}
189+
],
190+
"source": [
191+
"xx1.ndim"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 7,
197+
"id": "7eb025ba-bf5e-424b-8332-4d7682dd8f52",
198+
"metadata": {},
199+
"outputs": [
200+
{
201+
"data": {
202+
"text/plain": [
203+
"array([[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
204+
" [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
205+
" [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
206+
" [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
207+
" [ 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
208+
" [ 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
209+
" [ 2, 2, 2, 2, 2, 2, 2, 2, 2],\n",
210+
" [ 3, 3, 3, 3, 3, 3, 3, 3, 3],\n",
211+
" [ 4, 4, 4, 4, 4, 4, 4, 4, 4]])"
212+
]
213+
},
214+
"execution_count": 7,
215+
"metadata": {},
216+
"output_type": "execute_result"
217+
}
218+
],
219+
"source": [
220+
"xx2"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": 8,
226+
"id": "69d33c20-60a6-4217-998a-2b5dff3e4f41",
227+
"metadata": {},
228+
"outputs": [
229+
{
230+
"data": {
231+
"text/plain": [
232+
"(9, 9)"
233+
]
234+
},
235+
"execution_count": 8,
236+
"metadata": {},
237+
"output_type": "execute_result"
238+
}
239+
],
240+
"source": [
241+
"xx2.shape"
242+
]
243+
},
244+
{
245+
"cell_type": "code",
246+
"execution_count": 9,
247+
"id": "3857e1c9-ae1b-410f-9bcf-e925b3432eb3",
248+
"metadata": {},
249+
"outputs": [
250+
{
251+
"data": {
252+
"text/plain": [
253+
"2"
254+
]
255+
},
256+
"execution_count": 9,
257+
"metadata": {},
258+
"output_type": "execute_result"
259+
}
260+
],
261+
"source": [
262+
"xx2.ndim"
263+
]
264+
},
265+
{
266+
"cell_type": "markdown",
267+
"id": "3fbd2e7e-62b5-4f02-bba1-93c0081a2318",
268+
"metadata": {},
269+
"source": [
270+
"作者\t**生姜DrGinger** \n",
271+
"脚本\t**生姜DrGinger** \n",
272+
"视频\t**崔崔CuiCui** \n",
273+
"开源资源\t[**GitHub**](https://github.com/Visualize-ML) \n",
274+
"平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
275+
"\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
276+
"\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466) "
277+
]
278+
}
279+
],
280+
"metadata": {
281+
"kernelspec": {
282+
"display_name": "Python [conda env:base] *",
283+
"language": "python",
284+
"name": "conda-base-py"
285+
},
286+
"language_info": {
287+
"codemirror_mode": {
288+
"name": "ipython",
289+
"version": 3
290+
},
291+
"file_extension": ".py",
292+
"mimetype": "text/x-python",
293+
"name": "python",
294+
"nbconvert_exporter": "python",
295+
"pygments_lexer": "ipython3",
296+
"version": "3.12.7"
297+
}
298+
},
299+
"nbformat": 4,
300+
"nbformat_minor": 5
301+
}

0 commit comments

Comments
 (0)