Skip to content

Commit 0d65173

Browse files
committed
Add chapters
1 parent cbbe085 commit 0d65173

File tree

76 files changed

+10499
-86
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+10499
-86
lines changed

chapter01_basic/01_notebook.ipynb

Lines changed: 320 additions & 0 deletions
Large diffs are not rendered by default.

chapter01_basic/02_pandas.ipynb

Lines changed: 242 additions & 0 deletions
Large diffs are not rendered by default.

chapter01_basic/03_numpy.ipynb

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Introducing the multidimensional array in NumPy for fast array computations"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import random\n",
17+
"import numpy as np"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 2,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"n = 1000000\n",
27+
"x = [random.random() for _ in range(n)]\n",
28+
"y = [random.random() for _ in range(n)]"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 3,
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/plain": [
39+
"([0.926, 0.722, 0.962], [0.291, 0.339, 0.819])"
40+
]
41+
},
42+
"execution_count": 3,
43+
"metadata": {},
44+
"output_type": "execute_result"
45+
}
46+
],
47+
"source": [
48+
"x[:3], y[:3]"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 4,
54+
"metadata": {},
55+
"outputs": [
56+
{
57+
"data": {
58+
"text/plain": [
59+
"[1.217, 1.061, 1.781]"
60+
]
61+
},
62+
"execution_count": 4,
63+
"metadata": {},
64+
"output_type": "execute_result"
65+
}
66+
],
67+
"source": [
68+
"z = [x[i] + y[i] for i in range(n)]\n",
69+
"z[:3]"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 5,
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"101 ms ± 5.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"%timeit [x[i] + y[i] for i in range(n)]"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 6,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"xa = np.array(x)\n",
96+
"ya = np.array(y)"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 7,
102+
"metadata": {},
103+
"outputs": [
104+
{
105+
"data": {
106+
"text/plain": [
107+
"array([ 0.926, 0.722, 0.962])"
108+
]
109+
},
110+
"execution_count": 7,
111+
"metadata": {},
112+
"output_type": "execute_result"
113+
}
114+
],
115+
"source": [
116+
"xa[:3]"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 8,
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"data": {
126+
"text/plain": [
127+
"array([ 1.217, 1.061, 1.781])"
128+
]
129+
},
130+
"execution_count": 8,
131+
"metadata": {},
132+
"output_type": "execute_result"
133+
}
134+
],
135+
"source": [
136+
"za = xa + ya\n",
137+
"za[:3]"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 9,
143+
"metadata": {},
144+
"outputs": [
145+
{
146+
"name": "stdout",
147+
"output_type": "stream",
148+
"text": [
149+
"1.09 ms ± 37.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
150+
]
151+
}
152+
],
153+
"source": [
154+
"%timeit xa + ya"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 10,
160+
"metadata": {},
161+
"outputs": [
162+
{
163+
"name": "stdout",
164+
"output_type": "stream",
165+
"text": [
166+
"3.94 ms ± 4.44 µs per loop (mean ± std. dev. of 7 runs\n",
167+
" 100 loops each)\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"%timeit sum(x) # pure Python"
173+
]
174+
},
175+
{
176+
"cell_type": "code",
177+
"execution_count": 11,
178+
"metadata": {},
179+
"outputs": [
180+
{
181+
"name": "stdout",
182+
"output_type": "stream",
183+
"text": [
184+
"298 µs ± 4.62 µs per loop (mean ± std. dev. of 7 runs,\n",
185+
" 1000 loops each)\n"
186+
]
187+
}
188+
],
189+
"source": [
190+
"%timeit np.sum(xa) # NumPy"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": 12,
196+
"metadata": {},
197+
"outputs": [],
198+
"source": [
199+
"d = [abs(x[i] - y[j])\n",
200+
" for i in range(1000)\n",
201+
" for j in range(1000)]"
202+
]
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": 13,
207+
"metadata": {},
208+
"outputs": [
209+
{
210+
"data": {
211+
"text/plain": [
212+
"[0.635, 0.587, 0.106]"
213+
]
214+
},
215+
"execution_count": 13,
216+
"metadata": {},
217+
"output_type": "execute_result"
218+
}
219+
],
220+
"source": [
221+
"d[:3]"
222+
]
223+
},
224+
{
225+
"cell_type": "code",
226+
"execution_count": 14,
227+
"metadata": {},
228+
"outputs": [],
229+
"source": [
230+
"da = np.abs(xa[:1000, np.newaxis] - ya[:1000])"
231+
]
232+
},
233+
{
234+
"cell_type": "code",
235+
"execution_count": 15,
236+
"metadata": {},
237+
"outputs": [
238+
{
239+
"data": {
240+
"text/plain": [
241+
"array([[ 0.635, 0.587, ..., 0.849, 0.046],\n",
242+
" [ 0.431, 0.383, ..., 0.646, 0.158],\n",
243+
" ...,\n",
244+
" [ 0.024, 0.024, ..., 0.238, 0.566],\n",
245+
" [ 0.081, 0.033, ..., 0.295, 0.509]])"
246+
]
247+
},
248+
"execution_count": 15,
249+
"metadata": {},
250+
"output_type": "execute_result"
251+
}
252+
],
253+
"source": [
254+
"da"
255+
]
256+
},
257+
{
258+
"cell_type": "code",
259+
"execution_count": 16,
260+
"metadata": {},
261+
"outputs": [
262+
{
263+
"name": "stdout",
264+
"output_type": "stream",
265+
"text": [
266+
"134 ms ± 1.79 ms per loop (mean ± std. dev. of 7 runs,\n",
267+
" 10 loops each)\n",
268+
" 1000 loops each)\n"
269+
]
270+
}
271+
],
272+
"source": [
273+
"%timeit [abs(x[i] - y[j]) \\\n",
274+
" for i in range(1000) \\\n",
275+
" for j in range(1000)]"
276+
]
277+
},
278+
{
279+
"cell_type": "code",
280+
"execution_count": 17,
281+
"metadata": {},
282+
"outputs": [
283+
{
284+
"name": "stdout",
285+
"output_type": "stream",
286+
"text": [
287+
" 10 loops each)\n",
288+
"1.54 ms ± 48.9 µs per loop (mean ± std. dev. of 7 runs\n",
289+
" 1000 loops each)\n"
290+
]
291+
}
292+
],
293+
"source": [
294+
"%timeit np.abs(xa[:1000, np.newaxis] - ya[:1000])"
295+
]
296+
}
297+
],
298+
"metadata": {},
299+
"nbformat": 4,
300+
"nbformat_minor": 2
301+
}

0 commit comments

Comments
 (0)