Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numpy\n",
"\n",
"\n",
"This section introduces you to [`numpy`](http://www.numpy.org/), Python's\n",
"numerical computing library.\n",
"\n",
"\n",
"Numpy is not actually part of the standard Python library. But it is a\n",
"fundamental part of the Python ecosystem - it forms the basis for many\n",
"important Python libraries, and it (along with its partners\n",
"[`scipy`](https://www.scipy.org/), [`matplotlib`](https://matplotlib.org/) and\n",
"[`pandas`](https://pandas.pydata.org/)) is what makes Python an attractive\n",
"alternative to Matlab as a scientific computing platform.\n",
"\n",
"\n",
"## Contents\n",
"\n",
"\n",
"* [The Python list versus the Numpy array](#the-python-list-versus-the-numpy-array)\n",
"* [Numpy basics](#numpy-basics)\n",
" * [Creating arrays](#creating-arrays)\n",
" * [Operating on arrays](#operating-on-arrays)\n",
" * [Array properties](#array-properties)\n",
" * [Descriptive statistics](#descriptive-statistics)\n",
" * [Reshaping and rearranging arrays](#reshaping-and-rearranging-arrays)\n",
"* [Multi-variate operations](#multi-variate-operations)\n",
" * [Matrix multplication](#matrix-multiplication)\n",
" * [Broadcasting](#broadcasting)\n",
"* [Array indexing](#array-indexing)\n",
" * [Indexing multi-dimensional arrays](#indexing-multi-dimensional-arrays)\n",
" * [Boolean indexing](#boolean-indexing)\n",
" * [Coordinate array indexing](#coordinate-array-indexing)\n",
"* [Generating random numbers](#generating-random-numbers)\n",
"\n",
"* [Appendix: Importing Numpy](#appendix-importing-numpy)\n",
"* [Appendix: Vectors in Numpy](#appendix-vectors-in-numpy)\n",
"\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"the-python-list-versus-the-numpy-array\"></a>\n",
"## The Python list versus the Numpy array\n",
"\n",
"\n",
"Numpy adds a new data type to the Python language - the `array` (more\n",
"specifically, the `ndarray`). A Numpy `array` is a N-dimensional array of\n",
"homogeneously-typed numerical data.\n",
"\n",
"\n",
"You have already been introduced to the Python `list`, which you can easily\n",
"use to store a handful of numbers (or anything else):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data = [10, 8, 12, 14, 7, 6, 11]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You could also emulate a 2D or ND matrix by using lists of lists, for example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xyz_coords = [[-11.4, 1.0, 22.6],\n",
" [ 22.7, -32.8, 19.1],\n",
" [ 62.8, -18.2, -34.5]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For simple tasks, you could stick with processing your data using python\n",
"lists, and the built-in\n",
"[`math`](https://docs.python.org/3.5/library/math.html) library. And this\n",
"might be tempting, because it does look quite a lot like what you might type\n",
"into Matlab.\n",
"\n",
"\n",
"But __BEWARE!__ A Python list is a terrible data structure for scientific\n",
"computing!\n",
"\n",
"\n",
"This is a major source of confusion for those poor souls who have spent their\n",
"lives working in Matlab, but have finally seen the light and switched to\n",
"Python. It is _crucial_ to be able to distinguish between a Python list and a\n",
"Numpy array.\n",
"___Python list == Matlab cell array:___ A list in python is akin to a cell\n",
"array in Matlab - they can store anything, but are extremely inefficient, and\n",
"unwieldy when you have more than a couple of dimensions.\n",
"___Numy array == Matlab matrix:___ These are in contrast to the Numpy array\n",
"and Matlab matrix, which are both thin wrappers around a contiguous chunk of\n",
"memory, and which provide blazing-fast performance (because behind the scenes\n",
"in both Numpy and Matlab, it's C, C++ and FORTRAN all the way down).\n",
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
"\n",
"\n",
"So you should strongly consider turning those lists into Numpy arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"data = np.array([10, 8, 12, 14, 7, 6, 11])\n",
"\n",
"xyz_coords = np.array([[-11.4, 1.0, 22.6],\n",
" [ 22.7, -32.8, 19.1],\n",
" [ 62.8, -18.2, -34.5]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you look carefully at the code above, you will notice that we are still\n",
"actually using Python lists. We have declared our data sets in exactly the\n",
"same way as we did earlier, by denoting them with square brackets `[` and `]`.\n",
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
"\n",
"\n",
"The key difference here is that these lists immediately get converted into\n",
"Numpy arrays, by passing them to the `np.array` function. To clarify this\n",
"point, we could rewrite this code in the following equivalent manner:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"# Define our data sets as python lists\n",
"data = [10, 8, 12, 14, 7, 6, 11]\n",
"xyz_coords = [[-11.4, 1.0, 22.6],\n",
" [ 22.7, -32.8, 19.1],\n",
" [ 62.8, -18.2, -34.5]]\n",
"\n",
"# Convert them to numpy arrays\n",
"data = np.array(data)\n",
"xyz_coords = np.array(xyz_coords)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
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
"Of course, in practice, we would never create a Numpy array in this way - we\n",
"will be loading our data from text or binary files directly into a Numpy\n",
"array, thus completely bypassing the use of Python lists and the costly\n",
"list-to-array conversion. I'm emphasising this to help you understand the\n",
"difference between Python lists and Numpy arrays. Apologies if you've already\n",
"got it, forgiveness please.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"numpy-basics\"></a>\n",
"## Numpy basics\n",
"\n",
"\n",
"Let's get started."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"creating-arrays\"></a>\n",
"### Creating arrays\n",
"\n",
"\n",
"Numpy has quite a few functions which behave similarly to their equivalents in\n",
"Matlab:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('np.zeros gives us zeros: ', np.zeros(5))\n",
"print('np.ones gives us ones: ', np.ones(5))\n",
"print('np.arange gives us a range: ', np.arange(5))\n",
"print('np.linspace gives us N linearly spaced numbers:', np.linspace(0, 1, 5))\n",
"print('np.random.random gives us random numbers: ', np.random.random(5))\n",
"print('np.random.randint gives us random integers: ', np.random.randint(1, 10, 5))\n",
"print('np.eye gives us an identity matrix:')\n",
"print(np.eye(4))\n",
"print('np.diag gives us a diagonal matrix:')\n",
"print(np.diag([1, 2, 3, 4]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> There will be more on random numbers [below](#generating-random-numbers).\n",
"\n",
"\n",
"The `zeros` and `ones` functions can also be used to generate N-dimensional\n",
"arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z = np.zeros((3, 4))\n",
"o = np.ones((2, 10))\n",
"print(z)\n",
"print(o)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> Note that, in a 2D Numpy array, the first axis corresponds to rows, and the\n",
"> second to columns - just like in Matlab.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"operating-on-arrays\"></a>\n",
"### Operating on arrays\n",
"\n",
"\n",
"All of the mathematical operators you know and love can be applied to Numpy\n",
"arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (3, 3))\n",
"print('a:')\n",
"print(a)\n",
"print('a + 2:')\n",
"print( a + 2)\n",
"print('a * 3:')\n",
"print( a * 3)\n",
"print('a % 2:')\n",
"print( a % 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"array-properties\"></a>\n",
"### Array properties\n",
"\n",
"\n",
"Numpy is a bit different than Matlab in the way that you interact with\n",
"arrays. In Matlab, you would typically pass an array to a built-in function,\n",
"e.g. `size(M)`, `ndims(M)`, etc. In contrast, a Numpy array is a Python\n",
"object which has _attributes_ that contain basic information about the array:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z = np.zeros((2, 3, 4))\n",
"print(z)\n",
"print('Shape: ', z.shape)\n",
"print('Number of dimensions: ', z.ndim)\n",
"print('Number of elements: ', z.size)\n",
"print('Data type: ', z.dtype)\n",
"print('Number of bytes: ', z.nbytes)\n",
"print('Length of first dimension: ', len(z))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> As depicted above, passing a Numpy array to the built-in `len` function will\n",
"> only give you the length of the first dimension, so you will typically want\n",
"> to avoid using it - use the `size` attribute instead.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"descriptive-statistics\"></a>\n",
"### Descriptive statistics\n",
"\n",
"\n",
"Similarly, a Numpy array has a set of methods<sup>1</sup> which allow you to\n",
"calculate basic descriptive statisics on an array:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.random(10)\n",
"print('a: ', a)\n",
"print('min: ', a.min())\n",
"print('max: ', a.max())\n",
"print('index of min: ', a.argmin()) # remember that in Python, list indices\n",
"print('index of max: ', a.argmax()) # start from zero - Numpy is the same!\n",
"print('mean: ', a.mean())\n",
"print('variance: ', a.var())\n",
"print('stddev: ', a.std())\n",
"print('sum: ', a.sum())\n",
"print('prod: ', a.prod())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> <sup>1</sup> Python, being an object-oriented language, distinguishes\n",
"> between _functions_ and _methods_. _Method_ is simply the term used to refer\n",
"> to a function that is associated with a specific object. Similarly, the term\n",
"> _attribute_ is used to refer to some piece of information that is attached\n",
"> to an object, such as `z.shape`, or `z.dtype`.\n",
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
"<a class=\"anchor\" id=\"reshaping-and-rearranging-arrays\"></a>\n",
"### Reshaping and rearranging arrays\n",
"\n",
"\n",
"A numpy array can be reshaped very easily, using the `reshape` method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (4, 4))\n",
"b = a.reshape((2, 8))\n",
"print('a:')\n",
"print(a)\n",
"print('b:')\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that this does not modify the underlying data in any way - the `reshape`\n",
"method returns a _view_ of the same array, just indexed differently:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a[3, 3] = 12345\n",
"b[0, 7] = 54321\n",
"print('a:')\n",
"print(a)\n",
"print('b:')\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you need to create a reshaped copy of an array, use the `np.array`\n",
"function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (4, 4))\n",
"b = np.array(a.reshape(2, 8))\n",
"a[3, 3] = 12345\n",
"b[0, 7] = 54321\n",
"print('a:')\n",
"print(a)\n",
"print('b:')\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `T` attribute is a shortcut to obtain the transpose of an array."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (4, 4))\n",
"print(a)\n",
"print(a.T)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `transpose` method allows you to obtain more complicated rearrangements\n",
"of an array's axes:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (2, 3, 4))\n",
"b = a.transpose((2, 0, 1))\n",
"print('a: ', a.shape)\n",
"print(a)\n",
"print('b:', b.shape)\n",
"print(b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> Note again that the `T` attribute and `transpose` method return _views_ of\n",
"> your array.\n",
"\n",
"\n",
"Numpy has some useful functions which allow you to concatenate or stack\n",
"multiple arrays into one. The `concatenate` function does what it says on the\n",
"tin:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.zeros(3)\n",
"b = np.ones(3)\n",
"\n",
"print('1D concatenation:', np.concatenate((a, b)))\n",
"\n",
"a = np.zeros((3, 3))\n",
"b = np.ones((3, 3))\n",
"\n",
"print('2D column-wise concatenation:')\n",
"print(np.concatenate((a, b), axis=1))\n",
"\n",
"print('2D row-wise concatenation:')\n",
"\n",
"# The axis parameter defaults to 0,\n",
"# so it is not strictly necessary here.\n",
"print(np.concatenate((a, b), axis=0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `hstack`, `vstack` and `dstack` functions allow you to concatenate vectors\n",
"or arrays along the first, second, or third dimension respectively:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.zeros(3)\n",
"b = np.ones(3)\n",
"\n",
"print('a: ', a)\n",
"print('b: ', b)\n",
"\n",
"hstacked = np.hstack((a, b))\n",
"vstacked = np.vstack((a, b))\n",
"dstacked = np.dstack((a, b))\n",
"\n",
"print('hstacked: (shape {}):'.format(hstacked.shape))\n",
"print( hstacked)\n",
"print('vstacked: (shape {}):'.format(vstacked.shape))\n",
"print( vstacked)\n",
"print('dstacked: (shape {}):'.format(dstacked.shape))\n",
"print( dstacked)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
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
"<a class=\"anchor\" id=\"multi-variate-operations\"></a>\n",
"## Multi-variate operations\n",
"\n",
"\n",
"Many operations in Numpy operate on an elementwise basis. For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (5))\n",
"b = np.random.randint(1, 10, (5))\n",
"\n",
"print('a: ', a)\n",
"print('b: ', b)\n",
"print('a + b: ', a + b)\n",
"print('a * b: ', a * b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This also extends to higher dimensional arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (4, 4))\n",
"b = np.random.randint(1, 10, (4, 4))\n",
"\n",
"print('a:')\n",
"print(a)\n",
"print('b:')\n",
"print(b)\n",
"\n",
"print('a + b')\n",
"print(a + b)\n",
"print('a * b')\n",
"print(a * b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wait ... what's that you say? Oh, I couldn't understand because of all the\n",
"froth coming out of your mouth. I guess you're angry that `a * b` didn't give\n",
"you the matrix product, like it would have in Matlab. Well all I can say is\n",
"that Python is not Matlab. Get over it. Take a calmative.\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"matrix-multiplication\"></a>\n",
"*## Matrix multiplication\n",
"\n",
"\n",
"When your heart rate has returned to its normal caffeine-induced state, you\n",
"can use the `dot` method, or the `@` operator, to perform matrix\n",
"multiplication:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (4, 4))\n",
"b = np.random.randint(1, 10, (4, 4))\n",
"\n",
"print('a:')\n",
"print(a)\n",
"print('b:')\n",
"print(b)\n",
"\n",
"print('a @ b')\n",
"print(a @ b)\n",
"\n",
"print('a.dot(b)')\n",
"print(a.dot(b))\n",
"\n",
"print('b.dot(a)')\n",
"print(b.dot(a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> The `@` matrix multiplication operator is a relatively recent addition\n",
"> to Python and Numpy, so you might not see it all that often in existing\n",
"> code. But it's here to stay, so go ahead and use it!\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"broadcasting\"></a>\n",
"### Broadcasting\n",
"\n",
"\n",
"One of the coolest (and possibly confusing) features of Numpy is its\n",
"[_broadcasting_\n",
"rules](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html).\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"array-indexing\"></a>\n",
"## Array indexing\n",
"\n",
"\n",
"Just like in Matlab, slicing up your arrays is a breeze in Numpy. If you are\n",
"after some light reading, you might want to check out the [comprehensive Numpy\n",
"Indexing\n",
"reference](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html).\n",
"\n",
"\n",
"> As with indexing regular Python lists, array indices start from 0, and end\n",
"> indices (if specified) are exclusive.\n",
"\n",
"\n",
"Let's whet our appetites with some basic 1D array slicing. Numpy supports the\n",
"standard Python __slice__ notation for indexing, where you can specify the\n",
"start and end indices, and the step size, via the `start:stop:step` syntax:"
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
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, 10)\n",
"\n",
"print('a: ', a)\n",
"print('first element: ', a[0])\n",
"print('first two elements: ', a[:2])\n",
"print('last element: ', a[a.shape[0] - 1])\n",
"print('last element again: ', a[-1])\n",
"print('last two elements: ', a[-2:])\n",
"print('middle four elements: ', a[3:7])\n",
"print('Every second element: ', a[::2])\n",
"print('Every second element, reversed: ', a[1::-2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that slicing an array in this way returns a _view_, not a copy, into that\n",
"array:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, 10)\n",
"print('a:', a)\n",
"every2nd = a[::2]\n",
"print('every 2nd:', every2nd)\n",
"every2nd += 10\n",
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
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
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"indexing-multi-dimensional-arrays\"></a>\n",
"### Indexing multi-dimensional arrays\n",
"\n",
"\n",
"Multi-dimensional array indexing works in much the same way as one-dimensional\n",
"indexing but with, well, more dimensions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (5, 5))\n",
"print('a:')\n",
"print(a)\n",
"print(' First row: ', a[ 0, :])\n",
"print(' Last row: ', a[ -1, :])\n",
"print(' second column: ', a[ :, 1])\n",
"print(' Centre:')\n",
"print(a[1:4, 1:4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For arrays with more than two dimensions, the ellipsis (`...`) is a handy\n",
"feature - it allows you to specify a slice comprising all elements along\n",
"more than one dimension:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, (3, 3, 3))\n",
"print('a:')\n",
"print(a)\n",
"print('All elements at x=0:')\n",
"print(a[0, ...])\n",
"print('All elements at z=2:')\n",
"print(a[..., 2])\n",
"print('All elements at x=0, z=2:')\n",
"print(a[0, ..., 2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"boolean-indexing\"></a>\n",
"### Boolean indexing\n",
"\n",
"\n",
"A numpy array can be indexed with a boolean array of the same shape. For\n",
"example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, 10)\n",
"\n",
"print('a: ', a)\n",
"print('a > 5: ', a > 4)\n",
"print('elements in a that are > 5: ', a[a > 5])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
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
"In contrast to the simple indexing we have already seen, boolean indexing will\n",
"return a _copy_ of the indexed data, __not__ a view. For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, 10)\n",
"b = a[a > 5]\n",
"print('a: ', a)\n",
"print('b: ', b)\n",
"print('Setting b[0] to 999')\n",
"b[0] = 999\n",
"print('a: ', a)\n",
"print('b: ', b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> In general, any 'simple' indexing operation on a Numpy array, where the\n",
"> indexing object comprises integers, slices (using the standard Python\n",
"> `start:stop:step` notation), colons (`:`) and/or ellipses (`...`), will\n",
"> result in a __view__ into the indexed array. Any 'advanced' indexing\n",
"> operation, where the indexing object contains anything else (e.g. boolean or\n",
"> integer arrays, or even python lists), will result in a __copy__ of the\n",
"> data.\n",
"\n",
"\n",
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
"Logical operators `~` (not), `&` (and) and `|` (or) can be used to manipulate\n",
"and combine boolean Numpy arrays:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randint(1, 10, 10)\n",
"gt5 = a > 5\n",
"even = a % 2 == 0\n",
"\n",
"print('a: ', a)\n",
"print('elements in a which are > 5: ', a[gt5])\n",
"print('elements in a which are <= 5: ', a[~gt5])\n",
"print('elements in a which are even: ', a[even])\n",
"print('elements in a which are odd: ', a[~even])\n",
"print('elements in a which are > 5 and even: ', a[gt5 & even])\n",
"print('elements in a which are > 5 or odd: ', a[gt5 | ~even])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"coordinate-array-indexing\"></a>\n",
"### Coordinate array indexing\n",
"\n",
"\n",
"You can index a numpy array using another array containing coordinates into\n",
"the first array. As with boolean indexing, this will result in a copy of the\n",
"data. Generally, you will need to have a separate array, or list, of\n",
"coordinates into each data axis:"
"cell_type": "code",
"execution_count": null,
"a = np.random.randint(1, 10, (4, 4))\n",
"print(a)\n",
"rows = [0, 2, 3]\n",
"cols = [1, 0, 2]\n",
"indexed = a[rows, cols]\n",
"for r, c, v in zip(rows, cols, indexed):\n",
" print('a[{}, {}] = {}'.format(r, c, v))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a class=\"anchor\" id=\"generating-random-numbers\"></a>\n",
"## Generating random numbers\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"appendix-importing-numpy\"></a>\n",
"## Appendix: Importing Numpy\n",
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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
"\n",
"\n",
"For interactive exploration/experimentation, you might want to import\n",
"Numpy like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from numpy import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This makes your Python session very similar to Matlab - you can call all\n",
"of the Numpy functions directly:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"e = array([1, 2, 3, 4, 5])\n",
"z = zeros((100, 100))\n",
"d = diag([2, 3, 4, 5])\n",
"\n",
"print(e)\n",
"print(z)\n",
"print(d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But if you are writing a script or application using Numpy, I implore you to\n",
"Numpy like this instead:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The downside to this is that you will have to prefix all Numpy functions with\n",
"`np.`, like so:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"e = np.array([1, 2, 3, 4, 5])\n",
"z = np.zeros((100, 100))\n",
"d = np.diag([2, 3, 4, 5])\n",
"\n",
"print(e)\n",
"print(z)\n",
"print(d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is a big upside, however, in that other people who have to read/use your\n",
"code will like you a lot more. This is because it will be easier for them to\n",
"figure out what the hell your code is doing. Namespaces are your friend - use\n",
"them!\n",
"\n",
"\n",
"<a class=\"anchor\" id=\"appendix-vectors-in-numpy\"></a>\n",
"## Appendix: Vectors in Numpy\n",
"One aspect of Numpy which might trip you up, and which can be quite\n",
"frustrating at times, is that Numpy has no understanding of row or column\n",
"vectors. __An array with only one dimension is neither a row, nor a column\n",
"vector - it is just a 1D array__. If you have a 1D array, and you want to use\n",
"it as a row vector, you need to reshape it to a shape of `(1, N)`. Similarly,\n",
"to use a 1D array as a column vector, you must reshape it to have shape\n",
"`(N, 1)`.\n",
"\n",
"\n",
"In general, when you are mixing 1D arrays with 2- or N-dimensional arrays, you\n",
"need to make sure that your arrays have the correct shape. For example:"
]
},
{
"cell_type": "code",