SlideShare a Scribd company logo
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)?
Ü scientific Python ecosystem
from:
www.scipy.org
+ SciKits and many other packages
Python comes with batteries included
Ü extensive Python standard library
What about batteries for scientists (and others as well)?
Ü scientific Python ecosystem
from:
www.scipy.org
+ SciKits and many other packages
Python comes with batteries included
Ü extensive Python standard library
What about batteries for scientists (and others as well)?
Ü scientific Python ecosystem
from:
www.scipy.org
+ SciKits and many other packages
www.scipy-lectures.org
Python
Matplotlib
SciKits Numpy
SciPy
IPython
IP[y]:
Cython
2017
EDITION
Edited by
Gaël Varoquaux
Emmanuelle Gouillart
Olaf Vahtras
Scipy
Lecture Notes
www.scipy-lectures.org
Gaël Varoquaux • Emmanuelle Gouillart • Olav Vahtras
Christopher 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
and many others...
docs.scipy.org/doc/numpy/
A wish list
I we want to work with vectors and matrices
©
­
­
­
­
«
a11 a12 · · · a1n
a21 a22 · · · a2n
.
.
.
.
.
.
...
.
.
.
an1 an2 · · · ann
ª
®
®
®
®
¬
colour image as N × M × 3-array
I we want our code to run fast
I we want support for linear algebra
I ...
List indexing
0
-N
N-3
-3
1
-N+1
N-2
-2
2
-N+2
N-1
-1
I indexing starts at 0
I negative indices count from the end of the list to the beginning
List slicing
basic 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=1
I slice contains the elements start to stop-1
I slice contains stop-start elements
I start, stop, and also step can be negative
I default values:
I start 0, i.e. starting from the first element
I stop N, i.e up to and including the last element
I step 1
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]]
I How can we extract a row?
I 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]]
I How can we extract a row?
I 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]]
I How can we extract a row? 
I How can we extract a column? ‚
Lists of lists do not work like matrices
Problems with lists as matrices
I different axes are not treated on equal footing
I lists can contain arbitrary objects
matrices 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
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
Ü
Getting started
Import the NumPy package:
from numpy import *
from numpy import array, sin, cos
import numpy
import numpy as np
Ü
Check the NumPy version:
np.__version__
Data types
Some important data types:
integer int8, int16, int32, int64, uint8, ...
float float16, float32, float64, ...
complex complex64, complex128, ...
boolean bool8
Unicode string
default type: float64
 Beware of overflows!
Strides
0 1 2 3 4 5

(8,)
0 1 2 3 4 5
8 8 8 8 8

0 1 2
3 4 5

