Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.2 Funktions, Conditionals, and Iteration in Python\n",
"\n",
"Let us create a Python function, and call it from a loop."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def HelloWorldXY(x, y):\n",
" if (x < 10):\n",
" print(\"Hello World, x was < 10\")\n",
" elif (x < 20):\n",
" print(\"Hello World, x was >= 10 but < 20\")\n",
" else:\n",
" print(\"Hello World, x was >= 20\")\n",
" return x + y\n",
"\n",
"print(HelloWorldXY(1,2))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us call the function `HelloWorldXY()` from a loop:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Now running with i: 8\n"
]
},
{
"ename": "NameError",
"evalue": "name 'HelloWorldXY' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-1-7ea2350df544>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m8\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m25\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# i=8, 13, 18, 23 (start, stop, step)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"\\n--- Now running with i: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mHelloWorldXY\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Result from HelloWorld: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'HelloWorldXY' is not defined"
]
}
],
"source": [
"for i in range(8, 25, 5): # i=8, 13, 18, 23 (start, stop, step)\n",
" print(\"\\n--- Now running with i: {}\".format(i))\n",
" r = HelloWorldXY(i,i)\n",
" print(\"Result from HelloWorld: {}\".format(r))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want a loop starting at 0 to 2 (exclusive) you could do any of the following:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iterate over the items. `range(2)` is like a list [0,1].\n",
"0\n",
"1\n",
"Iterate over an actual list.\n",
"0\n",
"1\n",
"While works\n",
"0\n",
"1\n",
"Python supports standard key words like continue and break\n",
"Entered while\n",
"while broken\n"
]
}
],
"source": [
"print(\"Iterate over the items. `range(2)` is like a list [0,1].\")\n",
"for i in range(2):\n",
" print(i)\n",
"\n",
"print(\"Iterate over an actual list.\")\n",
"for i in [0,1]:\n",
" print(i)\n",
"\n",
"print(\"While works\")\n",
"i = 0\n",
"while i < 2:\n",
" print(i)\n",
" i += 1\n",
" \n",
"print(\"Python supports standard key words like continue and break\")\n",
"while True:\n",
" print(\"Entered while\")\n",
" break\n",
"print(\"while broken\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.3 Data in Numpy"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shape scaler () \n",
"Shape vector (3,) \n",
"Shape matrix (3, 3) \n",
"Shape tensor (3, 3, 2, 1)\n",
"Type scalar or array <class 'numpy.ndarray'> \n",
"Type after addition with integer <class 'numpy.int64'>\n",
"v[1:] = [ 2 10] \n",
"m[1:][2:] = \n",
" [[5 6]\n",
" [8 9]]\n",
"[ 1 2 10] [[ 1 2 10]] [[ 1 2 10]]\n",
"(3,) (1, 3) (1, 3)\n"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"# Scalar\n",
"s = np.array(5)\n",
"# Vector\n",
"v = np.array([1, 2, 10])\n",
"# Matrix\n",
"m = np.array([[1,2,3], \n",
" [4,5,6], \n",
" [7,8,9]])\n",
"# Tensor:\n",
"t = np.array([[[[1],[2]], [[3],[4]], [[5],[6]]],\n",
" [[[7],[8]], [[9],[10]], [[11],[12]]],\n",
" [[[13],[14]], [[15],[16]], [[17],[17]]]])\n",
"\n",
"# Shape\n",
"print(\"Shape scaler\", s.shape, \"\\nShape vector\", v.shape, \"\\nShape matrix\", m.shape, \"\\nShape tensor\", t.shape)\n",
"\n",
"# Type\n",
"print(\"Type scalar or array\", type(s), \"\\nType after addition with integer\", type(s + 3))\n",
"\n",
"# Slicing\n",
"print(\"v[1:] = \", v[1:], \"\\nm[1:][2:] = \\n\", m[1:,1:])\n",
"\n",
"# Reshape arrays\n",
"x = v.reshape(1, 3)\n",
"y = v[None, :]\n",
"print(v, x, y)\n",
"print(v.shape, x.shape, y.shape)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.4 Element-wise Operations"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[6, 7, 8, 9, 10]\n",
"[ 6 7 8 9 10]\n",
"[30 35 40 45 50] \n",
" [30 35 40 45 50] \n",
"\n",
"a =\n",
" [[1 3]\n",
" [5 7]] \n",
"b =\n",
" [[2 4]\n",
" [6 8]]\n",
"a + b =\n",
" [[ 3 7]\n",
" [11 15]]\n",
"a * b =\n",
" [[ 2 12]\n",
" [30 56]]\n"
]
},
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (2,2) (5,) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-7ddd6b5f4e75>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"a * b =\\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0;31m# Shape mismatch:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"a * values =\\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,2) (5,) "
]
}
],
"source": [
"# The Python way:\n",
"values = [1, 2, 3, 4, 5]\n",
"for i in range(len(values)):\n",
" values[i] += 5\n",
" \n",
"print(values)\n",
"\n",
"# The Numpy way:\n",
"values = np.array([1, 2, 3, 4, 5])\n",
"values += 5\n",
"\n",
"print(values)\n",
"\n",
"# Multiplication\n",
"x = np.multiply(values, 5)\n",
"y = values * 5\n",
"print(x, \"\\n\", y, \"\\n\")\n",
"\n",
"# Element wise matrix operations\n",
"a = np.array([[1,3],[5,7]])\n",
"b = np.array([[2,4],[6,8]])\n",
"print(\"a =\\n\", a, \"\\nb =\\n\", b)\n",
"print(\"a + b =\\n\", a + b)\n",
"print(\"a * b =\\n\", a * b)\n",
"# Shape mismatch:\n",
"print(\"a * values =\\n\", a * values)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Numpy Matrix Multiplication\n",
"Recap element-wise multiplication:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"m =\n",
" [[1 2 3]\n",
" [4 5 6]] \n",
"n =\n",
" [[0.25 0.5 0.75]\n",
" [1. 1.25 1.5 ]]\n",
"x =\n",
" [[0.25 1. 2.25]\n",
" [4. 6.25 9. ]] \n",
"y =\n",
" [[0.25 1. 2.25]\n",
" [4. 6.25 9. ]]\n"
]
}
],
"source": [
"# Elementwise recap:\n",
"m = np.array([[1,2,3],[4,5,6]])\n",
"# Scalar multiplication\n",
"n = m * 0.25\n",
"# Python Elementwise matrix multiplication\n",
"x = m * n\n",
"# Numpy Elementwise matrix multiplication\n",
"y = np.multiply(m, n)\n",
"\n",
"print(\"m =\\n\", m, \"\\nn =\\n\", n)\n",
"print(\"x =\\n\", x, \"\\ny =\\n\", y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Matrix Product:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a =\n",
" [[1 2 3 4]\n",
" [5 6 7 8]] \n",
"a.shape =\n",
" (2, 4) \n",
"b =\n",
" [[ 1 2 3]\n",
" [ 4 5 6]\n",
" [ 7 8 9]\n",
" [10 11 12]] \n",
"b.shape =\n",
" (4, 3)\n",
"c = \n",
" [[ 70 80 90]\n",
" [158 184 210]] \n",
"c.shape =\n",
" (2, 3)\n",
"d = \n",
" [[ 70 80 90]\n",
" [158 184 210]] \n",
"d.shape =\n",
" (2, 3)\n"
]
}
],
"source": [
"\"\"\" Using np.matmul \"\"\"\n",
"a = np.array([[1,2,3,4],[5,6,7,8]])\n",
"b = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])\n",
"\n",
"print(\"a =\\n\", a, \"\\na.shape =\\n\", a.shape, \"\\nb =\\n\", b, \"\\nb.shape =\\n\", b.shape)\n",
"\n",
"# Matrix product\n",
"c = np.matmul(a, b)\n",
"print(\"c = \\n\", c, \"\\nc.shape =\\n\", c.shape)\n",
"\n",
"# Dimension mismatch:\n",
"# print(np.matmul(b, a))\n",
"\"\"\" Using np.dot \"\"\"\n",
"d = np.dot(a, b)\n",
"print(\"d = \\n\", d, \"\\nd.shape =\\n\", d.shape)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transpose"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"m = \n",
" [[ 1 2 3 4]\n",
" [ 5 6 7 8]\n",
" [ 9 10 11 12]] \n",
"m.T = \n",
" [[ 1 5 9]\n",
" [ 2 6 10]\n",
" [ 3 7 11]\n",
" [ 4 8 12]]\n",
"m = \n",
" [[ 1 2 3 4]\n",
" [ 5 6 7 200]\n",
" [ 9 10 11 12]] \n",
"m_t = \n",
" [[ 1 5 9]\n",
" [ 2 6 10]\n",
" [ 3 7 11]\n",
" [ 4 200 12]]\n",
"entries [3][1], [1][3], respectively are edited in both matrices\n"
]
}
],
"source": [
"m = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n",
"print(\"m = \\n\", m,\"\\nm.T = \\n\", m.T)\n",
"\n",
"# note how the transposed matrix is not a copy of the original:\n",
"m_t = m.T\n",
"m_t[3][1] = 200\n",
"print(\"m = \\n\", m, \"\\nm_t = \\n\", m_t)\n",
"print(\"entries [3][1], [1][3], respectively are edited in both matrices\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A real use case"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[-0.27 0.45 0.64 0.31]] (1, 4)\n",
"[[ 0.02 0.001 -0.03 0.036]\n",
" [ 0.04 -0.003 0.025 0.009]\n",
" [ 0.012 -0.045 0.28 -0.067]] (3, 4)\n",
"Matrix multiplication gives:\n",
" [[-0.01299 0.00664 0.13494]] \n",
"or, equivalently:\n",
" [[-0.01299]\n",
" [ 0.00664]\n",
" [ 0.13494]]\n"
]
}
],
"source": [
"inputs = np.array([[-0.27, 0.45, 0.64, 0.31]])\n",
"print(inputs, inputs.shape)\n",
"\n",
"weights = np.array([[0.02, 0.001, -0.03, 0.036], \n",
" [0.04, -0.003, 0.025, 0.009], \n",
" [0.012, -0.045, 0.28, -0.067]])\n",
"print(weights, weights.shape)\n",
"\n",
"print(\"Matrix multiplication gives:\\n\", np.matmul(inputs, weights.T), \"\\nor, equivalently:\\n\", np.matmul(weights, inputs.T))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Some more useful Numpy methods"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Showing some basic math on arrays\n",
"Max: 4\n",
"Average: 2.0\n",
"Max index: 2\n",
"\n",
"Use numpy to create a [3,3] dimension array with random number\n",
"[[0.92371879 0.58999086 0.76979433]\n",
" [0.48733651 0.44698554 0.91494542]\n",
" [0.59130531 0.69632003 0.32785335]]\n"
]
}
],
"source": [
"print(\"\\nShowing some basic math on arrays\")\n",
"\n",
"b = np.array([0,1,4,3,2])\n",
"print(\"Max: {}\".format(np.max(b)))\n",
"print(\"Average: {}\".format(np.average(b)))\n",
"print(\"Max index: {}\".format(np.argmax(b)))\n",
"\n",
"print(\"\\nUse numpy to create a [3,3] dimension array with random number\")\n",
"c = np.random.rand(3, 3)\n",
"print(c)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}