Download presentation
Presentation is loading. Please wait.
1
Numpy (Numerical Python)
John R. Woodward
2
Last Lecture Numeric Python. Fast computation n-dimensional arrays
Creating (zero, random, arange) Shape (e.g. (2L, 3L)), dtype (e.g. float64), ndim Operations on arrays (arithmetic, dot product) Slicing, broadcasting import numpy as np Do not forget – this is at the start of each file.
3
Broadcasting - array arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
print arr arr[3:8] = -99 OUTPUT [ ] [ ]
4
Python list list = [0, 1, 2, 3, 4, 5, 6, 7, 8] print list list[3] = -99 list[3:8] = -99
5
Python list list = [0, 1, 2, 3, 4, 5, 6, 7, 8] print list
Output [0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, -99, 4, 5, 6, 7, 8] Traceback (most recent call last): File "F:\Previous\Users\jrw\workspace\Python1\slice2.py", line 14, in <module> TypeError: can only assign an iterable
6
Fast element-wise array functions
arr = np.arange(5) print arr print np.sqrt(arr) print np.exp(arr) [ ] [ ] [ ]
7
element-wise maximum x = randn(4) y = randn(4) print x print y
print np.maximum(x, y) [ ] [ ] [ ]
8
element-wise add x = randn(4) y = randn(4) print x print y
print np.add(x, y) [ ] [ ] [ ]
9
Zip two lists together - python
a = [1,2,3] b = [10, 20, 30] zipAB = zip(a,b) print zipAB OUTPUT [(1, 10), (2, 20), (3, 30)]
10
Zip three lists together
a = [1,2,3] b = [10, 20, 30] c = [True, False, True] zipABC = zip(a,b,c) print zipABC Output [(1, 10, True), (2, 20, False), (3, 30, True)]
11
And is the same as a = [1,2,3] b = [10, 20, 30]
c = [True, False, True] result = [(x,y,z) for x, y, z in zip(a,b,c)] print result Output [(1, 10, True), (2, 20, False), (3, 30, True)]
12
conditionals result = [(x if z else y) for x, y, z in zip(a,b,c)]
print result OUTPUT [1, 20, 3] NOTE depending on the boolean value, it decides which list to take value from.
13
where an easier way to do this with np a = [1,2,3] b = [10, 20, 30]
c = [True, False, True] np.where(c,a,b) Output is [ ]
14
types <type 'list'> <type 'numpy.ndarray'>
result = [(x if z else y) for x, y, z in zip(a,b,c)] print type(result) result = np.where(c,a,b) <type 'list'> <type 'numpy.ndarray'>
15
where(arr > 0, 2, -2) #creates random 3 by 2 array
arr = np.random.rand(3,2) print arr print np.where(arr > 0.5, 2, -2) [[-2 2] [ 2 2] [-2 -2]] [[ ] [ ] [ ]]
16
where(arr > 0, 2, arr) arr = np.random.rand(3, 2) print arr
print np.where(arr > 0.5, 2, arr) [[ ] [ ] [ ]] [[ ] [ ] [ ]]
17
Mathematical and statistical methods
arr = np.random.randn(2, 2) print arr print arr.mean() print np.mean(arr) print arr.sum() [[ ] [ ]]
18
Axis An array has an axis. These are labelled 0, 1, 2, …
Usually we think of these as x y z axiz, but we soon run out of letters. What comes after row, column, ??? These are just the dimensions.
19
Mean of rows/columns (axis)
arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) print arr print arr.mean(axis=0) print arr.mean(axis=1) #these are the #”means in different #directions” [[0 1 2] [3 4 5] [6 7 8]] [ ] [ ]
20
Sum different axis arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
print arr print arr.sum(0) print arr.sum(1) #we sum #vertically #horizonatally [[0 1 2] [3 4 5] [6 7 8]] [ ] [ ]
21
Cumulative sum (running total)
[[0 1 2] [3 4 5] [6 7 8]] [[ ] [ ] [ ]] [[ ] [ ] [ ]] arr = np.array( [[0, 1, 2], [3, 4, 5], [6, 7, 8]]) print arr print arr.cumsum(0) print arr.cumsum(1) This is across different axis.
22
Cumulative product “running product”
[[0 1 2] [3 4 5] [6 7 8]] [[ ] [ ] [ ]] [[ ] [ ] [ ]] arr = np.array( [[0, 1, 2], [3, 4, 5], [6, 7, 8]]) print arr print arr.cumsum(0) print arr.cumsum(1) This is across different axis.
23
Methods for Boolean arrays
arr = np.random.randn(5) print arr print (arr > 0).sum() 1/ makes a random array 2/ counts only the positive entries. [ ] 2
24
Methods for Boolean arrays
bools = np.array( [False, False, True, False]) print bools.any() print bools.all() This asks if all/any in bools is true. output True False
25
Sorting 1 arr = randn(4) print arr arr.sort() OUTPUT
[ ] [ ]
26
SORTING 2 [[1 3 2] [8 4 9] [3 5 8]] [3 4 8] [8 5 9]] [[1 2 3] [5 8 9]]
arr = randn(2, 3) print arr arr.sort(0) arr.sort(1) sorting on different axis
27
Unique and other set logic
names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe']) print np.unique(names) print sorted(set(names)) OUTPUT ['Bob' 'Joe' 'Will'] ['Bob', 'Joe', 'Will']
28
Testing Membership Test if something is in an array.
values = np.array([6, 0, 0, 3, 2, 5, 6]) print np.in1d(values, [2, 3, 6]) Output [ True False False True True False True] #asks is 6 in [2,3,6], is 0 in [2,3,6], ....
29
Storing arrays on disk in binary format
Lets save and load a file arr = np.arange(10) print arr np.save('some_array', arr) arr1 = np.load('some_array.npy') print arr1 NOTE THE file extension is npy
30
Saving multiple arrays
arr3 = np.arange(3) arr5 = np.arange(5) np.savez('array_archive.npz', a=arr3, b=arr5) arch = np.load('array_archive.npz') print type(arch) print arch['a'] print arch['b'] print dict(arch)
31
arr3 = np.arange(3) arr5 = np.arange(5) np.savez('array_archive.npz', a=arr3, b=arr5) arch = np.load('array_archive.npz') print type(arch) print arch['a'] print arch['b'] print dict(arch)
32
np.load returns dict-like object
<class 'numpy.lib.npyio.NpzFile'> [0 1 2] [ ] {'a': array([0, 1, 2]), 'b': array([0, 1, 2, 3, 4])}
33
Saving and loading text files
arr = np.loadtxt('array_ex.txt', delimiter=',') print arr print type(arr) OUTPUT [[ ] [ ]] <type 'numpy.ndarray'> array_ex.txt 1,2,3,4 3,4,5,6
34
Indexing elements in a NumPy array
Axis 0 is rows Axis 1 is colmuns. Axis 3 is ???
35
Indexing elements in a NumPy array
36
Two- dimensional array slicing
37
Ask questions on board first.
38
3d 2x2x2 a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3
39
Indexing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[0]
40
Indexing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[1]
41
Indexing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[0][0]
42
Indexing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[0][0][0]
43
Indexing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[0][1]
44
Indexing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[1][0]
45
Slicing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[0,0,0]
46
Slicing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[0,1,0]
47
Slicing a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[:,0]
48
a[:,:,0] Slicing 2,4 3,1 3,3 4,3 Both slices, both rows, column 0
a=np.array([ [ [3, 1],[4, 3] ], [2, 4],[3, 3] ] ]) 2,4 3,3 3,1 4,3 a[:,:,0] Both slices, both rows, column 0
49
Slicing Remember, slicing range: a:b Means elements from a up to b-1
50
Data Types Every element in an ndarray has the same type Basic types:
int float complex bool object string unicode
51
Data Types Types also have a defined size in bytes, e.g.
int32 float64 The size defines storage size and accuracy To set the type: a=np.array([1,2], dtype=np.int32) print a.dtype OUTPUT IS dtype('int32')
52
Iterating and Processing
You can iterate through a ndarray if you like: for e in a: print e or for e in a[0]: etc. but this is complicated an not advised There is a better way ...
53
Element-wise Operations
a=a*2 a=a+5 a=a+b etc. Functions: a.sum() a.max() a.mean() a.round()
54
Slices and Indexes a[0]=a[0]/2 a[0,0,0]+=1 a[:,1,1].sum()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.