(24, 8)
0 1 2 3 4 5
8 8 8 8 8
24
©
­
«
0 1
2 3
4 5
ª
®
¬
(16, 8)
0 1 2 3 4 5
8 8 8 8 8
16 16
Views
For the sake of efficiency, 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.T
No need to copy the matrix and to create a new one
Some array creation routines
I numerical ranges: arange, linspace, logspace
I homogeneous data: zeros, ones
I diagonal elements: diag, eye
I 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
I first element has index 0
I negative indices count from the end
I slices: [start:stop:step]
without the element indexed by stop
I if values are omitted:
I start: starting from first element
I stop: until (and including) the last element
I step: all elements between start and stop-1
Indexing and slicing in higher dimensions
I usual slicing syntax
I difference 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]
Indexing and slicing in higher dimensions
I usual slicing syntax
I difference 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]
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:]
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:]
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]
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]
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]
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]
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]
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]
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]
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)]
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
Axes
©
­
«
a11 a12 a13
a21 a22 a23
a31 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]
axis
0
axis 1
np.sum(a)
np.sum(a, axis=...)
Axes in more than two dimensions
12 13 14 15
16 17 18 19
20 21 22 23
0 1 2 3
4 5 6 7
8 9 10 11
a
x
i
s
0
axis
1
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 by
cutting perpendicular to the axes 0, 1, and 2
Matrix multiplication
0
2
1
3
4
6
5
7
6
26
7
31
0
2
1
3
4
6
5
7
6
26
7
31
0
2
1
3
4
6
5
7
6
26
7
31
0
2
1
3
4
6
5
7
6
26
7
31
try np.dot(•, •)
•.dot(•)
• @ •∗)
∗) Python≥3.5, NumPy≥1.10
Mathematical functions in NumPy
Universal functions (ufuncs) take ndarrays as argument
Trigonometric functions
sin, cos, tan, arcsin, arccos, arctan, hypot, arctan2, degrees,
radians, unwrap, deg2rad, rad2deg
Hyperbolic functions
sinh, cosh, tanh, arcsinh, arccosh, arctanh
Rounding
around, round_, rint, fix, floor, ceil, trunc
Sums, products, differences
prod, sum, nansum, cumprod, cumsum, diff, ediff1d, gradient,
cross, trapz
Exponents and logarithms
exp, expm1, exp2, log, log10, log2, log1p, logaddexp, logaddexp2
Other special functions
i0, sinc
Floating point routines
signbit, copysign, frexp, ldexp
Arithmetic operations
add, reciprocal, negative, multiply, divide, power, subtract,
true_divide, floor_divide, fmod, mod, modf, remainder
Handling complex numbers
angle, real, imag, conj
Miscellaneous
convolve, 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
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)
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
Application: Mandelbrot set
zn+1 = z2
n + c, z0 = 0
Mandelbrot set contains the points for which z remains bounded.
Application: π from random numbers
0
0
1
1
π/4
1. Create pairs of random numbers and
determine the fraction of pairs which
has a distance from the origin less
than one.
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
1 1
2
3
5
8
13
21
Fibonacci series:
1, 1, 2, 3, 5, 8, 13, 21, ...
Fn+1 = Fn + Fn−1, F1 = F2 = 1
or :

1 1
1 0
 
Fn
Fn−1

=

Fn+1
Fn

What is the limit of Fn+1/Fn for large n?
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 1
1 0
 
Fn
Fn−1

= λ

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
= 4x3
− 30x2
+ 70x − 50
p4.integ()
∫
p4(x)dx =
1
5
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
0
1
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 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)

More Related Content

PPTX
1_ Introduction Python.pptx python is a data
PPTX
Oct27
PPTX
Introduction to Algorithms
PPTX
CSE115 C Programming Multidimensional Array Introduction
PDF
Creating a Custom Serialization Format (Gophercon 2017)
PDF
CS.3.Arrays.pdf
PDF
CSE031.Lecture_07-FlowCharts_Pseudocode .Part_II.pdf
PDF
The Ring programming language version 1.8 book - Part 28 of 202
1_ Introduction Python.pptx python is a data
Oct27
Introduction to Algorithms
CSE115 C Programming Multidimensional Array Introduction
Creating a Custom Serialization Format (Gophercon 2017)
CS.3.Arrays.pdf
CSE031.Lecture_07-FlowCharts_Pseudocode .Part_II.pdf
The Ring programming language version 1.8 book - Part 28 of 202

Similar to Numpy intro presentation for college.pdf (20)

PPTX
Oct8 - 131 slid
PDF
Slicing in Python - What is It?
PPTX
Cluto presentation
PDF
Matrix algebra in_r
PPT
Introduction of MatLab
PDF
The Ring programming language version 1.9 book - Part 30 of 210
PPT
PPT
Matlab_Simulink_Tutorial.ppt
PPTX
Data Science for Folks Without (or With!) a Ph.D.
PDF
Regression and Classification with R
PDF
PDF
Ch07 linearspacealignment
PDF
The Ring programming language version 1.6 book - Part 25 of 189
PPT
Kaizen cso002 l1
PPTX
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
PPTX
Algorithim lec1.pptx
PDF
Intoduction to numpy
PDF
Data Structures Lab
PPTX
Discrete Math IP4 - Automata Theory
PDF
CE344L-200365-Lab2.pdf
Oct8 - 131 slid
Slicing in Python - What is It?
Cluto presentation
Matrix algebra in_r
Introduction of MatLab
The Ring programming language version 1.9 book - Part 30 of 210
Matlab_Simulink_Tutorial.ppt
Data Science for Folks Without (or With!) a Ph.D.
Regression and Classification with R
Ch07 linearspacealignment
The Ring programming language version 1.6 book - Part 25 of 189
Kaizen cso002 l1
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
Algorithim lec1.pptx
Intoduction to numpy
Data Structures Lab
Discrete Math IP4 - Automata Theory
CE344L-200365-Lab2.pdf
Ad

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Institutional Correction lecture only . . .
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
master seminar digital applications in india
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Computing-Curriculum for Schools in Ghana
Lesson notes of climatology university.
O7-L3 Supply Chain Operations - ICLT Program
Cell Structure & Organelles in detailed.
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
RMMM.pdf make it easy to upload and study
Institutional Correction lecture only . . .
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Final Presentation General Medicine 03-08-2024.pptx
A systematic review of self-coping strategies used by university students to ...
master seminar digital applications in india
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Chinmaya Tiranga quiz Grand Finale.pdf
Computing-Curriculum for Schools in Ghana
Ad

