Professional Documents
Culture Documents
Slides 2 PDF
Slides 2 PDF
Numerical Computing
with Numpy
© 2001-2019, Enthought, Inc.
Enthought, Inc.
200 W Cesar Chavez
Suite 202
Austin, TX 78701
www.enthought.com
Q2-2019
letter
Introduction to Numerical Computing
with Numpy
Enthought, Inc.
www.enthought.com
Introduction 1
NumPy 2
An interlude: Matplotlib basics 4
Introducing NumPy Arrays 17
Multi-Dimensional Arrays 21
Slicing/Indexing Arrays 22
Fancy Indexing 28
Creating arrays 32
Array Creation Functions 34
Array Calculation Methods 37
Array Broadcasting 45
Universal Function Methods 53
The array data structure 59
© 2008-2018 Enthought, Inc. 1
Hi there!
subplot
>>> plt.subplot(2, 1, 1)
>>> plt.plot(x)
>>> a.dtype
dtype('int32')
# Return the number of bytes
# used by the data portion of
# the array.
>>> a.nbytes
>>> a.ndim 16
1
# in-place operations
>>> x *= c
>>> x
array([ 0.,0.628,…,6.283])
a 10 1 2 3
index 0 1 2 3
4 >>> a[1]
>>> a.size
2 x 4 = 8 a 2 0 1 2 3 array([10, 11, 12, -1])
8 10 11 12 13
0
a 1 0 1 2 3
dim 1 10 11 12 -1
>>> a.ndim
2 a 0 1 2 3
dim 0 0 1 2 3
10 11 12 13
© 2008-2018 Enthought, Inc. 21
0 1 2 3 4 5
0
0 1 2 3 4 5
1
10 11 12 13 14 15
2
20 21 22 23 24 25
3
30 31 32 33 34 35
4
40 41 42 43 44 45
5
50 51 52 53 54 55
a = np.arange(25).reshape(5, 5)
0 1 2 3 4
0
0 1 2 3 4
1
5 6 7 8 9
2
10 11 12 13 14
3
15 16 17 18 19
4
20 21 22 23 24
# changing b changed a!
>>> a
array([ 0, 1, 10, 3, 4])
-3 -2 -1
0 1 2 3 4 5 6 7
a 0 10 20 30 40 50 60 70
y 10 20 50
© 2008-2018 Enthought, Inc. 28
>>> a[[0, 1, 2, 3, 4],
... [1, 2, 3, 4, 5]]
array([ 1, 12, 23, 34, 45])
0 1 2 3 4 5
>>> a[3:, [0, 2, 5]]
array([[30, 32, 35], 0
[40, 42, 45], 0 1 2 3 4 5
1
[50, 52, 55]]) 10 11 12 13 14 15
2
>>> mask = np.array( 20 21 22 23 24 25
3
... [1, 0, 1, 0, 0, 1],
30 31 32 33 34 35
... dtype=bool) 4
>>> a[mask, 2] 40 41 42 43 44 45
array([2, 22, 52]) 5
50 51 52 53 54 55
a = np.arange(25).reshape(5, 5)
0 1 2 3 4
0
0 1 2 3 4
1
5 6 7 8 9
2
10 11 12 13 14
3
15 16 17 18 19
4
20 21 22 23 24
0 1
0 1 2 3
0 1 2
-3 -3
-1 -1 -1 -1
-2 -2 -2
-4
arange()
linspace()
array()
zeros()
ones()
a 0 1 2 3
>>> a = np.array([0,1.,2,3],
... dtype='float32')
>>> a.dtype
dtype('float32') Base 2 Base 10
float64
dtype
>>> np.ones((2, 3),
... dtype='float32')
>>> np.arange(4) array([[ 1., 1., 1.],
array([0, 1, 2, 3]) [ 1., 1., 1.]],
>>> np.arange(0, 2*pi, pi/4) dtype=float32)
array([ 0.000, 0.785, 1.571, >>> np.zeros(3)
2.356, 3.142, 3.927, 4.712,
array([ 0., 0., 0.])
5.497])
0 1 2
# Be careful… 0
1. 1. 1.
>>> np.arange(1.5, 2.1, 0.3) 1
0. 0. 0.
array([ 1.5, 1.8, 2.1]) 1. 1. 1.
dtype: float64
dtype: float32
© 2008-2018 Enthought, Inc. zeros(3) zeros((3, )) 34
# Generate an n by n identity # empty(shape, dtype=float64,
# array. The default dtype is # order='C')
# float64. >>> np.empty(2)
>>> a = np.identity(4) array([1.78021120e-306,
>>> a 6.95357225e-308])
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.], # array filled with 5.0
[ 0., 0., 1., 0.], >>> a = np.full(2, 5.0)
[ 0., 0., 0., 1.]]) array([5., 5.])
>>> a.dtype
dtype('float64') # alternative approaches
>>> np.identity(4, dtype=int) # (slower)
array([[ 1, 0, 0, 0], >>> a = np.empty(2)
[ 0, 1, 0, 0], >>> a.fill(4.0)
[ 0, 0, 1, 0], >>> a
[ 0, 0, 0, 1]]) array([4., 4.])
>>> a[:] = 3.0
>>> a
array([3., 3.])
© 2008-2018 Enthought, Inc. 35
# loadtxt() automatically
# generates an array from the
# Generate N evenly spaced # txt file
# elements on a log scale arr = np.loadtxt('Data.txt',
# between base**start and ... skiprows=1,
... dtype=int, delimiter=",",
# base**stop (default base=10)
... usecols = (0,1,2,4),
>>> np.logspace(0, 1, 5) ... comments = "%")
array([1., 1.78, 3.16, 5.62, 10.])
# Save an array into a txt file
np.savetxt('filename', arr)
© 2008-2018 Enthought, Inc. 36
1 2 3 6
4 5 6 15
5 7 9
a = np.arange(-15, 15).reshape(5, 6) ** 2
0 1 2 3 4 5
0
225 196 169 144 121 100
1
81 64 49 36 25 16
2
9 4 1 0 1 4
3
9 16 25 36 49 64
4
81 100 121 144 169 196
4x3 3
0 0 0 0 1 2 0 0 0 0 1 2 0 1 2
10 10 10 + = 10 10 10 + 0 1 2
= 10 11 12
20 20 20 20 20 20 0 1 2 20 21 22
30 30 30 30 30 30 0 1 2 30 31 32
4x1 3
0 0 1 2 0 0 0 0 1 2
10 + = 10 10 10 + 0 1 2 =
20 20 20 20 0 1 2
30 30 30 30 0 1 2
4x3 4
0 0 0 0 1 2 3
10 10 10 +
20 20 20
30 30 30
>>> a = array([0, 10, 20, 30])
>>> b = array([0, 1, 2])
>>> y = a[:, newaxis] + b
a b
4x1 3
0 0 0 0 1 2 0 1 2
10 10 10 + 0 1 2 = 10 11 12
20 20 20 0 1 2 20 21 22
30 30 30 0 1 2 30 31 32
np.repeat()
horizon
yi 0 1 2 2
1
0
xi 0 0 1 2 2
1 1
1 0 1 2 2 2 2
2 0 1 2
zi data_cube
Universal Function Methods
The mathematical, comparative, logical, and bitwise
operators op that take two arguments (binary operators)
have special methods that operate on arrays:
op.reduce(a,axis=0)
op.accumulate(a,axis=0)
op.outer(a,b)
op.reduceat(a,indices)
53
op.reduce()
1 0
60 64 68
0
3 0 1 2
0 1 2
33 10 11 12
10 11 12
63 20 21 22
20 21 22
93 30 31 32
30 31 32
55
op.accumulate()
EXAMPLE
op.reduceat(a,indices)
>>> a = np.array(
applies op to ranges in the 1-D array a [0,10,20,30,40,50])
defined by the values in indices. >>> indices = np.array([1,4])
The resulting array has the same length >>> np.add.reduceat(a,indices)
array([60, 90])
as indices.
0 10 20 30 40 50
For example:
y = add.reduceat(a, indices) 1 4
[i 1]
indices For multidimensional arrays,
y[i] a[n]
n indices
[i]
reduceat() is always applied along the
last axis (sum of rows for 2-D arrays).
This is different from the default for
reduce() and accumulate().
57
op.outer()
0 1 2 3 4 5 6 7 8
0 1 2
3 4 5
© 2008-2018 Enthought, Inc.
6 7 8 60
dtype * int64
ndim 2
shape 3 3
strides 24 8
data * 0 1 2 3 4 5 6 7 8
0 1 2
3 4 5
© 2008-2018 Enthought, Inc.
6 7 8 61
0 1
a 2 3
b 10 1 2 3
0 1 2
0 1 2 3 4 5
0
0 1 2
a b 1
0 1 2 3 4 5 3 4 5
0 1 0 1 2 0 3
0 1 2 0 3 4 5 1 4
0 a 1 0 3 2 5
a 1 0 1 2 1 4
2
3 4 5 2 5
© 2008-2018 Enthought, Inc. 64
>>> a = np.array([[0,1,2], >>> a = np.arange(6)
... [3,4,5]]) >>> a
array([0, 1, 2, 3, 4, 5])
# Return a new array with a >>> a.shape
# different shape (a view (6,)
# where possible)
>>> a.reshape(3,2) # Reshape array in-place to
array([[0, 1], # 2x3
[2, 3],
[4, 5]]) >>> a.shape = (2,3)
>>> a
# Reshape cannot change the array([[0, 1, 2],
# number of elements in an [3, 4, 5]])
# array
>>> a.reshape(4,2)
ValueError: total size of new
array must be unchanged 0 1 2 3 4 5 0 1 2
0
0 1 2
a 1
0 1 2 3 4 5 3 4 5
# Create a 2D array
>>> a = np.array([[0,1],
... [2,3]])
# Flatten out elements to 1-D
# Flatten out elements to 1D >>> b = a.ravel()
>>> b = a.flatten() >>> b
>>> b array([0,1,2,3])
array([0,1,2,3])
# Changing b does change a
# Changing b does not change a >>> b[0] = 10
>>> b[0] = 10
>>> b
>>> b
array([10,1,2,3]) array([10,1,2,3])
>>> a >>> a
array([[0, 1], b 10 1 2 3
array([[10, 1], b 10 1 2 3
[2, 3]]) [ 2, 3]])
0 1 10 1
a 2 3 a 2 3
© 2008-2018 Enthought, Inc. 66