Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Notebook 3 - NumPy\n",
"[NumPy](http://numpy.org) short for Numerical Python, has long been a cornerstone of numerical computing on Python. It provides the data structures, algorithms and the glue needed for most scientific applications involving numerical data in Python. All computation is done in vectorised form - using vectors of several values at once instead of singular values at a time. NumPy contains, among other thigs:\n",
"* A fast and efficient multidimensional array object `ndarray`.\n",
"* Mathematical functions for performing element-wise computations with arrays or mathematical operations between arrays.\n",
"* Tools for reading and manipulating large array data to disk and working with memory-mapped files.\n",
"* Linear algebra, random number generation and Fourier transform capabilities.\n",
"\n",
"For the rest of the course, whenever array is mentioned it refers to the NumPy ndarray.\n",
"<br>\n",
"\n",
"## Table of contents\n",
"- [The ndarray](#ndarray)\n",
" - [Creating arrays](#creating)\n",
" - [Data Types](#data)\n",
" - [Arithmetic Operations](#arithmetic)\n",
" - [Indexing and Slicing](#indexing)\n",
" - [Transposing and Swapping Axis](#transposing)\n",
"- [Universal Functinos](#universal)\n",
"- [Other useful operations](#other)\n",
"- [File IO](#file)\n",
"- [Liear algebra](#linear)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Why NumPy?\n",
"Is the first question that anybody asks when they find out about it. \n",
"\n",
"Some people might say: *I don't care about speed, I want to spend my time researching how to cure cancer, not optimise coputer code!*\n",
"\n",
"That's perfectly reasonable, but are you willing to wait a lot longer for your experiment to finish? I definitely don't want to do that. Let's see how much faster NumPy really is!\n",
"\n",
"to show that we'll be using the magic command `%timeit` which you can read more about [here](https://ipython.readthedocs.io/en/stable/interactive/magics.html) and don't worry about the details now, they will clear up later.\n",
"\n",
"Let's have a look at generating a vector of 10M random values and then summing them all up using the Python way and using the NumPy way!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"x = np.random.randn(10000000) # generate random numbers\n",
"\n",
"print(\"Running normal python sum()\")\n",
"%timeit sum(x)\n",
"\n",
"print(\"Running numpy sum()\")\n",
"%timeit np.sum(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**WOW** that was a difference of more than a **100 times** and that was just for a single summing operation. Imagine if you had several of those running all the time!\n",
"\n",
"Are you onboard with Numpy then? Let's proceed..."
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The ndarray <a name=\"ndarray\"></a>\n",
"The ndarray is a backbone on Numpy. It's a fast and flexible container for N-dimensional array objects, usually used for large datasets in Python. Arrays enable you to perform mathematical operations on whole blocks of data using similar syntax to the equivalent operations between scalar elements.\n",
"\n",
"Here is a quick example of its capabilities:"
]
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"\n",
"# create a 2x3 array of random values\n",
"data = np.random.randn(2,3)\n",
"data"
]
},
{
"cell_type": "code",
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every array has a shape, a tuple indicating the size of each dimnesion and a dtype. You can obtain these via the respective methods:"
"source": [
"# number of dimensions of the array\n",
"data.ndim"
]
},
{
"cell_type": "code",
"source": [
"# the size of the array\n",
"data.shape"
]
},
{
"cell_type": "code",
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
"source": [
"# the type of values store in the array\n",
"data.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating arrays <a name=\"creating\"></a>\n",
"The easiest and quickest way to create an array is from a normal Python list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = [1.2, 5.2, 5, 7.8, 0.3]\n",
"arr = np.array(data)\n",
"\n",
"arr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is also possible to create multidimensional arrays in a similar fashion. An example would be:\n",
"```python\n",
"data = [[1.2, 5.2, 5, 7.8, 0.3],\n",
" [4.1, 7.2, 4.8, 0.1, 7.7]]\n",
"```\n",
"Try creating a multidimensional array below and verify its number of dimensions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
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
"source": [
"np.zeros(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Again, it is also possible to create a multidimensional array by passing a tuple as an argument"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.zeros((4,6))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another option is to use the `empty()` function which creates an array filled with garbage values. It is used in a similar way to `zeros()`.\n",
"\n",
"Try using it below and see what it creates!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.empty((10, 10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"NumPy also has an equivalent to the built-in Python function `range()`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here are most of the possible ways of creating an array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| Function | Description |\n",
"|----|:--|\n",
"| array | Convert input data to an ndarray either by inferring a dtype<br>or explicitly specifying a dtype; copies the input data by default. |\n",
"| asarray | Convert input to ndarray, but do not copy if the input is already an ndarray. |\n",
"| arange | Similar to the built-in `range` function but returns an ndarray. |\n",
"| linspace | Return evenly spaced numbers over a specified interval. |\n",
"| ones | Produces an array of all 1s with the given shape and dtype. |\n",
"| ones_like | Similar to `ones` but takes another array and produces a ones array<br>of the same shape and dtype |\n",
"| zeros, zeros_like | Similar to `ones` but produces an array of 0s. |\n",
"| empty, emtpy_like | Create new array by allocating memory but without populating any values. |\n",
"| full, full_like | Produce an array of a given shape and dtype with all values set to the indicated \"fill value\". |\n",
"| eye | Create a square NxN identity matrix (1s on the diagonal and 0s elsewhere). |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Data Types <a name=\"data\"></a>\n",
"The data type or `dtype` is a special object containing the information the array needs to interpret a chunk of memory. We can specify it during the creation of an array "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 2, 3], dtype=np.float64)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"arr.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An ndarray can only hold data in **one** dtype. This makes it a little less flexible than a regular Python list, but is part of what allows NumPy to run so fast. Here is a table of all the dtypes.\n",
"There is no need to remember all of dtypes.\n",
"\n",
"| Type | Type code | Description |\n",
"|----|---|---|\n",
"| int8, uint8 | i1, u1 | Signed and unsigned 8bit integer |\n",
"| int16, uint16 | i2, u2 | Signed and unsigned 16bit integer |\n",
"| int32, uint32 | i4, u4 | Signed and unsigned 32bit integer |\n",
"| int64, uint64 | i8, u8 | Signed and unsigned 64bit integer |\n",
"| float16 | f2 | Half-precision floating point |\n",
"| float32 | f4 or f | Standard single-precision floating point |\n",
"| float64 | f8 or d | Standard double-precision floating point |\n",
"| float128 | f16 or g | Extended-precision floating point |\n",
"| complex64<br>complex128<br>complex256 | c8, c16, c32 | Complex numbers represented by two 32, 64 or 128 floats, respectively |\n",
"| bool | ? | Boolean type storing True or False |\n",
"| object | O | Python object type, a value can be any Python object |\n",
"| string_ | S | Fixed-length ASCII string type.<br>For example use `S10` to create a string dtype with length 10 |\n",
"| unicode_ | U | Fixed-length Unicode type |\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similar to normal Python, you can cast (convert) an array from one dtype to another using the `astype` method:"
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
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 2, 3])\n",
"arr.dtype"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"float_arr = arr.astype(np.float64)\n",
"float_arr.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The normal limitations to casting apply here as well. You can try creating a `float64` array and then converting it to an `int64` array below:"
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 1\n",
"Create a 5x5 [identity matrix](https://en.wikipedia.org/wiki/Identity_matrix). Then convert it to float64 dtype. At the end confirm its properties using the appropriate attributes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Arithmetic operations <a name=\"arithmetic\"></a>\n",
"You have already gotten a taste of this in the examples above but let's try to extend that.\n",
"\n",
"Arrays are important because they enable you to express batch operations on data without having to write for loops - this is called **vectorisation**.\n",
"\n",
"Any arithmetic operation between equal-size arrays applies the operation element-wise:"
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
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 2, 3], [4, 5, 6]])\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A * A"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A - A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Arithmetic operations with scalars propogate the scalar argument to each element in the array:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A * 5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A ** 0.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Comparisons between arrays of the same size yield boolean arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"B = np.array([[1, 7, 4],[4, 12, 2]])\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A > B"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Applying arithmetic operations to differently sized arrays is called **broadcasting** but will not be covered in this course due to the limited time."
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
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 2\n",
"Generate a vector of size 10 with values ranging from 0 to 1, both included."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Indexing and slicing <a name=\"indexing\"></a>\n",
"NumPy offers many options for indexing and slicing. Coincidentally, they are very similar to Python.\n",
"\n",
"Let's see how this is done in 1D:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = np.arange(10)\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A[5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A[5:8]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A[5:8] = 0\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Important:** Unlike regular Python, NumPy array slices are _views_ on the original array. This means that the data is not copied, and any modifications to the source array will be reflected in the view. Similarly, changing the slice will update the original array."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A_slice"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A[5:8] = [12, 17, 24] #Update source array\n",
"A_slice #Slice is changed"
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A_slice[:] = 0 #Edit the slice\n",
"A #The array is changed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's now have a look at higher dimensional arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"C"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have 2 dimensions, we need to input 2 indices to get a specific element of the array. Alternatively, if we input only one index, then we obtain the whole row of the array:"
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
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C[2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C[2][1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C[2, 1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a picture to better explain indexing in 2D:\n",
"<img src=\"img/ndarray.png\" alt=\"drawing\" width=\"300\"/>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The same concepts and techniques are extended into multidimensional arrays:\n",
"if you omit later indices, the returned object will be a lower dimensional ndarray consisting of all data along the higher dimensions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's look into **slicing**. You already saw above that slicing in 1D is done the same way as in standard Python data structures. So how do we do that in 2D? Well, it is fairly intuitive:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"C"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C[:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This can be read as *select the first 2 rows of C*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C[1, :2] # Select row 1, the first 2 columns."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C[:, :1] # Select all rows, first 1 column (i.e. select column 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is some visual aid for what happened above:\n",
"<img src=\"img/indexing.png\" alt=\"drawing\" width=\"400\"/>"
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is also possible to index arrays via booleans.\n",
"\n",
"Say we have an 1D array of 0s and 1s and then a 2D array of randomly generated data:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fruits = np.array([\"banana\", \"orange\", \"mango\", \"banana\", \"tomato\", \"passionfruit\", \"cherry\"])\n",
"fruits"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = np.random.randn(7,4)\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fruits == \"banana\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data[fruits == \"banana\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Powerful right? The only caveat is that the boolean array must be of the same length as the array axis it's indexing.\n",
"You can also mix and match boolean arrays but there is one small difference compared to Python - the typical boolean operators (`and` and `or`) do not work, and instead you must use `&`(and) and `|`(or)."
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
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mask = (fruits == \"banana\") | (fruits == \"cherry\")\n",
"data[mask]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 3\n",
"Create a 5x5 matrix of random values. Square all positive values of the matrix and set all else to 0. Attempt to do this in place - ie. without copying the matrix"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 4\n",
"Create a 4 by 4 2D array with 1s on the border and 0s inside"
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
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transposing Arrays and Swapping Axes <a name=\"transposing\"></a>\n",
"We can use the method `reshape()` to convert the data from one shape into another. Later we can use the `T` attribute to obtain the transpose of the array."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = np.arange(15)\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"B = A.reshape((3,5))\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"B.T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also reshape 3D arrays but how would `T` work then? Luckily, we can use the `tranpose()` method which allows us to chose the axes we want to swap:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = np.arange(16)\n",
"C = A.reshape((2, 2, 4))\n",
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C.transpose((1, 0, 2))\n",
"C.shape"
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
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotting\n",
"You can easily plot and show images in Python via the package `matplotlib` which can be used to plot an array. \n",
"\n",
"First we have to set up our environment. Read and run the code below to do just that:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"# Import NumPy and matplotlib\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Set a greyscale colourmap (we want white for 0 and black for 1)\n",
"plt.set_cmap('Greys')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can create an array of values and plot in a canvas. The easiest way to do this is to pick values between 0 and 1 and plot grayscale images where 1 corresponds to black and 0 corresponds to white. Let's see how we can do this by creating an array of 0s:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"canvas = np.zeros((100,50))\n",
"plt.imshow(canvas, interpolation=\"none\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All is white right? let's add some black to it!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"canvas[50, 25] = 1\n",
"plt.imshow(canvas, interpolation=\"none\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 5\n",
"Use the canvas template above and create an image where the top right and bottom left pixels are set to black.\n",
"\n",
"*Note: Remember to first reset your canvas to only 0s*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 6\n",
"Draw a horizontal and vertical line across the canvas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 7\n",
"Make the top left corner of the image black.\n",
"\n",
"*Extra challenge: do this wihtout using numbers for indexing*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Universal Functions <a name=\"universal\"></a>\n",
"or *ufunc* are functions that perform element-wise operations on data in ndarrays. You can think of them as fast vectorised wrappers for simple functions. Here is an example of `sqrt` and `exp`:"
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = np.arange(10)\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.sqrt(A)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.exp(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other universal functions take 2 arrays as input. These are called *binary* functions.\n",
"\n",
"For example `maximum()` selects the biggest values from two input arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.random.randn(10)\n",
"y = np.random.randn(10)\n",
"np.maximum(x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other functions can return multiple arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = np.random.randn(10)\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"remainder, whole = np.modf(A)\n",
"print(remainder)\n",
"print(whole)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a list of most *ufuncs* in NumyPy:\n",
"*again, you don't need to memorise them. This is just a reference*\n",
"### Unary functions (accept one argument)\n",
"\n",
"| Function | Description |\n",
"|----|----|\n",
"| abs, fabs | Compute the absolute value element-wise for integer, floating point, or complex values.<br>Use fabs as a faster alternative for non-complex-valued data |\n",
"| sqrt | Compute the square root of each element. Equivalent to arr ** 0.5 |\n",
"| square | Compute the square of each element. Equivalent to arr ** 2 |\n",
"| exp | Compute the exponent ex of each element |\n",
"| log, log10, log2, log1p | Natural logarithm (base e), log base 10, log base 2, and log(1 + x), respectively |\n",
"| sign | Compute the sign of each element: 1 (positive), 0 (zero), or -1 (negative) |\n",
"| ceil | Compute the ceiling of each element, i.e. the smallest integer greater than or equal to each element |\n",
"| floor | Compute the floor of each element, i.e. the largest integer less than or equal to each element |\n",
"| rint | Round elements to the nearest integer, preserving the dtype |\n",
"| modf | Return fractional and integral parts of array as separate array |\n",
"| isnan | Return boolean array indicating whether each value is NaN (Not a Number) |\n",
"| isfinite, isinf | Return boolean array indicating whether each element is finite (non-inf, non-NaN) or infinite, respectively |\n",
"| cos, cosh, sin, sinh, tan, tanh | Regular and hyperbolic trigonometric functions |\n",
"| arccos, arccosh, arcsin,<br>arcsinh, arctan, arctanh | Inverse trigonometric functions |\n",
"| logical_not | Compute truth value of not x element-wise. Equivalent to -arr. |\n",
"\n",
"### Binary functions (accept 2 arguments)\n",
"| Functions | Description |\n",
"| ---- | ---- |\n",
"| add | Add corresponding elements in arrays |\n",
"| subtract | Subtract elements in second array from first array |\n",
"| multiply | Multiply array elements |\n",
"| divide, floor_divide | Divide or floor divide (truncating the remainder) |\n",
"| power | Raise elements in first array to powers indicated in second array |\n",
"| maximum, fmax | Element-wise maximum. fmax ignores NaN |\n",
"| minimum, fmin | Element-wise minimum. fmin ignores NaN |\n",
"| mod | Element-wise modulus (remainder of division) |\n",
"| copysign | Copy sign of values in second argument to values in first argument |\n",
"| greater, greater_equal, less,<br>less_equal, equal, not_equal |\tPerform element-wise comparison, yielding boolean array. <br>Equivalent to infix operators >, >=, <, <=, ==, != |\n",
"| logical_and, logical_or, logical_xor | Compute element-wise truth value of logical operation. Equivalent to infix operators & |, ^ |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Other useful operations <a name=\"other\"></a>\n",
"NumPy offers a set of mathematical functions that compute statistics about an entire array:"
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"B = np.random.randn(5, 4)\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"B.mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.mean(B)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"B.sum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"B.mean(axis=1) # Compute mean in column (axis 1) direction (i.e. the mean of each row)"
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here `mean(axis=1)` means compute the mean across the columns (axis 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a set of other similar functions:\n",
"\n",
"| Function | Description|\n",
"| --- | --- |\n",
"|sum | Sum of all the elements in the array or along an axis. Zero-length arrays have sum 0. |\n",
"| mean | Arithmetic mean. Zero-length arrays have NaN mean. |\n",
"| std, var | Standard deviation and variance, respectively, with optional<br>degrees of freedom adjustment (default denominator n). |\n",
"|min, max | Minimum and maximum. |\n",
"| argmin, argmax | Indices of minimum and maximum elements, respectively. |\n",
"| cumsum | Cumulative sum of elements starting from 0 |\n",
"| cumprod | Cumulative product of elements starting from 1 |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There also are some boolean operations. `any` tests whether one or more values in an array is `True`, and `all` tests whether all values are `True`:"
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = np.random.randn(100)\n",
"A_bool = A > 0\n",
"A_bool"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A_bool.any()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A_bool.all()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 8\n",
"Generate and normalise a random 5x5x5 matrix"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 9\n",
"Create a random vector of size 30 and find its mean value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 10\n",
"Subtract the mean of each row of a randomly generated matrix:"
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sorting <a name=\"sorting\"></a>\n",
"Similar to Python's built-in list type, NumyPy arrays can be sorted in place:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = np.random.randn(10)\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A.sort()\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another option is `unique()` which returns the sorted unique values in an array."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Linear Algebra <a name=\"linear\"></a>\n",
"Similar to other languages like MATLAB, NumyPy offers a set of standard linear algebra operations, like matrix multiplication, decompositions, determinants and etc.. Unlike some other languages though, the default operations like `*` peform element-wise operations. To perform matrix-wise operartions we need to use special functions:"
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temp = np.arange(16)\n",
"A = temp[:8]\n",
"B = temp[8:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A.dot(B)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also extend this with the `numpy.linalg` package:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from numpy.linalg import inv, qr\n",
"A = np.random.randn(5, 5)\n",
"mat = A.T.dot(A)\n",
"mat"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"inv(mat)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mat.dot(inv(mat))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a set of commonly used numpy.linalg functions\n",
"\n",
"| Function | Description |\n",
"| --- | --- |\n",
"| diag | Return the diagonal (or off-diagonal) elements of a square matrix as a 1D array,<br>or convert a 1D array into a square matrix with zeros on the off-diagonal |\n",
"| dot | Matrix multiplication |\n",
"| trace | Compute the sum of the diagonal elements |\n",
"| det | Compute the matrix determinant |\n",
"| eig | Compute the eigenvalues and eigenvectors of a square matrix |\n",
"| inv | Compute the inverse of a square matrix |\n",
"| pinv | Compute the Moore-Penrose pseudo-inverse inverse of a square matrix |\n",
"| qr | Compute the QR decomposition |\n",
"| svd | Compute the singular value decomposition (SVD) |\n",
"| solve | Solve the linear system Ax = b for x, where A is a square matrix |\n",
"| lstsq | Compute the least-squares solution to y = Xb |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 11\n",
"Obtain the diagonal of a dot product of 2 random matrices"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## File IO <a name=\"file\"></a>\n",
"NumPy offers its own set of File IO functions.\n",
"\n",
"The most common one is `genfromtxt()` which can load the common `.csv` and `.tsv` files.\n",
"\n",
"Now let us analyse temperature data from Stockholm over the years.\n",
"\n",
"First we have to load the file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = np.genfromtxt(\"./data/stockholm_td_adj.dat\")\n",
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first column of this array gives years, and the 6th gives temperature readings. We can extract these."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"yrs = data[:, 0]\n",
"temps = data[:, 5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Having read in our data, we can now work with it - for example, we could produce a plot.\n",
"We will cover plotting in more depth in notebook 4, so there's no need to get too caught up in the details right now - this is just an examle of something we might do having read in some data. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(16, 6)) # Create a 16x6 figure\n",
"plt.plot(yrs, temps) # Plot temps vs yrs\n",
"\n",
"#Set some labels\n",
"plt.title(\"Temperatures in Stockholm\")\n",
"plt.xlabel(\"year\")\n",
"plt.ylabel(\"Temperature (C)\")\n",
"\n",
"plt.show() # Show the plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 12\n",
"Read in the file `daily_gas_price.csv`, which lists the daily price of natural gas since 1997. Each row contains a date and a price, separated by a comma. Find the minimum, maximum, and mean gas price over the dataset.\n",
"\n",
"(Hint: you will need to use the delimiter option in `np.genfromtxt` to specify that data is separated by commas. Also, NumPy will interpret the data in float format by default - we may need to set the dtype to a string format at first, then discard the dates, before turning the gas prices back into floats to process them! Otherwise, NumPy may find it confusing to try and interpret dates formatted as YYYY-MM-DD as floats and will probably complain.)"
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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",