Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following...
Transcript of Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following...
![Page 1: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/1.jpg)
Introduction to NumPy arraysGert-Ludwig Ingold
� https://github.com/gertingold/euroscipy-numpy-tutorial.git
![Page 2: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/2.jpg)
Python comes with batteries includedÜ extensive Python standard library
What about batteries for scientists (and others as well)?
Ü scienti�c Python ecosystem
from:www
.sci
py.o
rg
+ SciKits and many other packages
![Page 3: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/3.jpg)
Python comes with batteries includedÜ extensive Python standard library
What about batteries for scientists (and others as well)?
Ü scienti�c Python ecosystem
from:www
.sci
py.o
rg
+ SciKits and many other packages
![Page 4: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/4.jpg)
Python comes with batteries includedÜ extensive Python standard library
What about batteries for scientists (and others as well)?
Ü scienti�c Python ecosystem
from:www
.sci
py.o
rg
+ SciKits and many other packages
![Page 5: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/5.jpg)
www.scipy-lectures.org
Python
MatplotlibSciKits Numpy
SciPy
IPython
IP[y]:
Cython
2017EDITION
Edited byGaël VaroquauxEmmanuelle GouillartOlaf Vahtras
ScipyLecture Notes
www.scipy-lectures.org
Gaël Varoquaux • Emmanuelle Gouil lart • Olav VahtrasChristopher Burns • Adrian Chauve • Robert Cimrman • Christophe Combelles
Pierre de Buyl • Ralf Gommers • André Espaze • Zbigniew Jędrzejewski-Szmek Valentin Haenel • Gert-Ludwig Ingold • Fabian Pedregosa • Didrik Pinte
Nicolas P. Rougier • Pauli Virtanen
an d man y ot her s . . .
docs.scipy.org/doc/numpy/
![Page 6: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/6.jpg)
A wish list
I we want to work with vectors and matrices
©«a11 a12 · · · a1na21 a22 · · · a2n...
.... . .
...
an1 an2 · · · ann
ª®®®®¬colour image as N × M × 3-array
I we want our code to run fastI we want support for linear algebraI . . .
![Page 7: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/7.jpg)
List indexing
0
-N
N-3
-3
1
-N+1
N-2
-2
2
-N+2
N-1
-1
I indexing starts at 0I negative indices count from the end of the list to the beginning
![Page 8: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/8.jpg)
List slicingbasic syntax: [start:stop:step]
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
a[0:5] a[5:8]
I if step=1I slice contains the elements start to stop-1I slice contains stop-start elements
I start, stop, and also step can be negativeI default values:
I start 0, i.e. starting from the �rst elementI stop N, i.e up to and including the last elementI step 1
![Page 9: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/9.jpg)
Let’s do some slicing
![Page 10: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/10.jpg)
Matrices and lists of lists
Can we use lists of lists to work with matrices?
©«0 1 23 4 56 7 8
ª®¬matrix = [[0, 1, 2],
[3, 4, 5],[6, 7, 8]]
I How can we extract a row?
�
I How can we extract a column?
�
![Page 11: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/11.jpg)
Matrices and lists of lists
Can we use lists of lists to work with matrices?
©«0 1 23 4 56 7 8
ª®¬matrix = [[0, 1, 2],
[3, 4, 5],[6, 7, 8]]
I How can we extract a row?
�
I How can we extract a column?
�
Let’s do some experiments
![Page 12: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/12.jpg)
Matrices and lists of lists
Can we use lists of lists to work with matrices?
©«0 1 23 4 56 7 8
ª®¬matrix = [[0, 1, 2],
[3, 4, 5],[6, 7, 8]]
I How can we extract a row? �I How can we extract a column? �
Lists of lists do not work like matrices
![Page 13: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/13.jpg)
Problems with lists as matrices
I di�erent axes are not treated on equal footingI lists can contain arbitrary objectsmatrices have a homogeneous structure
I list elements can be scattered in memory
Applied to matrices . . .. . . lists are conceptually inappropriate. . . lists have less performance than possible
![Page 14: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/14.jpg)
We need a new object
ndarraymultidimensional, homogeneous array of �xed-size items
![Page 15: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/15.jpg)
Getting started
Import the NumPy package:
from numpy import *
![Page 16: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/16.jpg)
Getting started
Import the NumPy package:
from numpy import *from numpy import array, sin, cos
![Page 17: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/17.jpg)
Getting started
Import the NumPy package:
from numpy import *from numpy import array, sin, cosimport numpy
![Page 18: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/18.jpg)
Getting started
Import the NumPy package:
from numpy import *from numpy import array, sin, cosimport numpyimport numpy as np
Ü
![Page 19: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/19.jpg)
Getting started
Import the NumPy package:
from numpy import *from numpy import array, sin, cosimport numpyimport numpy as np
ÜCheck the NumPy version:np.__version__
![Page 20: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/20.jpg)
Data typesSome important data types:
integer int8, int16, int32, int64, uint8, . . .
�oat float16, float32, float64, . . .
complex complex64, complex128, . . .
boolean bool8
Unicode string
default type: float64
� Beware of over�ows!
![Page 21: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/21.jpg)
Strides
(0 1 2 3 4 5
) (8,)
0 1 2 3 4 5
8 8 8 8 8
(0 1 23 4 5
) (24, 8)
0 1 2 3 4 5
8 8 8 8 8
24
©«0 12 34 5
ª®¬(16, 8)
0 1 2 3 4 5
8 8 8 8 8
16 16
![Page 22: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/22.jpg)
Views
For the sake of e�ciency, NumPy uses views if possible.
I Changing one or more matrix elements will change it in all views.I Example: transposition of a matrix a.TNo need to copy the matrix and to create a new one
![Page 23: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/23.jpg)
Some array creation routines
I numerical ranges: arange, linspace, logspaceI homogeneous data: zeros, onesI diagonal elements: diag, eyeI random numbers: rand, randint
� Numpy has an append()-method. Avoid it if possible.
![Page 24: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/24.jpg)
Indexing and slicing in one dimension
1d arrays: indexing and slicing as for lists
I �rst element has index 0I negative indices count from the endI slices: [start:stop:step]
without the element indexed by stopI if values are omitted:
I start: starting from �rst elementI stop: until (and including) the last elementI step: all elements between start and stop-1
![Page 25: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/25.jpg)
Indexing and slicing in higher dimensionsI usual slicing syntaxI di�erence to lists:slices for the various axes separated by comma
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[2, -3]
![Page 26: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/26.jpg)
Indexing and slicing in higher dimensionsI usual slicing syntaxI di�erence to lists:slices for the various axes separated by comma
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[:3, :5]
![Page 27: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/27.jpg)
Indexing and slicing in higher dimensions
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[-3:, -3:]
![Page 28: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/28.jpg)
Indexing and slicing in higher dimensions
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[-3:, -3:]
![Page 29: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/29.jpg)
Indexing and slicing in higher dimensions
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[:, 3]
![Page 30: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/30.jpg)
Indexing and slicing in higher dimensions
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[:, 3]
![Page 31: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/31.jpg)
Indexing and slicing in higher dimensions
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[1, 3:6]
![Page 32: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/32.jpg)
Indexing and slicing in higher dimensions
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[1, 3:6]
![Page 33: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/33.jpg)
Indexing and slicing in higher dimensions
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[1::2, ::3]
![Page 34: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/34.jpg)
Indexing and slicing in higher dimensions
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[1::2, ::3]
![Page 35: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/35.jpg)
Fancy indexing – Boolean mask
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[a % 3 == 0]
![Page 36: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/36.jpg)
Fancy indexing – array of integers
0
8
16
24
32
1
9
17
25
33
2
10
18
26
34
3
11
19
27
35
4
12
20
28
36
5
13
21
29
37
6
14
22
30
38
7
15
23
31
39
a[(1, 1, 2, 2, 3, 3), (3, 4, 2, 5, 3, 4)]
![Page 37: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/37.jpg)
Application: sieve of Eratosthenes
0 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
0 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
2
0 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
2 3
0 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
2 3 5
0 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
2 3 5 7
0 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
2 3 5 7 11 13 17 19 23
29 31 37 41 43 47
![Page 38: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/38.jpg)
Axes
©«a11 a12 a13a21 a22 a23a31 a32 a33
ª®¬a[0, 0]a[1, 0]a[2, 0]
a[0, 1]a[1, 1]a[2, 1]
a[0, 2]a[1, 2]a[2, 2]ax
is0
axis 1
np.sum(a)np.sum(a, axis=. . . )
![Page 39: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/39.jpg)
Axes in more than two dimensions
12 13 14 15
16 17 18 19
20 21 22 230 1 2 3
4 5 6 7
8 9 10 11
axis0
axis1
axis 2
array([[[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]],
[[12, 13, 14, 15],[16, 17, 18, 19],[20, 21, 22, 23]]])
create this array and produce 2d arrays bycutting perpendicular to the axes 0, 1, and 2
![Page 40: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/40.jpg)
Matrix multiplication
0213
4657
626
731
0213
4657
626
731
0213
4657
626
731
0213
4657
626
731
try np.dot(•, •)•.dot(•)•@ • ∗)
∗) Python≥3.5, NumPy≥1.10
![Page 41: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/41.jpg)
Mathematical functions in NumPy
Universal functions (ufuncs) take ndarrays as argument
Trigonometric functionssin, cos, tan, arcsin, arccos, arctan, hypot, arctan2, degrees,
radians, unwrap, deg2rad, rad2deg
Hyperbolic functionssinh, cosh, tanh, arcsinh, arccosh, arctanh
Roundingaround, round_, rint, �x, �oor, ceil, trunc
Sums, products, di�erencesprod, sum, nansum, cumprod, cumsum, di�, edi�1d, gradient,
cross, trapz
Exponents and logarithmsexp, expm1, exp2, log, log10, log2, log1p, logaddexp, logaddexp2
Other special functionsi0, sinc
Floating point routinessignbit, copysign, frexp, ldexp
Arithmetic operationsadd, reciprocal, negative, multiply, divide, power, subtract,
true_divide, �oor_divide, fmod, mod, modf, remainder
Handling complex numbersangle, real, imag, conj
Miscellaneousconvolve, clip, sqrt, square, absolute, fabs, sign, maximum,
minimum, fmax, fmin, nan_to_num, real_if_close, interp
Many more special functions are provided as ufuncs by SciPy
![Page 42: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/42.jpg)
Rules for broadcasting
Arrays can be broadcast to the same shape if one of the following pointsis ful�lled:1. The arrays all have exactly the same shape.2. The arrays all have the same number of dimensions and the lengthof each dimension is either a common length or 1.
3. The arrays that have too few dimensions can have their shapesprepended with a dimension of length 1 to satisfy property 2.
![Page 43: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/43.jpg)
Broadcasting
shape=(3, 4)
0
4
8
1
5
9
2
6
10
3
7
11
shape=(1,)
1
1
1
1
1
1
1
1
1
1
1
1
1
shape=(4,)
1
1
1
1
1
1
1
1
1
1
1
1
1 1 1 1
shape=(3,)
1 1 1
shape=(3, 1)
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
![Page 44: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/44.jpg)
Application: Mandelbrot set
zn+1 = z2n + c, z0 = 0
Mandelbrot set contains the points for which z remains bounded.
![Page 45: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/45.jpg)
Application: π from random numbers
00
1
1
π/41. Create pairs of random numbers anddetermine the fraction of pairs whichhas a distance from the origin lessthan one.
2. Multiply the result by four to obtain anapproximation of π .
hint: count_nonzero(a) counts the number of non-zero values in thearray a and also works for Boolean arrays. Remember thatnp.info(...) can be helpful.
![Page 46: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/46.jpg)
Fibonacci series and linear algebra
1 123
58
13
21
Fibonacci series:1, 1, 2, 3, 5, 8, 13, 21, . . .
Fn+1 = Fn + Fn−1, F1 = F2 = 1
or :(1 11 0
) (FnFn−1
)=
(Fn+1Fn
)What is the limit of Fn+1/Fn for large n?
![Page 47: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/47.jpg)
Eigenvalue problems
©«a11 · · · a1n.... . .
...
an1 · · · ann
ª®®¬©«v(k)1...
v(k)n
ª®®¬ = λ(k)©«v(k)1...
v(k)n
ª®®¬ k = 1, . . . , n
eigenvalue λ(k) eigenvector©«v(k)1...
v(k)n
ª®®¬for our Fibonacci problem:(
1 11 0
) (FnFn−1
)= λ
(Fn+1Fn
)We are looking for the eigenvalue larger than one.
![Page 48: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/48.jpg)
Linear algebra in NumPy
import numpy.linalg as LA
Matrix and vector productsdot, vdot, inner, outer, matmul, tensordot, einsum, LA.matrix_power, kron
DecompositionsLA.cholesky, LA.qr, LA.svd
Matrix eigenvaluesLA.eig, LA.eigh, LA.eigvals, LA.eigvalsh
Norms and other numbersLA.norm, LA.cond, LA.det, LA.matrix_rank, LA.slogdet, trace
Solving equations and inverting matrices LA.solve, LA.tensorsolve, LA.lstsq,LA.inv, LA.pinv, LA.tensorinv
hint: see also the methods for linear algebra in SciPy
![Page 49: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/49.jpg)
Statistics in NumPy
Order statisticsamin, amax, nanmin, nanmax, ptp, percentile, nanpercentile
Averages and variancesmedian, average, mean, std, var, nanmedian, nanmean, nanstd, nanvar
Correlatingcorrcoef, correlate, cov
Histogramshistogram, histogram2d, histogramdd, bincount, digitize
![Page 50: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/50.jpg)
Application: Brownian motion
+1-1
1. Simulate several trajectories for a one-dimensional Brownianmotionhint: np.random.choice
2. Plot the mean distance from the origin as a function of time3. Plot the variance of the trajectories as a function of time
![Page 51: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/51.jpg)
Sorting, searching, and counting in NumPy
Sortingsort, lexsort, argsort, ndarray.sort, msort, sort_complex, partition, argpartition
Searchingargmax, nanargmax, argmin, nanargmin, argwhere, nonzero, �atnonzero, where,searchsorted, extract
Countingcount_nonzero
![Page 52: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/52.jpg)
Application: identify entry closest to 1/2
©«0.05344164 0.37648768 0.80691163 0.714008150.60825034 0.35778938 0.37393356 0.326153740.83118547 0.33178711 0.21548027 0.42209291
ª®¬⇓
©«0.376487680.608250340.42209291
ª®¬hint: use np.argsort
![Page 53: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/53.jpg)
Polynomials in NumPy
Power series: numpy.polynomial.polynomial
Polynomial ClassPolynomialBasicspolyval, polyval2d, polyval3d, polygrid2d, polygrid3d, polyroots, polyfromrootsFittingpoly�t, polyvander, polyvander2d, polyvander3dCalculuspolyder, polyintAlgebrapolyadd, polysub, polymul, polymulx, polydiv, polypowMiscellaneouspolycompanion, polydomain, polyzero, polyone, polyx, polytrim, polyline
also: Chebyshev, Legendre, Laguerre, Hermite polynomials
![Page 54: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/54.jpg)
Some examples
P.Polynomial([24, -50, 35, -10, 1])
p4(x) = x4 − 10x3 + 35x2 − 50x + 24 = (x − 1)(x − 2)(x − 3)(x − 4)
p4.deriv()dp4(x)dx
= 4x3 − 30x2 + 70x − 50
p4.integ() ∫p4(x)dx = 1
5x5 − 5
2x4 +
353x3 − 25x2 + 24x + C
p4.polydiv()
p4(x)2x + 1
=12x3 − 21
4x2 +
1618x − 561
16+
94516p4(x)
![Page 55: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/55.jpg)
Application: polynomial �t
01
5π
2
5π
3
5π
4
5π π
0
0.2
0.4
0.6
0.8
1
x
y
add some noise to a function and �t it to apolynomial
see scipy.optimize.curve_fit for general �t functions
![Page 56: Introduction to NumPy arrays · Arrays can be broadcast to the same shape if one of the following points is ful˝lled: 1.The arrays all have exactly the same shape. 2.The arrays all](https://reader033.fdokument.com/reader033/viewer/2022042807/5f7c69fe0850f838d813af65/html5/thumbnails/56.jpg)
Application: image manipulationfrom scipy import miscface = misc.face(gray=True)