02 Numpy Introduction
02 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
A image is a 3D-
tensor
4D Tensor Example
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
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