AIVN Numpy
AIVN Numpy
Quang-Vinh Dinh
PhD in Computer Science
Year 2024
Objectives
Indexing Broadcasting Examples
Axis 1 data
Feature Label
0 1 2
Axis 0 1 2
1 2 3 2
3 4 shape=(3,2)
*
0 1 2
5 6
= 2 4 6
result
Introduction Axis 1
Axis 0 1 2
3 4 shape=(3,2)
SECTION 2 5 6
Common Functions
SECTION 3
Examples Axis 1
Introduction Section 1 Page 1
❖ Numpy is a Python library
❖ For scientific computations
Introduction Section 1 Page 2
❖ Data type for common libraries
Introduction arr_np = np.array(python_list)
❖ Numpy arrays are multi-dimensional arrays
Axis 1
shape=(3,) 1 2 3 shape=(3,3,2)
Axis 0 1 2 4 5 6
1 2 3 Axis 0
Axis 0
3 4 shape=(3,2) 7 8 9
Axis 2
5 6
Axis 1
2 5
1 2 3 4
2 5 10 17
Motivation Section 1 Page 5
❖ Why do I need Numpy?
2 5
1 2 3 4
2 5 10 17
Motivation Section 1 Page 6
❖ Why do I need Numpy?
Implementation view
1 2 3 4 implement for each element
2 4 Execution view
2 5 10 17 run for all the elements
Outline
SECTION 1
Introduction data
0 1 2
4 2 4
SECTION 2
2 2 1
Common Functions
SECTION 3
Examples
Some Common Functions Section 2 Page 7
❖ Create Numpy arrays
arr1 = 0 1 2 3 4
0 1 2
arr2 = 0 2 4
Some Common Functions Section 2 Page 9
❖ reshape function
reshape() function
data data_rs
1 2 3 1 2
4 5 6 3 4
5 6
Some Common Functions Section 2 Page 10
❖ reshape and flatten functions
data
0 1 2
4 2 4
2 2 1
4 2 4 2 2 1
Some Common Functions
Section 2 Page 11
❖ reshape and flatten functions
repeat(data, repeats, axis)
data repeat_axis_0
4 3 4 3
7 1 4 3
7 1
7 1
repeat_axis_1
4 4 3 3
7 7 1 1
Outline
SECTION 1
Introduction 0
data
1 2
1 2 3 2
*
SECTION 2 0 1 2
SECTION 3
* * * = 2 4 6
SECTION 4
2 2 2
Examples
Array Indexing Section 3 Page 12
❖ Slicing
arr[for_axis_0, for_axis_1, …]
‘:’: get all the elements
‘a:b’: get the elements from a𝑡ℎ to (b𝑡ℎ −1)
data 0: 1 , 0 data : , :
0 1 0 1
0 1 2 0 1 2
1 3 4 1 3 4
2 5 6 2 5 6
Array Indexing Section 3 Page 13
❖ 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,0]= 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
Array Indexing Section 3 Page 14
❖ 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)
Array Indexing Section 3 Page 15
❖ 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
Array Indexing Section 3 Page 16
❖ Using Lists as indices
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
Numpy Array Operations Section 3 Page 18
❖ Summation
data
sum(data)
1 2
Axis 0 10
3 4
Axis 1
4 6 3 7
Numpy Array Operations Section 3 Page 19
❖ 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
Broadcasting Section 3 Page 20
❖ 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
Broadcasting Section 3 Page 21
❖ Vector and a scalar
data result
0 1 2 0 1 2
1 2 3
* 2 = 2 4 6
1 2 3
* * * = 2 4 6
2 2 2
Broadcasting Section 3 Page 22
❖ Matrix and vector
1
X Y
2
0 1 2 0 1 2
0 3
1 2 3 2 2 4
v
4 5 1
6 5 5 7
7 8 9
+ 1 0 1 =
2 8 8 10
10 11 12 3 11 11 13
Broadcasting
Section 3 Page 23
❖ 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
Example 1: Softmax function Section 3 Page 24
❖ Chuyển các giá trị của một vector thành các giá trị xác suất
𝑒𝑥
𝑥 ∈ (−∞, 0)
𝑒 𝑥 ∈ (0, 1)
𝑥
Outline
SECTION 1
SECTION 2
Common Functions
SECTION 3 One-hot encoding for label
Indexing and Broadcasting 𝑦 = 0 → 𝒚 = [1 0]
𝑦 = 1 → 𝒚 = [0 1]
SECTION 4 vector
scalar
Examples
Example 2
Section 4 Page 25
❖ Weather forecasting
1 2 5 1 4 3 4 2 0 7 4 1 2 5 5 9 3 6 0 3 4 9 9 4 7 4 5 5 6 6 2 4 9 5 3 1
Reshape(6, 6)
1 2 5 1 4 3
4 2 0 7 4 1
2 5 5 9 3 6
Re-organize data so that temps for
each day are in a row 0 3 4 9 9 4
7 4 5 5 6 6
2 4 9 5 3 1
Reshape an array
Example 2 Section 4 Page 28
❖ Daily average temperatures
Example 3 Section 4 Page 29
❖ One-hot Encoding
Feature Label
Feature Label
𝑦 = 0 → 𝒚 = [1 0] 𝑦 = 0 → 𝒚 = [1 0 0]
𝑦 = 1 → 𝒚 = [0 1] 𝑦 = 1 → 𝒚 = [0 1 0]
𝑦 = 2 → 𝒚 = [0 0 1]
scalar vector
One-hot Encoding
Feature Label
#classes=3
#samples=9
np.genfromtxt(fname=‘…’,
dtype=float, delimiter=‘,’,
skip_header=1,
usecols=[0, 1, 2, 3])
np.genfromtxt(fname=‘…’,
dtype=float, delimiter=‘,’,
skip_header=1,
usecols=4)
Example 4 Section 4 Page 33
❖ Some functions
np.unique(data) = 1 2 3
Example 4 Section 4 Page 34
❖ Some functions
data = ‘class-1’ ‘class-2’ ‘class-3’
Quang-Vinh Dinh
PhD in Computer Science
Year 2024
Objectives
OpenCV and Numpy Image Loading Other Examples
0
samples 255
in Numpy
B
G
R
Outline
SECTION 1
SECTION 2
Image Loading
SECTION 3
Brightness Changes
B
SECTION 4 G
Color Conversion R
Image Data
SECTION 1 PAGE 1
v Grayscale images
Height
Width
v Color images
𝑟
Pixel p= 𝑔
𝑏
0 ≤ r,g,b ≤ 255
Resolution: #pixels
https://textbooks.math.gatech.edu/ila/ila.pdf
❑ RGB: Đây là cách phổ biến nhất để biểu diễn màu sắc trong hình ảnh. Mỗi hình ảnh được biểu diễn bằng ba
kênh màu - đỏ (Red), xanh lục (Green), và xanh lam (Blue). Mỗi kênh này có giá trị từ 0 đến 255.
R G B Color
R 255 0 0
0 0 0 Black
G 0 255 0 255 255 0 Yellow
128 128 128 Grey
B 0 0 255 86 180 233 Sky Blue
… … … …
B
G
R
Data Processing SECTION 1 PAGE 4
v Images in files
width
height
OpenCV and Numpy SECTION 1 PAGE 5
v Read an image
image1.png
image2.png
OpenCV and Numpy SECTION 1 PAGE 6
v Read an image
image2.png
OpenCV and Numpy SECTION 1 PAGE 7
v Read an image
B
❑ Đọc và hiển thị ảnh với OpenCV
Thư viện OpenCV sử dụng kênh màu BGR khi đọc ảnh.
B G R
❑ Đọc ảnh với OpenCV và hiển thị bằng Matplotlib
Input
B
G
R
Màu sắc hình ảnh sau khi hiển thị bằng thư viện Matplotlib
không đúng, tại sao? Output
OpenCV and Numpy SECTION 1 PAGE 10
v how?
R B
G G
B R
image1.png
❑ Đọc ảnh với OpenCV và hiển thị bằng Matplotlib
Input
Thư viện matplotlib sử dụng kênh màu RGB, nên sau khi
đọc hình ảnh bằng cv2.imread(), chúng ta cần chuyển đổi kênh
màu sang dạng RGB.
Output
Outline
SECTION 1 samples
SECTION 2
Image Loading
SECTION 3
Brightness Changes
SECTION 4
Color Conversion
Image File Loading SECTION 2 PAGE 13
v Cat-Dog dataset
cats_and_dogs
cat images
train
dog images
SECTION 2
Image Loading
in Numpy
SECTION 3
Brightness Changes
0 1 2 … 255
SECTION 4
What we think
Color Conversion
Brightness Changes SECTION 3 PAGE 21
v Data type
uint8 data type
in Numpy
0 1 2 … 255
What we think
Brightness Changes SECTION 3 PAGE 23
v clip() and where() functions
where() function
numpy.clip() 0 1 2 3
arr = 4
arr<3 = T T T F F
out = 0 1 2 6 8
Brightness Changes SECTION 3 PAGE 24
v Given two images
Grayscale Image
(Height, Width)
Color Image
(Height, Width, Channel)
Brightness Changes
SECTION 3 PAGE 25
v Load an image
(Height, Width)
img = cv2.imread(path, 0)
img = cv2.imread(path, 1)
Idea
For each pixel
Increase pixel value
by a value v
Increase
brightness
I=I+v
I = clip(I)
Brightness Changes
SECTION 3 PAGE 28
v Decrease the brightness
of a grayscale image
Idea
For each pixel in each channel
Decrease pixel value
by a value v
Decrease
brightness
I=I-v
I = clip(I)
16
Brightness Changes
SECTION 3 PAGE 29
v Why?
0
255
in Numpy
Brightness Changes
SECTION 3 PAGE 30
v Why?
0
255
in Numpy
Brightness Changes
SECTION 3 PAGE 31
v Solution
SECTION 2
Image Loading
SECTION 3
Brightness Changes
SECTION 4
Color Conversion
Color to Grayscale Conversion SECTION 4 PAGE 36
v apply_along_axis() function
np.max np.max np.max np.max
2 6 0 1
3 0 8 3 2 6 0 1
data
4 4 5 0 3 0 8 3
2 0 4 6 4 4 5 0
2 0 4 6
2 6 0 1
3 0 8 3
data
4 4 5 0
2 0 4 6
np.apply_along_axis(np.max,
axis=0,
arr=data)
result0 4 6 8 6
Color to Grayscale Conversion SECTION 4 PAGE 37
v apply_along_axis() function
np.max np.max
2 6 0 1 4 4 5 0
2 6 0 1
3 0 8 3
data np.max np.max
4 4 5 0
2 0 4 6 3 0 8 3 2 0 4 6
result1
2 6 0 1 6
3 0 8 3 8
data
4 4 5 0 5
2 0 4 6 6
np.apply_along_axis(np.max,
axis=1,
arr=data)
Color to Grayscale Conversion SECTION 4 PAGE 38
v Apply with images
200
1 2 3
Axis 0 4 5 6
100 7 8 9
Axis 2
Axis 1
100
200
Color to Grayscale Conversion SECTION 4 PAGE 39
v Apply with images
color2grayscale color2grayscale
12 60 200 … … … …
Cropping a Region SUP. PAGE 40
v Slicing
arr[for_axis_0, for_axis_1, …]
‘:’: get all the elements
‘a:b’: get the elements from a!" to (b!" −1)
data 0: 1 , 0 data : , :
0 1 0 1
0 1 2 0 1 2
1 3 4 1 3 4
2 5 6 2 5 6
Cropping a Region SUP. PAGE 41
v Slicing
v 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,0]= 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
Cropping a Region
(𝑥# , 𝑦# )
width
arr[axis_0, axis_1]
arr[i' : i( , j' : j( ]
heigh
𝑦
(𝑥$ , 𝑦$ )
𝑥
Drawing a Bounding Box
SUP. PAGE 43