Professional Documents
Culture Documents
Numpy
Introduction and applications to data
processing
Outline
Introduction to Numpy
Numpy Array Indexing
Numpy Array Operations
Broadcasting
Data Processing
AI VIETNAM
Free AI Course
Introduction
Numpy is a Python library
Year 2020 1
AI VIETNAM
Free AI Course
Introduction
Numpy is a Python library
For scientific computations
Year 2020 2
AI VIETNAM
Free AI Course
Introduction
Numpy array Tensor in
Tensorflow and Pytorch
Year 2020 3
AI VIETNAM
Free AI Course
Introduction
Numpy arrays are multi-dimensional arrays
Axis 1
Axis 0 1 2 1 2 3
1 2 3
Axis 0
3 4 Axis 0 4 5 6
5 6 7 8 9
Axis 2
Axis 1
Year 2020 4
AI VIETNAM
Free AI Course
Introduction
Create Numpy array
From List
arr_np = np.array(python_list)
data 0
data
1
0 1
1 2
data 1
2 3
2
Year 2020 5
AI VIETNAM
Free AI Course
Introduction
Common attributes
dtype: data type
shape: return a tuple of #elements in each dimension
shape=(3,3,2)
ndim: return #dimensions Axis 1 1 2 3
Axis 0 ndim=2
1 2 4 5 6
Axis 0
1 2 3 shape=(3,) shape=(3,2)
ndim=1 3 4 ndim=2 7 8 9
Axis 2
Axis 0 dtype example
5 6 Axis 1
Year 2020 6
AI VIETNAM
Free AI Course
Introduction
Update an element
index 0 1 2
data = 1 2 3
data[0] = 8
data = 8 2 3
Year 2020 7
AI VIETNAM
Free AI Course
Introduction
Create Numpy array
Year 2020 8
AI VIETNAM
Free AI Course
Introduction
Create Numpy array
arange() function eye() function random() function
0 1 2 3 4
arr1 = 0 1 2 3 4
0 1 2
arr2 = 0 2 4
Year 2020 9
AI VIETNAM
Free AI Course
Introduction
Some important functions
flatten() function
where() function
1 2
arr =
arr = 0 1 2 3 4 3 4
arr<3 = T T T F F flatten()
out = 1 2 3 4
out = 0 1 2 6 8
Year 2020 10
AI VIETNAM
Free AI Course
Introduction
Some important functions
reshape() function
data data_rs
1 2 3 1 2
4 5 6 3 4
5 6
Year 2020 11
4
AI VIETNAM
Free AI Course 2020
Outline
Introduction to Numpy
Numpy Array Indexing
Numpy Array Operations
Broadcasting
Data Processing
AI VIETNAM
Free AI Course
Array Indexing
Slicing
arr[for_axis_0, for_axis_1, …]
‘:’: get all the elements
‘a:b’: get the elements from a𝑡ℎ to b𝑡ℎ
Year 2020 12
4
AI VIETNAM
Free AI Course
Array Indexing
Slicing
Year 2020 13
4
AI VIETNAM
Free AI Course
Array Indexing
Slicing
Mutable
0 1 2
1
0
1 2 3 a_arr [0,1]
a_arr = 0 2
1
5 6 7
b_arr = a_arr[:, 1:3]
0 1 0 1
0 2 3 b_arr [0,1]= 99 0 99 3
b_arr =
1 7 1 6 7
6
result
0 1 2
0
1 99 3
a_arr =
1
5 6 7
Year 2020 14
4
AI VIETNAM
Free AI Course
Array Indexing
Get a row
0 1 2
0 1 2 3
arr = 1 5 6 7
2 9 10 11
0 1 2
row_m1 = 5 6 7 shape(3, )
0 1 2
row_m2 = 0 5 6 7 shape(1, 3)
Year 2020 15
4
AI VIETNAM
Free AI Course
Array Indexing
Get a column
0 1 2
0
1 2 3
arr = 1
5 6 7
2 9 10 11
0 1 2
col_m1 = 2 6 10 (3, )
0 2
col_m2 = 1 6 (3, 1)
2 10
Year 2020 16
4
AI VIETNAM
Free AI Course
Array Indexing
Using Lists as indices
0 1
0 1 2
arr = 1 3 4
2 5 6
Year 2020 17
4
AI VIETNAM
Free AI Course
Array Indexing
Boolean indices
0 1 0 1
0 1 2 0 F F
arr = 1 3 4 arr > 2 = 1 T T
2 5 6 2 T T
0 1 2 3
arr[arr>2] = 3 4 5 6
Year 2020 18
4
AI VIETNAM
Free AI Course 2020
Outline
Introduction to Numpy
Numpy Array Indexing
Numpy Array Operations
Broadcasting
Data Processing
AI VIETNAM
Free AI Course
Numpy Array Operations
Addition
1 5 6
2 6 8
+ =
3 7 10
4 8 12
Year 2020 19
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Subtraction
1 5 4
2 6 4
- =
3 7 4
4 8 4
Year 2020 20
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Multiplication
1 5 5
2 6 12
* =
3 7 21
4 8 32
Year 2020 21
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Division
1 5 0.2
2 6 0.33
/ =
3 7 0.42
4 8 0.5
Year 2020 22
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Square root
data result
1 1.0
2 1.4
sqrt(data) =
3 1.7
4 2.0
Year 2020 23
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Inner product
v w
result
1 2
= 8
2 3
Year 2020 24
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Vector-matrix multiplication
X v result
1 2 1 5
=
3 4 2 11
v X result
1 2 1 2 7
=
3 4 10
Year 2020 25
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Matrix-matrix multiplication
X Y result
1 2 2 3 6 5
=
3 4 2 1 14 13
Y X result
2 3 1 2 11 16
=
2 1 3 4 5 8
Year 2020 26
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Transpose
T
1 2 1 3
3 4 2 4
Year 2020 27
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Summation
data
sum(data)
1 2
Axis 0 10
3 4
Axis 1
4 6 3 7
Year 2020 28
4
AI VIETNAM
Free AI Course
Numpy Array Operations
Max and min
data data
1 1
2 .max( ) = 3 2 .min( ) = 1
3 3
1 2 1 2
3 4 .max(axis=0) = 5 6 3 4 .min(axis=0) = 1 2
5 6 5 6
1 2 1 2
3 4 .max(axis=1) = 2 4 6 3 4 .min(axis=1) = 1 3 5
5 6 5 6
Year 2020 29
4
AI VIETNAM
Free AI Course 2020
Outline
Introduction to Numpy
Numpy Array Indexing
Numpy Array Operations
Broadcasting
Data Processing
AI VIETNAM
Free AI Course
Broadcasting
Vector and a scalar
data result
0 1 2 0 1 2
1 2 3
* 2 = 2 4 6
data result
0 1 2 0 1 2
1 2 3 - 2 = -1 0 1
Year 2020 30
4
AI VIETNAM
Free AI Course
Broadcasting
Matrix and vector
X
0 1 2
0
1 2 3
v
1
4 5 6
2 7 8 9
+ 1 0 1
3 10 11 12
Y
0 1 2 0 1 2
0
1 2 3 1 0 1 0
2 2 4
1
4 5 6 1 0 1 1
5 5 7
2 7 8 9
+ 1 0 1
=
2 8 8 10
3 10 11 12 1 0 1 3 11 11 13
Year 2020 31
4
AI VIETNAM
Free AI Course 2020
Outline
Introduction to Numpy
Numpy Array Indexing
Numpy Array Operations
Broadcasting
Data Processing
AI VIETNAM
Free AI Course
Data Processing
Text data
IRIS data
Year 2020 32
4
AI VIETNAM
Free AI Course
Data Processing
Text data
IRIS data
Demo
Year 2020 33
4
AI VIETNAM
Free AI Course
Image Data
Grayscale images
(Height, Width)
Pixel p = scalar Resolution: #pixels
Resolution = HeightxWidth
0 ≤ p ≤ 255
Year 2020 http://jalammar.github.io/visual-numpy/ 34
4
AI VIETNAM
Free AI Course
Image Classification: Image Data
Color images
Trouser
Fashion-MNIST dataset
Pullover
Dress
Grayscale images
Resolution=28x28 Coat
Bag
Ankle
784 Boot
AI VIETNAM
Free AI Course
OpenCV
Introduction
Year 2020 37
4
AI VIETNAM
Free AI Course 2020
Summary
Indexing
Broadcasting data data 0, 1 data 1: 3 data 0: 1 , 0 data : , :
0 1 0 1 0 1 0 1 0 1
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2
1 3 4 1 3 4 1 3 4 1 3 4 1 3 4
2 5 6 2 5 6 2 5 6 2 5 6 2 5 6
Multi-dimension Array
1 2 3
Axis 0 4 5 6
7 8 9
Axis 2
Axis 1
38