More on Numpy
More on Numpy
2
Course Outcomes
CO Title Level
Number
3
THEORY
INTRODUCTION - Numpy
Numpy, Scipy and Matplotlib provide MATLAB-like functionality in
python.
Numpy Features:
Typed multi-dimentional arrays (matrices)
Fast numerical computations (matrix math)
High-level math functions
4
1. Need of Numpy
Python does numerical computations slowly.
1000 x 1000 matrix multiply
Python triple loop takes > 10 min.
Numpy takes ~0.03 seconds
5
2. NumPy Overview
1. Arrays
2. Shaping and transposition
3. Mathematical Operations
4. Indexing and slicing
5. Broadcasting
6
3. Arrays
Structured lists of numbers:-
Vectors
Matrices
Images
Tensors
ConvNets
7
Arrays
Structured lists of numbers.
• Vectors
• Matrices
• Images
• Tensors
• ConvNets
8
Arrays
Structured lists of numbers.
• Vectors
• Matrices
• Images
• Tensors
• ConvNets
9
Arrays
12
B. Arrays Creation
np.ones, np.zeros
np.arange
np.concatenate
np.astype
np.zeros_like, np.ones_like
np.random.random
13
Arrays Creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
14
Arrays Creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
15
Arrays Creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
16
Arrays Creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
17
Arrays Creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
18
Arrays Creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
19
Arrays Creation
• np.ones, np.zeros
• np.arange
• np.concatenate
• np.astype
• np.zeros_like, np.ones_like
• np.random.random
20
C. Arrays, danger zone
21
D. Shaping
a = np.array([1,2,3,4,5,6])
a = a.reshape(3,2)
a = a.reshape(2,-1)
a = a.ravel()
1. Total number of elements cannot change.
2. Use -1 to infer axis shape
3. Row-major by default (MATLAB is column-major)
22
E. Return values
23
F. Transposition
a = np.arange(10).reshape(5,2)
a = a.T
a = a.transpose((1,0))
24
G. Saving and loading arrays
np.savez(‘data.npz’, a=a)
data = np.load(‘data.npz’)
a = data[‘a’]
25
H. Image arrays
Images are 3D arrays: width, height, and channels
Common image formats:
height x width x RGB (band-interleaved)
height x width (band-sequential)
Gotchas:
Channels may also be BGR (OpenCV does this)
May be [width x height], not [height x width]
26
I. Saving and Loading Images
SciPy: skimage.io.imread,skimage.io.imsave
height x width x RGB
PIL / Pillow: PIL.Image.open, Image.save
width x height x RGB
OpenCV: cv2.imread, cv2.imwrite
height x width x BGR
27
RECAP
We just saw how to create arrays, reshape them, and permute axes
Questions so far?
28
RECAP
We just saw how to create arrays, reshape them, and permute axes
Questions so far?
29
3. Mathematical operators
30
Mathematical operators
31
Mathematical operators
32
Mathematical operators
33
A. Math, upcasting
Just as in Python and Java, the result of a math operator is cast to the
more general or precise datatype.
uint64 + uint16 => uint64
float32 / int32 => float32
34
B. Math, universal functions
Also called ufuncs, Element-wise
Examples:
• np.exp
• np.sqrt
• np.sin
• np.cos
• np.isnan
35
Math, universal functions
36
Math, universal functions
37
C. Indexing
38
D. Indexing, slices and arrays
39
E. Python Slicing
Syntax: start:stop:step
a = list(range(10))
a[:3] # indices 0, 1, 2
a[-3:] # indices 7, 8, 9
a[3:8:2] # indices 3, 5, 7
a[4:1:-1] # indices 4, 3, 2 (this one is tricky)
40
F. Axes
41
G. Broadcasting
a = a + 1 # add one to every element
42
Broadcasting example
43
H. Broadcasting failures
If a.shape is 100, 200, 3 but b.shape is 4 then a + b will fail. The trailing
dimensions must have the same shape (or be 1)
44
Tips to avoid bugs
45
Learning Outcomes
On completion of the course students will be able to understand
47
Summary
• Nearly every scientist working in Python draws on the power of
NumPy.
• NumPy brings the computational power of languages like C and
Fortran to Python, a language much easier to learn and use. With this
power comes simplicity: a solution in NumPy is often clear and
elegant.
48
References
1.https://towardsdatascience.com/detecting-cute-animals-with-machin
e-learning-d39a511bd144?gi=e6a385d621ba
2.https://machinelearningmastery.com/how-to-develop-a-face-recognit
ion-system-using-facenet-in-keras-and-an-svm-classifier/
49
THANK YOU
For queries
Email: [email protected]
50