Numpy Introduction
講者:Isaac
Outline
• What’s Numpy
• Common concept in Numpy
• Advance topic in Numpy
2
What’s Numpy
3
What’s Numpy
• a library for the Python that support for large, multi-dimensional
arrays/matrices
• http://www.numpy.org/
• core functionality of NumPy is its "ndarray“ data structure
• for n-dimensional array
• all elements of a single array must be of the same type
4
N-dimensional array
5
What’s Tensor
• What is Tensor?
• Tensor = multidimensional array
6
What’s Tensor
7
3D Tensor Example
• Each image contain many pixels
• Each pixels compose red, green, blue(RGB)
• Each channel have brightness levels between 0~255
3D Tensor Example
3D Tensor Example
An RGB image = 3D
tensor
3D Tensor Example
= [image width, image height, image channel]
A image is a 3D-
tensor
4D Tensor Example
= [batch size, image width, image height, image
channel]
A batch of images is a 4D-
tensor
Common concept in Numpy
13
Why numpy
http://machinelearningexp.com/data-science-
performance-of-python-vs-pandas-vs-numpy/
14
Why numpy
15
Data type in Numpy
16
What’s axis?
17
Numpy array creation
18
Numpy array creation
19
Numpy array reshape
20
Numpy array indexing/slicing
21
Numpy array indexing/slicing
22
Numpy array indexing/slicing
23
Numpy array broadcasting
24
Numpy array broadcasting
25
Rules of broadcasting
• Rule 1
• If the two arrays differ in their number of dimensions, the shape of the one with fewer
dimensions is padded with ones on its leading (left) side.
• Rule 2
• If the shape of the two arrays does not match in any dimension, the array with shape equal to 1
in that dimension is stretched to match the other shape.
• Rule 3
• If in any dimension the sizes disagree and neither is equal to 1, an error is raised.
26
Numpy array operations
27
Numpy array operations
28
Numpy array operations
ndarray.sum(axis = 0) -> array([ 12,ndarray.sum(axis
15, 18]) = 1) -> array([ 6, 15, 24])
29
Most popular Numpy functions
30
Numpy V.S. TensorFlow
31
Cool Numpy animation
• http://groups.csail.mit.edu/mac/projects/amorphous/GrayScott/
• video/fingerprint.mp4
32
Example and practice
• Example
• example/numpy_example
• Practice
• practice/numpy_practice1
33
Advance topic in Numpy
34
Copy and view
• while executing the functions
• some of them return a copy of the input array, while some return the view
• return contents are physically stored in another location
• it is called copy
• return contents are in the same memory as origin contents
• it is called it as view
• view is faster than copy
35
High performance numpy
• reference
• https://zhuanlan.zhihu.com/p/28626431
• https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/4-1-speed-up-
numpy/
• https://www.slideshare.net/skarl86/numpy-tutorialfinal-20160303
36
Example and practice
• Example
• example/numpy_example
• Practice
• practice/numpy_practice2
37