Introduction to NumPy arrays Gert-Ludwig Ingold



https://github.com/gertingold/euroscipy-numpy-tutorial.git

Python comes with batteries included Ü extensive Python standard library What about batteries for scientists (and others as well)?

from: www.scipy.org

Ü scientific Python ecosystem

+ SciKits and many other packages

Python comes with batteries included Ü extensive Python standard library What about batteries for scientists (and others as well)?

from: www.scipy.org

Ü scientific Python ecosystem

+ SciKits and many other packages

www.scipy-lectures.org

SciKits

Numpy

SciPy

Matplotlib

2015

Python

EDITION

IP[y]:

Cython

IPython

Scipy

Lecture Notes

www.scipy-lectures.org

Edited by Gaël Varoquaux Emmanuelle Gouillart Olaf Vahtras

Gaël Varoquaux • Emmanuelle Gouillart • Olav Vahtras Valentin Haenel • Nicolas P. Rougier • Ralf Gommers Fabian Pedregosa • Zbigniew Jędrzejewski-Szmek • Pauli Virtanen Christophe Combelles • Didrik Pinte • Robert Cimrman André Espaze • Adrian Chauve • Christopher Burns

docs.scipy.org/doc/numpy/

A wish list É

we want to work with vectors and matrices

 a11 a12 a21 a22  . ..  .. . an1 an2

··· ··· .. .

 a1n a2n  ..  . 

···

ann colour image as N × M × 3-array

É

we want our code to run fast

É

we want support for linear algebra

É

...

List indexing

0

1 -N

-N+1

2 -N+2

N-3

N-2 -3

N-1 -2

-1

É

indexing starts at 0

É

negative indices count from the end of the list to the beginning

List slicing basic syntax: [start:stop:step]

a[0:5] 0 0

1 1

É

3

5

4 4

5

6 6

7 7

if step=1 É É

É

3

2 2

a[5:8]

slice contains the elements start to stop-1 slice contains stop-start elements

default values: É É É

start stop step

0 (first element) N-1 (last element) 1

8

Let’s do some slicing

Matrices and lists of lists Can we use lists of lists to work with matrices?   0 1 2 3 4 5 6 7 8

matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

É

How can we extract a row?

É

How can we extract a column?

Matrices and lists of lists Can we use lists of lists to work with matrices?   0 1 2 3 4 5 6 7 8

É É

matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

How can we extract a row? How can we extract a column?

Let’s do some experiments

Matrices and lists of lists Can we use lists of lists to work with matrices?   0 1 2 3 4 5 6 7 8

matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

É

How can we extract a row? ☺

É

How can we extract a column? ☹

Lists of lists do not work like matrices

Problems with lists as matrices

É

different axes are not treated on equal footing

É

lists can contain arbitrary objects matrices have a homogeneous structure

É

list elements can be scattered in memory

Applied to matrices . . . . . . lists are conceptually inappropriate . . . lists have less performance than possible

We need a new object

ndarray multidimensional, homogeneous array of fixed-size items

Getting started

Import the NumPy package: from numpy import *

Getting started

Import the NumPy package: from numpy import * from numpy import array, sin, cos

Getting started

Import the NumPy package: from numpy import * from numpy import array, sin, cos import numpy

Getting started

Import the NumPy package: from numpy import * from numpy import array, sin, cos import numpy import numpy as np Ü

Data types Some important data types: integer

int8, int16, int32, int64, uint8, . . .

float

float16, float32, float64, . . .

complex

complex64, complex128, . . .

boolean

bool8

Unicode string Default: Python float 

Beware of overflows!

Strides (8,) 0 1 2 3 4 5



8

8

8

8

8

0

1

2

3

4

8

8

8

8

8

0

1

2

3

4

5

(24, 8)   0 1 2 3 4 5

5

24

  0 1 2 3 4 5

(16, 8) 8

8

8

8

8

0

1

2

3

4

16

16

5

Views

For the sake of efficiency, NumPy uses views if possible. É

Changing one or more matrix elements will change it in all views.

É

Example: transposition of a matrix a.T No need to copy the matrix and to create a new one

Some array creation routines É

numerical ranges: arange, linspace, logspace

É

homogeneous data: zeros, ones

É

diagonal elements: diag, eye

É

random numbers: rand, randint

 Numpy has an append()-method. Avoid it if possible.

Indexing and slicing in one dimension 1d arrays: indexing and slicing as for lists É

first element has index 0

É

negative indices count from the end

É

slices: [start:stop:step] without the element indexed by stop

É

if values are omitted: É É É

start: starting from first element stop: until (and including) the last element step: all elements between start and stop-1

