Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Numpy
Introduction and applications to data
processing (draft version)
Quang-Vinh Dinh
Ph.D. in Computer Science
Year 2020
AI VIETNAM
Free AI Course 2020
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 is a Python library
For scientific computations
Numpy array Tensor in
Tensorflow and Pytorch
Year 2020 3
AI VIETNAM
Free AI Course
Introduction
Numpy is a Python library
For scientific computations
Numpy array Tensor in Tensorflow and Pytorch
Numpy arrays are multi-dimensional arrays
Axis 1
1 2 1 2 3
1 2 3 Axis 0
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
data
1
0 1
1 2
data
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
1 2 3 shape=(3,) Axis 0
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 = 0 1 2 6 8 out = 1 2 3 4
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
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 to
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 6 7 1 6 7
result
0 1 2
0
1 99 3
a_arr =
1
5 6 7
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)
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
0 1
0 1 2
arr = 1 3 4
2 5 6
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
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
1 5 4
2 6 4
- =
3 7 4
4 8 4
1 5 5
2 6 12
* =
3 7 21
4 8 32
1 5 0.2
2 6 0.33
/ =
3 7 0.42
4 8 0.5
data result
1 1.0
2 1.4
sqrt(data) =
3 1.7
4 2.0
v w
result
1 2
= 8
2 3
X v result
1 2 1 5
=
3 4 2 11
v X result
1 2 1 2 7
=
3 4 10
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
T
1 2 1 3
3 4 2 4
data
sum(data)
1 2
Axis 0 10
3 4
Axis 1
4 6 3 7
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 294
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
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 314
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
Demo
(Height, Width)
Pixel p = scalar Resolution: #pixels
Resolution = HeightxWidth
0 ≤ p ≤255
Year 2020 274
AI VIETNAM
Free AI Course
Image Classification: Image Data
Color images
Trouser
Pullover
Fashion-MNIST dataset
Dress
Resolution=28x28 Sandal
Bag
Ankle
Year 2020 Boot
AI VIETNAM
Free AI Course
Image Classification
Fashion-MNIST data
Download data
28
28
784 31
AI VIETNAM
Free AI Course
Data Processing
Images in binary
Demo
Demo
Summary
Indexing
Broadcasting data data data data 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
36