Data Science Lab: Numpy: Numerical Python
Data Science Lab: Numpy: Numerical Python
2
Introduction to Numpy
▪ Summary
▪ Numpy and computation efficiency
▪ Numpy arrays
▪ Computation with Numpy arrays
▪ Broadcasting
▪ Accessing Numpy arrays
▪ Working with arrays, other functionalities
3
Introduction to Numpy
4
Introduction to Numpy
5
Introduction to Numpy
Python List
PyObject
header (list size,
attributes) header (object type,
reference count, size)
0x568900
value: 0.86
0x568948
0x568980
PyObject
0x5689f0
header (object type,
reference count, size)
value: 'a'
6
Introduction to Numpy
NumpyArray
0.67
header (list size,
attributes) 0.45
data 0.33
7
Introduction to Numpy
8
Multidimensional arrays
x1
9
Multidimensional arrays
3D array
13 14 15 list3 = [[[1,2,3], [4,5,6]],
7 8 9
[[7,8,9], [10,11,12]],
1 2 3 18
12 [13,14,15], [16,17,18]]]
4 5 6
10
Multidimensional arrays
11
Multidimensional arrays
x0 x1
12
Multidimensional arrays
13
Multidimensional arrays
x0 x1
15
Numpy arrays
Out[2]:
[[1.1],
[1.1]]
17
Numpy arrays
18
Numpy arrays
19
Computation on Numpy
Summary:
▪ Universal functions (Ufuncs):
▪ Binary operations (+,-,*,...)
▪ Unary operations (exp(),abs(),...)
▪ Aggregate functions
▪ Sorting
▪ Algebraic operations (dot product, inner product)
20
Computation on Numpy
21
Computation on Numpy
▪ Example:
In [1]: x=np.array([[1,1],[2,2]])
y=np.array([[3, 4],[6, 5]])
x*y
1 1 3 4 1*3 1*4 3 4
* = =
2 2 6 5 2*6 2*5 12 10
22
Computation on Numpy
23
Computation on Numpy
▪ Example:
In [1]: x=np.array([[1,1],[2,2]])
np.exp(x)
1 1 e^1 e^1
2 2 e^2 e^2
24
Computation on Numpy
▪ Aggregate functions
▪ Return a single value from an array
▪ np.min(x), np.max(x), np.mean(x), np.std(x), np.sum(x)
▪ np.argmin(x), np.argmax(x)
▪ Or equivalently:
▪ x.min(), x.max() x.mean(), x.std(), x.sum()
▪ x.argmin(), x.argmax()
▪ Example
In [1]: x=np.array([[1,1],[2,2]])
x.sum()
Out[1]: 6
25
Computation on Numpy
In [1]: x=np.array([[1,7],[2,4]])
1 7 0
x.argmax(axis=0) 2 4 1
2 4
Out[2]: [8, 6] (sum the elements of each row)
26
Computation on Numpy
arr.min(axis=1)
shape = (3, 1, 3) shape = (3, 3)
13 14 15
7 8 9 [[[1,2,3]], [[1, 2, 3],
1 2 3 18 [[7,8,9]], [7, 8, 9],
12 [[13,14,15]]] [13, 14, 15]]
4 5 6
27
Computation on Numpy
▪ Sorting
▪ np.sort(x): creates a sorted copy of x
▪ x is not modified
▪ x.sort(): sorts x inplace (x is modified)
28
Computation on Numpy
▪ Sorting
▪ Array is sorted along the last axis (-1) by default
In [1]: x = np.array([[2,1,3],[7,9,8]])
np.sort(x) # Sort along rows (axis -1)
Out[1]: [[1,2,3],[7,8,9]]
axis -1
2 1 3 1 2 3
7 9 8 7 8 9
29
Computation on Numpy
▪ Sorting
▪ Allows specifying the axis being sorted
In [1]: x = np.array([[2,7,3],[7,2,1]])
np.sort(x, axis=0) # Sort along columns
Out[1]: [[2,2,1],
[7,7,3]]
2 7 3 2 2 1
axis 0
7 2 1 7 7 3
30
Computation on Numpy
▪ Sorting
▪ np.argsort(x): return the position of the indices of the
sorted array (sorts by default on axis -1)
In [1]: x = np.array([[2,1,3],[7,9,8]])
np.argsort(x) # Sort along rows (axis -1)
Out[1]: [[1,0,2],[0,2,1]]
array values array indices (by row, axis -1) sorted indices
2 1 3 0 1 2 1 0 2
7 9 8 0 1 2 0 2 1
31
Computation on Numpy
▪ Algebraic operations
▪ np.dot(x, y)
▪ inner product if x and y are two 1-D arrays
1 2 3 * 0 = 7
2
Out[1]: 7
32
Computation on Numpy
▪ Algebraic operations
▪ np.dot(x, y)
▪ matrix multiplied by vector
1 1 2 5
* =
2 2 3 10
In [1]: x=np.array([[1,1],[2,2]])
y=np.array([2, 3]) # works even if y is a row vector
np.dot(x, y)
33
Computation on Numpy
▪ Algebraic operations
▪ np.dot(x, y)
▪ matrix multiplied by matrix
1 1 2 2 3 3
* =
2 2 1 1 6 6
In [1]: x=np.array([[1,1],[2,2]])
y=np.array([[2,2],[1,1]])
np.dot(x, y)
Out[1]: [[3,3],[6,6]]
34
Notebook Examples
▪ 2-Numpy Examples.ipynb
▪ 1) Computation with arrays
35
Broadcasting
1 2 1 2 1 2
b) + 1 2 +
3 4 3 4 1 2
1 2 [1] 1 2 1 1
c) + +
3 4 [2] 3 4 2 2
d) 1 2 [1] 1 2 1 1
+ +
[2] 1 2 2 2
36
Broadcasting
▪ Rules of broadcasting
1. The shape of the array with fewer dimensions is
padded with leading ones
x.shape = (2, 3), y.shape = (3) y.shape = (1, 3)
▪ Example: compute x + y
▪ x = np.array([[1, 2],[3,4],[5,6]]) x.shape = (3, 2)
▪ y = np.array([11, 12, 13],) y.shape = (3,)
▪ z=x+y
▪ Apply Rule 1
▪ y.shape becomes (1, 3): y=[[11,12,13]]
11 12 13
▪ Apply Rule 3 1 2
▪ shapes (3, 2) and (1, 3) are incompatibles 3 4
39
Notebook Examples
▪ 2-Numpy Examples.ipynb
▪ 2) Broadcasting: dataset
normalization
40
Accessing Numpy Arrays
Out[1]: el = 7
42
Accessing Numpy Arrays
43
Accessing Numpy Arrays
44
Accessing Numpy Arrays
▪ Select the first two rows and the first and third
columns
In [2]: x[:2, ::2] # or x[0:2, 0:3:2] 1 2 3
4 5 6
45
Accessing Numpy Arrays
In [1]: x = np.array([[1,2,3],[4,5,6],[7,8,9]])
x[:, 1:] = 0
print(x)
46
Accessing Numpy Arrays
▪ Update a view
In [1]: x = np.array([[1,2,3],[4,5,6],[7,8,9]])
view = x[:,1:]
view[:,:] = 0
print(x)
47
Accessing Numpy Arrays
48
Accessing Numpy Arrays
▪ Mask creation
▪ x op value (e.g x==4)
▪ where op can be >, >=, <, <=, ==, !=
▪ Examples
In [1]: x = np.array([1.2, 4.1, 1.5, 4.5])
x > 4
50
Accessing Numpy Arrays
▪ Masking examples
▪ Even if the shape of x2 is (2, 2), the result
is a one-dimensional array containing the
elements that satisfy the condition
In [1]: x = np.array([1.2, 4.1, 1.5, 4.5])
x[x > 4]
51
Accessing Numpy Arrays
52
Accessing Numpy Arrays
53
Accessing Numpy Arrays
x[1] x[3]
54
Accessing Numpy Arrays
55
Accessing Numpy Arrays
56
Accessing Numpy Arrays
▪ Combined indexing:
▪ Allows mixing the indexing types described so far
▪ Important rule:
▪ The number of dimensions of selected data is:
• The same as the input if you mix:
• masking+slicing, fancy+slicing
• Reduced by one if you use simple indexing in one axis
• Because simple indexing takes only 1 single
element from an axis
58
Accessing Numpy Arrays
59
Accessing Numpy Arrays
60
Accessing Numpy Arrays
arr[:,0,:]
▪ 2-Numpy Examples.ipynb
▪ 3) Accessing Numpy
Arrays
62
Working with arrays
Summary:
▪ Array concatenation
▪ Array splitting
▪ Array reshaping
▪ Adding new dimensions
63
Working with arrays
In [1]: x = np.array([[1,2,3],[4,5,6]])
y = np.array([[11,12,13],[14,15,16]])
np.concatenate((x, y)) # Default axis: 0
Out[1]: [[1,2,3],[4,5,6],[11,12,13],[14,15,16]]
64
Working with arrays
4 5 6 14 15 16
axis 1
In [1]: x = np.array([[1,2,3],[4,5,6]])
y = np.array([[11,12,13],[14,15,16]])
np.concatenate((x, y), axis=1)
Out[1]: [[1,2,3,11,12,13],[4,5,6,14,15,16]]
65
Working with arrays
4 5 6 14 15 16 4 5 6
11 12 13
hstack
14 15 16
In [1]: x = np.array([[1,2,3],[4,5,6]])
y = np.array([[11,12,13],[14,15,16]])
h = np.hstack((x, y)) # along rows (horizontal)
v = np.vstack((x, y)) # along columns (vertical)
66
Working with arrays
1 2 3 x
new axis
11 12 13 y
In [1]: x = np.array([1,2,3])
y = np.array([11,12,13])
v = np.vstack((x, y)) # vertically
67
Working with arrays
68
Working with arrays
69
Working with arrays
▪ Reshaping arrays
In [1]: x = np.arange(6)
y = x.reshape((2,3))
0 1 2
0 1 2 3 4 5
3 4 5
70
Working with arrays
Out[1]: [[[1,2,3],[4,5,6]]]
0 1 2 0 1 2
x0 x1
3 4 5 3 4 5 x0 (depth)
x1 x2
71