Skip to content

Commit 18f39ec

Browse files
committed
更新第三章:数据科学。
1 parent 10772c0 commit 18f39ec

24 files changed

+4680
-1115
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"source": [
9+
"# Numpy数组\n",
10+
"\n",
11+
"\n",
12+
"## 数组:array\n",
13+
"\n",
14+
"\n",
15+
"很多其他科学计算的第三方库都是以Numpy为基础建立的。\n",
16+
"\n",
17+
"Numpy的一个重要特性是它的数组计算。\n",
18+
"\n",
19+
"\n",
20+
"\n",
21+
"\n",
22+
"使用前一定要先导入 Numpy 包,导入的方法有以下几种:\n",
23+
"```\n",
24+
"import numpy\n",
25+
"import numpy as np\n",
26+
"from numpy import *\n",
27+
"from numpy import array, sin\n",
28+
"```\n",
29+
"\n",
30+
"导入numpy,最常用为这种:"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 1,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"import numpy as np"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"假如我们想将列表中的每个元素增加1,但列表不支持这样的操作(报错):"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 2,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"a = [1, 2]\n",
56+
"\n",
57+
"# a + 1 # 报错"
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"使用numpy.array:"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 3,
70+
"metadata": {},
71+
"outputs": [
72+
{
73+
"data": {
74+
"text/plain": [
75+
"array([1, 2])"
76+
]
77+
},
78+
"execution_count": 3,
79+
"metadata": {},
80+
"output_type": "execute_result"
81+
}
82+
],
83+
"source": [
84+
"a = np.array(a)\n",
85+
"a # [1 2]"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 4,
91+
"metadata": {},
92+
"outputs": [
93+
{
94+
"data": {
95+
"text/plain": [
96+
"array([2, 3])"
97+
]
98+
},
99+
"execution_count": 4,
100+
"metadata": {},
101+
"output_type": "execute_result"
102+
}
103+
],
104+
"source": [
105+
"b = a + 1\n",
106+
"b # array([2,3])"
107+
]
108+
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"与另一个 array 相加,得到对应元素相加的结果:"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 5,
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"name": "stdout",
123+
"output_type": "stream",
124+
"text": [
125+
"[3 5]\n",
126+
"[2 6]\n",
127+
"[1 8]\n"
128+
]
129+
}
130+
],
131+
"source": [
132+
"c = a + b\n",
133+
"print(c) # array([3,5])\n",
134+
"\n",
135+
"# 对应元素相乘:\n",
136+
"print(a * b) # [2 6]\n",
137+
"\n",
138+
"# 对应元素乘方:\n",
139+
"print(a ** b) # [1 8]"
140+
]
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"metadata": {},
145+
"source": [
146+
"### 提取数组中的元素"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": 6,
152+
"metadata": {},
153+
"outputs": [
154+
{
155+
"name": "stdout",
156+
"output_type": "stream",
157+
"text": [
158+
"1\n",
159+
"[1 2]\n",
160+
"[3 4]\n",
161+
"[4 6]\n"
162+
]
163+
}
164+
],
165+
"source": [
166+
"# 提取第一个\n",
167+
"a = np.array([1, 2, 3, 4])\n",
168+
"print(a[0]) # 1\n",
169+
"\n",
170+
"# 提取前两个元素:\n",
171+
"print(a[:2]) # [1 2]\n",
172+
"\n",
173+
"# 最后两个元素\n",
174+
"print(a[-2:]) # [3 4]\n",
175+
"\n",
176+
"# 相加:\n",
177+
"print(a[:2] + a[-2:]) # [4 6]"
178+
]
179+
},
180+
{
181+
"cell_type": "markdown",
182+
"metadata": {},
183+
"source": [
184+
"### 修改数组形状\n",
185+
"\n",
186+
"查看array的形状:"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": 7,
192+
"metadata": {},
193+
"outputs": [
194+
{
195+
"data": {
196+
"text/plain": [
197+
"(4,)"
198+
]
199+
},
200+
"execution_count": 7,
201+
"metadata": {},
202+
"output_type": "execute_result"
203+
}
204+
],
205+
"source": [
206+
"b = a.shape\n",
207+
"b # (4,)"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": 8,
213+
"metadata": {},
214+
"outputs": [
215+
{
216+
"data": {
217+
"text/plain": [
218+
"array([[1, 2],\n",
219+
" [3, 4]])"
220+
]
221+
},
222+
"execution_count": 8,
223+
"metadata": {},
224+
"output_type": "execute_result"
225+
}
226+
],
227+
"source": [
228+
"# 修改 array 的形状:\n",
229+
"a.shape = 2, 2\n",
230+
"a\n",
231+
"# [[1 2]\n",
232+
"# [3 4]]"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 9,
238+
"metadata": {},
239+
"outputs": [
240+
{
241+
"data": {
242+
"text/plain": [
243+
"array([[2, 4],\n",
244+
" [6, 8]])"
245+
]
246+
},
247+
"execution_count": 9,
248+
"metadata": {},
249+
"output_type": "execute_result"
250+
}
251+
],
252+
"source": [
253+
"# 多维数组\n",
254+
"# a 现在变成了一个二维的数组,可以进行加法:\n",
255+
"a + a\n",
256+
"# [[2 4]\n",
257+
"# [6 8]]"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": 10,
263+
"metadata": {},
264+
"outputs": [
265+
{
266+
"data": {
267+
"text/plain": [
268+
"array([[ 1, 4],\n",
269+
" [ 9, 16]])"
270+
]
271+
},
272+
"execution_count": 10,
273+
"metadata": {},
274+
"output_type": "execute_result"
275+
}
276+
],
277+
"source": [
278+
"# 乘法仍然是对应元素的乘积,并不是按照矩阵乘法来计算:\n",
279+
"a * a\n",
280+
"# [[ 1 4]\n",
281+
"# [ 9 16]]"
282+
]
283+
},
284+
{
285+
"cell_type": "markdown",
286+
"metadata": {},
287+
"source": [
288+
"本节完。"
289+
]
290+
}
291+
],
292+
"metadata": {
293+
"kernelspec": {
294+
"display_name": "Python 3",
295+
"language": "python",
296+
"name": "python3"
297+
},
298+
"language_info": {
299+
"codemirror_mode": {
300+
"name": "ipython",
301+
"version": 3
302+
},
303+
"file_extension": ".py",
304+
"mimetype": "text/x-python",
305+
"name": "python",
306+
"nbconvert_exporter": "python",
307+
"pygments_lexer": "ipython3",
308+
"version": "3.8.8"
309+
}
310+
},
311+
"nbformat": 4,
312+
"nbformat_minor": 1
313+
}

0 commit comments

Comments
 (0)