Numpy intro presentation for college.pdf

  • 1. Introduction to NumPy arrays Gert-Ludwig Ingold ‡ https://github.com/gertingold/euroscipy-numpy-tutorial.git
  • 2. Python comes with batteries included Ü extensive Python standard library What about batteries for scientists (and others as well)? Ü scientific Python ecosystem from: www.scipy.org + SciKits and many other packages
  • 3. Python comes with batteries included Ü extensive Python standard library What about batteries for scientists (and others as well)? Ü scientific Python ecosystem from: www.scipy.org + SciKits and many other packages
  • 4. Python comes with batteries included Ü extensive Python standard library What about batteries for scientists (and others as well)? Ü scientific Python ecosystem from: www.scipy.org + SciKits and many other packages
  • 5. www.scipy-lectures.org Python Matplotlib SciKits Numpy SciPy IPython IP[y]: Cython 2017 EDITION Edited by Gaël Varoquaux Emmanuelle Gouillart Olaf Vahtras Scipy Lecture Notes www.scipy-lectures.org Gaël Varoquaux • Emmanuelle Gouillart • Olav Vahtras Christopher 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 and many others... docs.scipy.org/doc/numpy/
  • 6. A wish list I we want to work with vectors and matrices © ­ ­ ­ ­ « a11 a12 · · · a1n a21 a22 · · · a2n . . . . . . ... . . . an1 an2 · · · ann ª ® ® ® ® ¬ colour image as N × M × 3-array I we want our code to run fast I we want support for linear algebra I ...
  • 7. List indexing 0 -N N-3 -3 1 -N+1 N-2 -2 2 -N+2 N-1 -1 I indexing starts at 0 I negative indices count from the end of the list to the beginning
  • 8. List slicing basic 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=1 I slice contains the elements start to stop-1 I slice contains stop-start elements I start, stop, and also step can be negative I default values: I start 0, i.e. starting from the first element I stop N, i.e up to and including the last element I step 1
  • 9. Let’s do some slicing
  • 10. 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]] I How can we extract a row? I How can we extract a column?
  • 11. 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]] I How can we extract a row? I How can we extract a column? Let’s do some experiments
  • 12. 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]] I How can we extract a row? I How can we extract a column? ‚ Lists of lists do not work like matrices
  • 13. Problems with lists as matrices I different axes are not treated on equal footing I lists can contain arbitrary objects matrices 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
  • 14. We need a new object ndarray multidimensional, homogeneous array of fixed-size items
  • 15. Getting started Import the NumPy package: from numpy import *
  • 16. Getting started Import the NumPy package: from numpy import * from numpy import array, sin, cos
  • 17. Getting started Import the NumPy package: from numpy import * from numpy import array, sin, cos import numpy
  • 18. Getting started Import the NumPy package: from numpy import * from numpy import array, sin, cos import numpy import numpy as np Ü
  • 19. Getting started Import the NumPy package: from numpy import * from numpy import array, sin, cos import numpy import numpy as np Ü Check the NumPy version: np.__version__
  • 20. Data types Some important data types: integer int8, int16, int32, int64, uint8, ... float float16, float32, float64, ... complex complex64, complex128, ... boolean bool8 Unicode string default type: float64  Beware of overflows!
  • 21. Strides 0 1 2 3 4 5 (8,) 0 1 2 3 4 5 8 8 8 8 8 0 1 2 3 4 5 (24, 8) 0 1 2 3 4 5 8 8 8 8 8 24 © ­ « 0 1 2 3 4 5 ª ® ¬ (16, 8) 0 1 2 3 4 5 8 8 8 8 8 16 16
  • 22. Views For the sake of efficiency, 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.T No need to copy the matrix and to create a new one
  • 23. Some array creation routines I numerical ranges: arange, linspace, logspace I homogeneous data: zeros, ones I diagonal elements: diag, eye I random numbers: rand, randint  Numpy has an append()-method. Avoid it if possible.
  • 24. Indexing and slicing in one dimension 1d arrays: indexing and slicing as for lists I first element has index 0 I negative indices count from the end I slices: [start:stop:step] without the element indexed by stop I if values are omitted: I start: starting from first element I stop: until (and including) the last element I step: all elements between start and stop-1
  • 25. Indexing and slicing in higher dimensions I usual slicing syntax I difference 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]
  • 26. Indexing and slicing in higher dimensions I usual slicing syntax I difference 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]
  • 27. 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:]
  • 28. 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:]
  • 29. 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]
  • 30. 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]
  • 31. 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]
  • 32. 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]
  • 33. 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]
  • 34. 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]
  • 35. 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]
  • 36. 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)]
  • 37. 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
  • 38. Axes © ­ « a11 a12 a13 a21 a22 a23 a31 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] axis 0 axis 1 np.sum(a) np.sum(a, axis=...)
  • 39. Axes in more than two dimensions 12 13 14 15 16 17 18 19 20 21 22 23 0 1 2 3 4 5 6 7 8 9 10 11 a x i s 0 axis 1 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 by cutting perpendicular to the axes 0, 1, and 2
  • 41. Mathematical functions in NumPy Universal functions (ufuncs) take ndarrays as argument Trigonometric functions sin, cos, tan, arcsin, arccos, arctan, hypot, arctan2, degrees, radians, unwrap, deg2rad, rad2deg Hyperbolic functions sinh, cosh, tanh, arcsinh, arccosh, arctanh Rounding around, round_, rint, fix, floor, ceil, trunc Sums, products, differences prod, sum, nansum, cumprod, cumsum, diff, ediff1d, gradient, cross, trapz Exponents and logarithms exp, expm1, exp2, log, log10, log2, log1p, logaddexp, logaddexp2 Other special functions i0, sinc Floating point routines signbit, copysign, frexp, ldexp Arithmetic operations add, reciprocal, negative, multiply, divide, power, subtract, true_divide, floor_divide, fmod, mod, modf, remainder Handling complex numbers angle, real, imag, conj Miscellaneous convolve, 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
  • 42. 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.
  • 44. Application: Mandelbrot set zn+1 = z2 n + c, z0 = 0 Mandelbrot set contains the points for which z remains bounded.
  • 45. Application: π from random numbers 0 0 1 1 π/4 1. Create pairs of random numbers and determine the fraction of pairs which has a distance from the origin less than one. 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.
  • 46. Fibonacci series and linear algebra 1 1 2 3 5 8 13 21 Fibonacci series: 1, 1, 2, 3, 5, 8, 13, 21, ... Fn+1 = Fn + Fn−1, F1 = F2 = 1 or : 1 1 1 0 Fn Fn−1 = Fn+1 Fn What is the limit of Fn+1/Fn for large n?
  • 47. 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 1 1 0 Fn Fn−1 = λ Fn+1 Fn We are looking for the eigenvalue larger than one.
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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
  • 52. 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
  • 53. 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
  • 54. 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 5 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)
  • 55. Application: polynomial fit 0 1 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 fit it to a polynomial see scipy.optimize.curve_fit for general fit functions
  • 56. Application: image manipulation from scipy import misc face = misc.face(gray=True)