Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions, Conditionals, and Iteration in Python\n",
"\n",
"Let us create a Python function, and call it from a loop."
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World, x was < 10\n",
"3\n"
]
}
],
"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",
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us call the function `HelloWorldXY()` from a loop:"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--- Now running with i: 8\n",
"Hello World, x was < 10\n",
"Result from HelloWorld: 16\n",
"\n",
"--- Now running with i: 13\n",
"Hello World, x was >= 10 but < 20\n",
"Result from HelloWorld: 26\n",
"\n",
"--- Now running with i: 18\n",
"Hello World, x was >= 10 but < 20\n",
"Result from HelloWorld: 36\n",
"\n",
"--- Now running with i: 23\n",
"Hello World, x was >= 20\n",
"Result from HelloWorld: 46\n"
]
}
],
"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",
"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",
]
}
],
"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)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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(\"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": [
"## NumPy\n",
"\n",
"### Introducing NumPy\n",
"\n",
"Python is convenient, but it can also be slow. However, it does \n",
"allow you to access libraries that execute faster code written in \n",
"languages like C. NumPy is one such library: it provides fast alternatives \n",
"to math operations in Python and is designed to work efficiently with \n",
"groups of numbers - like matrices.\n",
"\n",
"NumPy is a large library and we are only going to scratch the surface \n",
"of it here. If you plan on doing much math with Python, you should \n",
"definitely spend some time exploring its documentation to learn more.\n",
"\n",
"### Importing Numpy\n",
"\n",
"When importing the NumPy library, the convention you will see \n",
"used most often - including here - is to name it `np`, like so:"
]
},
{
"cell_type": "code",
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
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now you can use the library by prefixing the names of functions and \n",
"types with `np`, which you will see in the following examples.\n",
"\n",
"### Data Types and Shapes\n",
"\n",
"The most common way to work with numbers in NumPy is through `ndarray` \n",
"objects. They are similar to Python lists, but can have any number of \n",
"dimensions. Also, `ndarray` supports fast math operations, which \n",
"is just what we want.\n",
"\n",
"Since it can store any number of dimensions, you can use `ndarrays` \n",
"to represent any of the data types : scalars, vectors, \n",
"matrices, or tensors. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Scalars\n",
"\n",
"Scalars in NumPy are a bit more involved than in Python. Instead of \n",
"Python's basic types like `int`, `float`, etc., NumPy lets \n",
"you specify signed and unsigned types, as well as different sizes.\n",
"So instead of Python's `int`, you have access to types \n",
"like `uint8`, `int8`, `uint16`, `int16`, and so on.\n",
"\n",
"These types are important because every object you make \n",
"(vectors, matrices, tensors) eventually stores scalars. And when you \n",
"create a NumPy array, you can specify the type - _but every item in the \n",
"array must have the same type_. In this regard, NumPy arrays are more \n",
"like C arrays than Python lists.\n",
"\n",
"If you want to create a NumPy array that holds a scalar, you do so \n",
"by passing the value to NumPy's `array` function, as follows:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array(5)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Scalar\n",
"s = np.array(5)\n",
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
"s"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can display the number of axes of a NumPy `array` via the `ndim` attribute;\n",
"a scalar array has $0$ axes (`ndim` == 0). The number of axes of an array is also\n",
"called its _rank_. "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.ndim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see the shape of your arrays by checking their `shape` attribute. So if \n",
"you executed this code:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"it would print out the result, an empty pair of parenthesis, `()`. This \n",
"indicates that it has zero dimensions.\n",
"\n",
"Even though scalars are inside arrays, you still use them like a normal scalar. \n",
"So you could type:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.int64"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = s + 3\n",
"type(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and $x$ would now equal $8$. If you were to check the type of \n",
"$x$, you would find it is probably `numPy.int64`, because \n",
"it is working with NumPy types, not Python types.\n",
"\n",
"By the way, even scalar types support most of the array functions. \n",
"So you can call `x.shape` and it would return `()` because \n",
"it has zero dimensions, even though it is not an array. If you tried \n",
"that with a normal Python scalar, you would get an error."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Vectors\n",
"\n",
"To create a vector, you would pass a Python list to the `array` function, like this:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = np.array([1,2,3])\n",
"v"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This vector has three entries and so is called a 3-dimensional vector. If you check a vector's `shape` attribute, it will return a single number representing the \n",
"vector's one-dimensional length. In the above example, "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3,)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Don’t confuse a 3D vector with a 3D array. A 3D vector has only one axis and has three dimensions along its axis, whereas a 3D array has three axes (and may have any number of dimensions along each axis). "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v.ndim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dimensionality can denote either the number of entries along a specific\n",
"axis (as in the case of our 3D vector) or the number of axes in an array (such as a\n",
"3D array), which can be confusing at times.\n",
"\n",
"You can access an element within the vector using indices, like this:"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v[1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"NumPy also supports advanced indexing techniques. For example, to access the items from the \n",
"second element onward, you would say:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 3])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v[1:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"NumPy slicing is quite powerful, \n",
"allowing you to access any combination of items in an `ndarray`. But it can also be a bit complicated, \n",
"so you should read up on it in the documentation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Matrices\n",
"\n",
"You create matrices using `NumPy`'s array function, just you did for vectors. However, instead \n",
"of just passing in a list, you need to supply a list of lists, where each list represents \n",
"a row. So to create a $3\\times 3$ matrix containing the numbers one through nine, you could \n",
"do this:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9]])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m = np.array([[1,2,3], [4,5,6], [7,8,9]])\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The entries from the first axis are called the _rows_, and the entries from \n",
"the second axis are called the _columns_. A matrix thus has two axes or _rank_ 2:"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m.ndim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Checking its shape attribute would return the tuple `(3, 3)` to indicate it has two dimensions, each length $3$:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 3)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can access elements of matrices just like vectors, but using additional index values. So to find \n",
"the number $6$ in the above matrix, you would access"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m[1][2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tensors\n",
"\n",
"Tensors are just like vectors and matrices, but they can have more dimensions. For example, to \n",
"create a $3\\times 3\\times 2\\times 1$ tensor, you could do the following:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[ 1],\n",
" [ 2]],\n",
"\n",
" [[ 3],\n",
" [ 4]],\n",
"\n",
" [[ 5],\n",
" [ 6]]],\n",
"\n",
"\n",
" [[[ 7],\n",
" [ 8]],\n",
"\n",
" [[ 9],\n",
" [10]],\n",
"\n",
" [[11],\n",
" [12]]],\n",
"\n",
"\n",
" [[[13],\n",
" [14]],\n",
"\n",
" [[15],\n",
" [16]],\n",
"\n",
" [[17],\n",
" [17]]]])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = np.array([[[[1],[2]],[[3],[4]],[[5],[6]]],[[[7],[8]],\\\n",
" [[9],[10]],[[11],[12]]],[[[13],[14]],[[15],[16]],[[17],[17]]]])\n",
"t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And `t.shape` returns `(3, 3, 2, 1)` and `t.ndim` indicates that we are dealing with a rank 4 tensor."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 3, 2, 1)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.shape"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.ndim"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can access items just like with matrices, but with more indices. So "
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"16"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t[2][1][1][0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
"### Changing Shapes\n",
"\n",
"Sometimes you will need to change the shape of your data without actually changing \n",
"its contents. For example, you may have a vector, which is one-dimensional, but need \n",
"a matrix, which is two-dimensional. There are two ways you can do that.\n",
"\n",
"Let's say you have the following vector:"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4,)"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v = np.array([1,2,3,4])\n",
"v.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calling `v.shape` would return `(4,)`. But what if you want a $1\\times 4$ matrix? \n",
"You can accomplish that with the `reshape` function, like so:\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3, 4]])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = v.reshape(1,4)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calling `x.shape` would return `(1,4)`. If you wanted a $4\\times 1$ matrix, you \n",
"could do this:"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1],\n",
" [2],\n",
" [3],\n",
" [4]])"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = v.reshape(4,1)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `reshape` function works for more than just adding a dimension of size $1$. Check out its \n",
"documentation for more examples.\n",
"\n",
"One more thing about reshaping NumPy arrays: if you see code from experienced NumPy users, you \n",
"will often see them use a special slicing syntax instead of calling `reshape`. Using this \n",
"syntax, the previous two examples would look like this:"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3, 4]])"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = v[None, :]\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4, 1)"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.shape"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1],\n",
" [2],\n",
" [3],\n",
" [4]])"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = v[:, None]\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4, 1)"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Those lines create a slice that looks at all of the items of `v` but asks NumPy to add a new dimension \n",
"of size $1$ for the associated axis. It may look strange to you now, but it's a common technique so \n",
"it's good to be aware of it. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Element-wise Operations\n",
"\n",
"#### The Python Way\n",
"\n",
"Suppose you had a list of numbers, and you wanted to add $5$ to every item in the list. \n",
"Without NumPy, you might do something like this:"
]
},
{
"cell_type": "code",
"name": "stdout",
"output_type": "stream",
"text": [
"853 ns ± 38.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
]
"values = [1,2,3,4,5]\n",
"for i in range(len(values)):\n",
" values[i] += 5\n",
" \n",
"values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That makes sense, but it's a lot of code to write and it runs slowly because \n",
"it's pure Python.\n",
"\n",
"__Note:__ Just in case you aren't used to using operators like `+=`, that just \n",
"means _add these two items and then store the result in the left item._ It is a more \n",
"succinct way of writing `values[i] = values[i] + 5`. The code you see in these examples \n",
"makes use of such operators whenever possible."
]
},
{
"cell_type": "markdown",