Skip to content

Commit 312309e

Browse files
author
learnp
committed
update nditer tutorial
1 parent 41acf45 commit 312309e

File tree

1 file changed

+172
-22
lines changed

1 file changed

+172
-22
lines changed

numpy/nditer.ipynb

+172-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"<h1 style=\"color:green\" align='center'>Numpy tutorial: iterate numpy array using nditer</h1>"
8+
]
9+
},
310
{
411
"cell_type": "code",
5-
"execution_count": 1,
12+
"execution_count": 27,
613
"metadata": {
714
"collapsed": true
815
},
@@ -13,8 +20,10 @@
1320
},
1421
{
1522
"cell_type": "code",
16-
"execution_count": 26,
17-
"metadata": {},
23+
"execution_count": 28,
24+
"metadata": {
25+
"scrolled": true
26+
},
1827
"outputs": [
1928
{
2029
"data": {
@@ -24,7 +33,7 @@
2433
" [ 8, 9, 10, 11]])"
2534
]
2635
},
27-
"execution_count": 26,
36+
"execution_count": 28,
2837
"metadata": {},
2938
"output_type": "execute_result"
3039
}
@@ -38,12 +47,94 @@
3847
"cell_type": "markdown",
3948
"metadata": {},
4049
"source": [
41-
"<h2 style=\"color:purple\">C style ordering</h2>"
50+
"<h3 style=\"color:purple\">Using normal for loop iteration</h3>"
4251
]
4352
},
4453
{
4554
"cell_type": "code",
46-
"execution_count": 27,
55+
"execution_count": 29,
56+
"metadata": {
57+
"scrolled": false
58+
},
59+
"outputs": [
60+
{
61+
"name": "stdout",
62+
"output_type": "stream",
63+
"text": [
64+
"0\n",
65+
"1\n",
66+
"2\n",
67+
"3\n",
68+
"4\n",
69+
"5\n",
70+
"6\n",
71+
"7\n",
72+
"8\n",
73+
"9\n",
74+
"10\n",
75+
"11\n"
76+
]
77+
}
78+
],
79+
"source": [
80+
"for row in a:\n",
81+
" for cell in row:\n",
82+
" print(cell)"
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"<h3 style=\"color:purple\">For loop with flatten</h3>"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 30,
95+
"metadata": {},
96+
"outputs": [
97+
{
98+
"name": "stdout",
99+
"output_type": "stream",
100+
"text": [
101+
"0\n",
102+
"1\n",
103+
"2\n",
104+
"3\n",
105+
"4\n",
106+
"5\n",
107+
"6\n",
108+
"7\n",
109+
"8\n",
110+
"9\n",
111+
"10\n",
112+
"11\n"
113+
]
114+
}
115+
],
116+
"source": [
117+
"for cell in a.flatten():\n",
118+
" print(cell)"
119+
]
120+
},
121+
{
122+
"cell_type": "markdown",
123+
"metadata": {},
124+
"source": [
125+
"<h1 style=\"color:blue\" align=\"center\">nditer</h1>"
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"<h3 style=\"color:purple\">C style ordering</h3>"
133+
]
134+
},
135+
{
136+
"cell_type": "code",
137+
"execution_count": 31,
47138
"metadata": {
48139
"scrolled": true
49140
},
@@ -81,9 +172,9 @@
81172
},
82173
{
83174
"cell_type": "code",
84-
"execution_count": 28,
175+
"execution_count": 32,
85176
"metadata": {
86-
"scrolled": true
177+
"scrolled": false
87178
},
88179
"outputs": [
89180
{
@@ -114,37 +205,67 @@
114205
"cell_type": "markdown",
115206
"metadata": {},
116207
"source": [
117-
"<h2 style=\"color:purple\">Modify array values while iterating</h2>"
208+
"<h3 style=\"color:purple\">external_loop</h3>"
118209
]
119210
},
120211
{
121212
"cell_type": "code",
122213
"execution_count": 33,
214+
"metadata": {
215+
"scrolled": false
216+
},
217+
"outputs": [
218+
{
219+
"name": "stdout",
220+
"output_type": "stream",
221+
"text": [
222+
"[0 4 8]\n",
223+
"[1 5 9]\n",
224+
"[ 2 6 10]\n",
225+
"[ 3 7 11]\n"
226+
]
227+
}
228+
],
229+
"source": [
230+
"for x in np.nditer(a, flags=['external_loop'],order='F'):\n",
231+
" print(x)"
232+
]
233+
},
234+
{
235+
"cell_type": "markdown",
236+
"metadata": {},
237+
"source": [
238+
"<h2 style=\"color:purple\">Modify array values while iterating</h2>"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": 34,
123244
"metadata": {
124245
"collapsed": true
125246
},
126247
"outputs": [],
127248
"source": [
128249
"for x in np.nditer(a, op_flags=['readwrite']):\n",
129-
" x[...] = x + 1"
250+
" x[...] = x * x"
130251
]
131252
},
132253
{
133254
"cell_type": "code",
134-
"execution_count": 34,
255+
"execution_count": 35,
135256
"metadata": {
136257
"scrolled": true
137258
},
138259
"outputs": [
139260
{
140261
"data": {
141262
"text/plain": [
142-
"array([[ 1, 2, 3, 4],\n",
143-
" [ 5, 6, 7, 8],\n",
144-
" [ 9, 10, 11, 12]])"
263+
"array([[ 0, 1, 4, 9],\n",
264+
" [ 16, 25, 36, 49],\n",
265+
" [ 64, 81, 100, 121]])"
145266
]
146267
},
147-
"execution_count": 34,
268+
"execution_count": 35,
148269
"metadata": {},
149270
"output_type": "execute_result"
150271
}
@@ -162,26 +283,55 @@
162283
},
163284
{
164285
"cell_type": "code",
165-
"execution_count": 35,
286+
"execution_count": 36,
166287
"metadata": {},
167288
"outputs": [
168289
{
169290
"data": {
170291
"text/plain": [
171-
"array([[ 0, 1, 2, 3],\n",
172-
" [ 4, 5, 6, 7],\n",
173-
" [ 8, 9, 10, 11]])"
292+
"array([[ 3],\n",
293+
" [ 7],\n",
294+
" [11]])"
174295
]
175296
},
176-
"execution_count": 35,
297+
"execution_count": 36,
177298
"metadata": {},
178299
"output_type": "execute_result"
179300
}
180301
],
181302
"source": [
182-
"b = np.arange(12).reshape(3,4)\n",
303+
"b = np.arange(3, 15, 4).reshape(3,1)\n",
183304
"b"
184305
]
306+
},
307+
{
308+
"cell_type": "code",
309+
"execution_count": 37,
310+
"metadata": {},
311+
"outputs": [
312+
{
313+
"name": "stdout",
314+
"output_type": "stream",
315+
"text": [
316+
"0 3\n",
317+
"1 3\n",
318+
"4 3\n",
319+
"9 3\n",
320+
"16 7\n",
321+
"25 7\n",
322+
"36 7\n",
323+
"49 7\n",
324+
"64 11\n",
325+
"81 11\n",
326+
"100 11\n",
327+
"121 11\n"
328+
]
329+
}
330+
],
331+
"source": [
332+
"for x, y in np.nditer([a,b]):\n",
333+
" print (x,y)"
334+
]
185335
}
186336
],
187337
"metadata": {
@@ -200,7 +350,7 @@
200350
"name": "python",
201351
"nbconvert_exporter": "python",
202352
"pygments_lexer": "ipython3",
203-
"version": "3.6.1"
353+
"version": "3.6.3"
204354
}
205355
},
206356
"nbformat": 4,

0 commit comments

Comments
 (0)