Indexing and slicing in higher dimensions É É

usual slicing syntax difference to lists: slices for the various axes separated by comma

a[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

Indexing and slicing in higher dimensions É É

usual slicing syntax difference to lists: slices for the various axes separated by comma

a[: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

Indexing and slicing in higher dimensions

a[-3:, -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

Indexing and slicing in higher dimensions

a[-3:, -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

Indexing and slicing in higher dimensions

a[:, 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

Indexing and slicing in higher dimensions

a[:, 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

Indexing and slicing in higher dimensions

a[1, 3:6] 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

Indexing and slicing in higher dimensions

a[1, 3:6] 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

Indexing and slicing in higher dimensions

a[1::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

Indexing and slicing in higher dimensions

a[1::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

Fancy indexing – Boolean mask

a[a % 3 == 0] 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

Fancy indexing – array of integers

a[(1, 1, 2, 2, 3, 3), (3, 4, 2, 5, 3, 4)] 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

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

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

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

Axes   a11 a12 a13 a21 a22 a23  a31 a32 a33

axis 0

axis 1 a[0, 0]

a[0, 1]

a[0, 2]

a[1, 0]

a[1, 1]

a[1, 2]

a[2, 0]

a[2, 1]

a[2, 2]

np.sum(a) np.sum(a, axis=. . . )

ax is

0

Axes in more than two dimensions

12

13

14

15

axis 1

0 161 172 183 19 4 205 216 227 23 8

9

10

11

array([[[ 0, [ 4, [ 8,

1, 2, 3], 5, 6, 7], 9, 10, 11]],

[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]])

axis 2

create this array and produce 2d arrays by cutting perpendicular to the axes 0, 1, and 2

Matrix multiplication 0 1 2 3

4 5 6 7

6 7 26 31

0 1 2 3

4 5 6 7

6 7 26 31

0 1 2 3

4 5 6 7

6 7 26 31

try np.dot(•, •) •.dot(•) • @ • ∗)

∗)

0 1 2 3

4 5 6 7

6 7 26 31

Python≥3.5, NumPy≥1.10

Mathematical functions in NumPy Universal functions (ufuncs) take ndarrays as argument Trigonometric functions

Other special functions

sin, cos, tan, arcsin, arccos, arctan, hypot, arctan2,

i0, sinc

degrees, radians, unwrap, deg2rad, rad2deg

Hyperbolic functions sinh, cosh, tanh, arcsinh, arccosh, arctanh

Floating point routines signbit, copysign, frexp, ldexp

Arithmetic operations

Rounding

add, reciprocal, negative, multiply, divide, power,

around, round_, rint, fix, floor, ceil, trunc

subtract, true_divide, floor_divide, fmod, mod,

Sums, products, differences

modf, remainder

prod, sum, nansum, cumprod, cumsum, diff,

Handling complex numbers

ediff1d, gradient, cross, trapz

angle, real, imag, conj

Exponents and logarithms

Miscellaneous

exp, expm1, exp2, log, log10, log2, log1p,

convolve, clip, sqrt, square, absolute, fabs, sign,

logaddexp, logaddexp2

maximum, minimum, fmax, fmin, nan_to_num, real_if_close, interp

Many more special functions are provided as ufuncs by SciPy

Rules for broadcasting

Arrays can be broadcast to the same shape if one of the following points is fulfilled: 1. The arrays all have exactly the same shape. 2. The arrays all have the same number of dimensions and the length of each dimension is either a common length or 1. 3. The arrays that have too few dimensions can have their shapes prepended with a dimension of length 1 to satisfy property 2.

Broadcasting shape=(3, 4)

shape=(1,)

shape=(4,)

0

1

2

3

1

1

1

1

1

1

1

1

4

5

6

7

1

1

1

1

1

1

1

1

8

9

10

11

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

Application: Mandelbrot set zn+1 = zn2 + c, z0 = 0 Mandelbrot set contains the points for which z remains bounded.

Application: π from random numbers 1

1. Create pairs of random numbers and determine the fraction of pairs which has a distance from the origin less than one.

π/ 4 0

0

1

2. Multiply the result by four to obtain an approximation of π.

hint: count_nonzero(a) counts the number of non-zero values in the array a and also works for Boolean arrays. Remember that np.info(...) can be helpful.

Fibonacci series and linear algebra

13 21 2

3

1 1

5

Fibonacci series: 1, 1, 2, 3, 5, 8, 13, 21, . . .

8

Fn+1 = Fn +Fn−1 , F1 = F2 = 1      1 1 Fn Fn+1 or : = 1 0 Fn−1 Fn

What is the limit of Fn+1 / Fn for large n?

Eigenvalue problems a11 · · · ..  ... . 

an1 · · ·

 (k)    (k)  a1n v1 v1 ..   ..  (k)  ..   . =λ  .  .

ann

(k) vn

eigenvalue λ(k)

k = 1, . . . , n

(k) vn

 (k)  v1  ..  eigenvector  .  (k)

vn for our Fibonacci problem:      1 1 Fn Fn+1 =λ 1 0 Fn−1 Fn

We are looking for the eigenvalue larger than one.

Linear algebra in NumPy import numpy.linalg as LA Matrix and vector products dot, vdot, inner, outer, matmul, tensordot, einsum, LA.matrix_power, kron

Decompositions LA.cholesky, LA.qr, LA.svd

Matrix eigenvalues LA.eig, LA.eigh, LA.eigvals, LA.eigvalsh

Norms and other numbers LA.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

Statistics in NumPy

Order statistics amin, amax, nanmin, nanmax, ptp, percentile, nanpercentile Averages and variances median, average, mean, std, var, nanmedian, nanmean, nanstd, nanvar Correlating corrcoef, correlate, cov Histograms histogram, histogram2d, histogramdd, bincount, digitize

Application: Brownian motion -1

+1

1. Simulate several trajectories for a one-dimensional Brownian motion hint: np.random.choice 2. Plot the mean distance from the origin as a function of time 3. Plot the variance of the trajectories as a function of time

Sorting, searching, and counting in NumPy

Sorting sort, lexsort, argsort, ndarray.sort, msort, sort_complex, partition, argpartition Searching argmax, nanargmax, argmin, nanargmin, argwhere, nonzero, flatnonzero, where, searchsorted, extract Counting count_nonzero

Application: identify entry closest to 1/ 2 

 0.05344164 0.37648768 0.80691163 0.71400815 0.60825034 0.35778938 0.37393356 0.32615374 0.83118547 0.33178711 0.21548027 0.42209291

⇓   0.37648768 0.60825034 0.42209291

hint: use np.argsort

Polynomials in NumPy Power series: numpy.polynomial.polynomial Polynomial Class Polynomial Basics polyval, polyval2d, polyval3d, polygrid2d, polygrid3d, polyroots, polyfromroots Fitting polyfit, polyvander, polyvander2d, polyvander3d Calculus polyder, polyint Algebra polyadd, polysub, polymul, polymulx, polydiv, polypow Miscellaneous polycompanion, polydomain, polyzero, polyone, polyx, polytrim, polyline also: Chebyshev, Legendre, Laguerre, Hermite polynomials

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 p4.integ() Z p4 (x)dx =

1 5

= 4x3 − 30x2 + 70x − 50

x5 −

5 2

x4 +

35 3

x3 − 25x2 + 24x + C

p4.polydiv() p4 (x) 2x + 1

=

1 2

x3 −

21 4

x2 +

161 8

x−

561 16

+

945 16p4 (x)

Application: polynomial fit 1

y

0.8 0.6 0.4 0.2 0 0

1 π 5

2 π 5

3 π 5

4 π 5

π

x add some noise to a function and fit it to a polynomial see scipy.optimize.curve_fit for general fit functions

Application: image manipulation from scipy import misc face = misc.face(gray=True)

Introduction to NumPy arrays - GitHub

we want our code to run fast. ▷ we want support for linear algebra ... 7. 8 a[0:5] a[5:8]. ▷ if step=1. ▷ slice contains the elements start to stop-1 .... Indexing and slicing in higher dimensions. 0. 8. 16. 24. 32. 1. 9. 17. 25. 33. 2. 10. 18. 26. 34. 3.

4MB Sizes 7 Downloads 373 Views

Recommend Documents

Introduction to NumPy arrays - GitHub
www.scipy-lectures.org. Python. Matplotlib. SciKits. Numpy. SciPy. IPython. IP[y]:. Cython. 2015 ..... numbers and determine the fraction of pairs which has ... origin as a function of time. 3. Plot the variance of the trajectories as a function of t

Introduction to Algorithms - GitHub
Each cut is free. The management of Serling ..... scalar multiplications to compute the 100 50 matrix product A2A3, plus another. 10 100 50 D 50,000 scalar ..... Optimal substructure varies across problem domains in two ways: 1. how many ...

Introduction to R - GitHub
Nov 30, 2015 - 6 Next steps ... equals, ==, for equality comparison. .... invoked with some number of positional arguments, which are always given, plus some ...

Introduction To DCA - GitHub
Maximum-Entropy Probability Model. Joint & Conditional Entropy. Joint & Conditional Entropy. • Joint Entropy: H(X,Y ). • Conditional Entropy: H(Y |X). H(X,Y ) ...

Introduction to phylogenetics using - GitHub
Oct 6, 2016 - 2.2 Building trees . ... Limitations: no model comparison (can't test for the 'best' tree, or the 'best' model of evolution); may be .... more efficient data reduction can be achieved using the bit-level coding of polymorphic sites ....

Introduction to Fluid Simulation - GitHub
upon the notes for a Siggraph course on Fluid Simulation[Bridson. 2007]. I also used .... “At each time step all the fluid properties are moved by the flow field u.

122COM: Introduction to C++ - GitHub
All students are expected to learn some C++. .... Going to be learning C++ (approved. ). ..... Computer Science - C++ provides direct memory access, allowing.

An Introduction to BigQuery - GitHub
The ISB-CGC platform includes an interactive Web App, over a Petabyte of TCGA data in Google Genomics and Cloud Storage, and tutorials and code ...

Introduction to Framework One - GitHub
Introduction to Framework One [email protected] ... Event Management, Logging, Caching, . ... Extend framework.cfc in your Application.cfc. 3. Done. (or in the ... All controllers are passed the argument rc containing the request.context, and all v

introduction - GitHub
warehouse to assemble himself. Pain-staking and time-consuming... almost like building your own base container images. This piggy purchased high- quality ...

Introduction - GitHub
software to automate routine labor, understand speech or images, make diagnoses ..... Shaded boxes indicate components that are able to learn from data. 10 ...... is now used by many top technology companies including Google, Microsoft,.

Introduction - GitHub
data. There are many ways to learn functions, but one particularly elegant way is ... data helps to guard against over-fitting. .... Gaussian processes for big data.

Introduction - GitHub
For the case that your PDF viewer does not support this, there is a list of all the descriptions on ...... 10. Other Formats. 10.1. AMS-TEX. AMS-TEX2.0. A macro package provided by the American .... A TeX Live port for Android OS. Based on ...

Introduction - GitHub
them each year. In an aggregate travel demand model, this would be represented as 100/365.25 = 0.2737851 trucks per day. In the simulation by contrast, this is represented as ... based on the distance traveled (Table 3.3). 2FAF3 Freight Traffic Analy

Main beam representation in non-regular arrays - GitHub
2 parts. Next: analysis of aperture arrays with mutual coupling. Preliminary: Patterns of apertures – a review. Algarve meeting ... Corrected l=1.2 m, λ=3.5 m ...

Course: Introduction to Intelligent Transportation Systems - GitHub
... Introduction to Intelligent Transportation Systems. University of Tartu, Institute of Computer Science. Project: Automatic Plate Number. Recognition (APNR).

Introduction to REST and RestHUB - GitHub
2. RestHUBанаRESTful API for Oracle DB querying. 2.1. Overview. RestHub was designed .... For example we want to create a simple HTML + Javascript page.

A Beginner's Introduction to CoffeeKup - GitHub
the buffer, then calls the title function which adds it s own HTML to the buffer, and ... Now it is starting to look like real HTML you d find on an ugly web page. 2 ...

Introduction to RestKit Blake Watters - GitHub
Sep 14, 2011 - Multi-part params via RKParams. RKParams* params = [RKParams paramsWithDictionary:paramsDictionary];. NSData* imageData .... This is typically configured as a secondary target on your project. // Dump your seed data out of your backend

Introduction to Scientific Computing in Python - GitHub
Apr 16, 2016 - 1 Introduction to scientific computing with Python ...... Support for multiple parallel back-end processes, that can run on computing clusters or cloud services .... system, file I/O, string management, network communication, and ...

Glow Introduction - GitHub
Architecture: Data Flow. 1. Outputs of tasks are saved by local agents. 2. Driver remembers all data locations. 3. Inputs of next group of tasks are pulled from the ...

Arrays
Creating an Array. 65 87 30. 1. 0. 2. name[];. Example: int temperature[3]; temperature[0] = 65; temperature[1] = 87; temperature[2] = 30;. OR int temperature[] = { 65, 87, 30 }; ... Where's the bug? string class[3] = { "Sam", "Jess", "Kim" }; for (

Introduction to Handibot Software and Handibot Apps I. Hello ... - GitHub
describing the new “FabMo” software platform that runs the tools. ... as a methodology because we believe it is an effective way for small companies, ..... Page 10 ...

Data 8R Data Types and Arrays Summer 2017 1 A Test of Skill - GitHub
1 A Test of Skill ... errors) of the following lines of Python code! >>> 6 / 3 ... Luckily, they've supplied a function named bar(labelArray, dataArray) to do.