0% found this document useful (0 votes)
12 views94 pages

AIVN Numpy

Uploaded by

hail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views94 pages

AIVN Numpy

Uploaded by

hail
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 94

Introduction to 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

One-hot encoding for label


1 2 3
𝑦 = 0 → 𝒚 = [1 0]
1 2 3
Axis 0 4 5 6 𝑦 = 1 → 𝒚 = [0 1]
7 8 9
Axis 2
* * * = 2 4 6
2 2 2 scalar vector
Axis 1
Outline
SECTION 1

Introduction Axis 1

Axis 0 1 2
3 4 shape=(3,2)
SECTION 2 5 6
Common Functions
SECTION 3

Indexing and Broadcasting 1 2 3


Axis 0 4 5 6
7 8 9
SECTION 4 Axis 2

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

1D array 2D array 3D array


Motivation Section 1 Page 4
❖ Why do I need Numpy?

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

Indexing and Broadcasting


4 2 4 2 2 1
SECTION 4

Examples
Some Common Functions Section 2 Page 7
❖ Create Numpy arrays

zeros() function ones() function full() function


Some Common Functions Section 2 Page 8
❖ Create Numpy arrays
arange(start, end, step)
eye() function random() function arange() function
0 1 2 3 4

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

Common Functions result = 2 4 6

SECTION 3

Indexing and Broadcasting


1 2 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 data 0, 1 data 1: 3


0 1 0 1 0 1
0 1 2 0 1 2 0 1 2
1 3 4 1 3 4 1 3 4
2 5 6 2 5 6 2 5 6

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

arr[[0, 1, 2], [0, 1, 0]] = 1 4 5

arr[[0, 0], [1, 1]] = 2 2


Array Indexing
Section 3 Page 17
❖ 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
Numpy Array Operations Section 3 Page 18
❖ Summation

data
sum(data)
1 2
Axis 0 10
3 4

Axis 1

sum(data, axis=0) sum(data, 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

(Stable) Formula Probability


X X-m
𝑚 = max(𝒙) 𝑥1 = 1.0 𝑥1 = −2.0 𝑓(𝑥1 ) = 0.09
𝑒 (𝑥𝑖−𝑚) 𝑥2 = 2.0 𝑥2 = −1.0 Softmax 𝑓(𝑥2 ) = 0.24
𝑓 𝑥𝑖 =
σ𝑗 𝑒 (𝑥𝑗 −𝑚) 𝑥3 = 3.0 𝑥3 = 0 𝑓(𝑥3 ) = 0.67

𝑒𝑥

𝑥 ∈ (−∞, 0)
𝑒 𝑥 ∈ (0, 1)

𝑥
Outline
SECTION 1

Introduction Feature Label

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

Predict future temperature in weather forecasting


Example 2 Section 4 Page 26
❖ Weather forecasting
Example 2
Section 4 Page 27
❖ Daily average temperatures

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

One-hot encoding for label One-hot encoding for 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

One-hot encoding for label


𝑦 = 0 → 𝒚 = [1 0 0]
𝑦 = 1 → 𝒚 = [0 1 0]
𝑦 = 2 → 𝒚 = [0 0 1]
Example 4 Section 4 Page 31
❖ Text data
❖ IRIS data
Example 4 Section 4 Page 32
❖ Some functions

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

Find the unique elements


‘class-1’ ‘class-2’ ‘class-3’ ‘class-2’
np.unique(data) data

result = ‘class-1’ ‘class-2’ ‘class-3’


1 2 3 2 1
data

np.unique(data) = 1 2 3
Example 4 Section 4 Page 34
❖ Some functions
data = ‘class-1’ ‘class-2’ ‘class-3’

data = ‘0’ ‘1’ ‘2’


Cast to a specified type
np.ndarray.astype(dtype=float) data = 0 1 2

For each element in data,


replace old by new
np.char.replace(data,
old,
new)
Example 4 Section 4 Page 35
❖ Text data
❖ IRIS data

col 0 col 1 col 2 col 3 col 4


43
Numpy for 2D and
3D Data

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

OpenCV and NumPy

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

Pixel p = scalar Resolution: #pixels


Resolution = HeightxWidth
0 ≤ p ≤ 255
Image Data SECTION 1 PAGE 2

v Color images

RGB color image

𝑟
Pixel p= 𝑔
𝑏

0 ≤ r,g,b ≤ 255

(Height, Width, channel)

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
… … … …

❑ Hình ảnh RGB thông thường

B
G

R
Data Processing SECTION 1 PAGE 4
v Images in files

output = cv2.imread(path, mode)

mode=0: read images in grayscale

mode=1: read images in color

output = cv2.resize(input, (height, width))

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

Not runnable on Colab

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

image_rgb = image[:, :, [2, 1, 0]]


image_rgb = image[:, :, ::-1]
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
OpenCV and Numpy SECTION 1 PAGE 11
v Read an image

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

OpenCV and NumPy

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

Load all the images and save in a


Numpy array
Image File Loading SECTION 2 PAGE 14
v Get file paths
samples
Image File Loading SECTION 2 PAGE 15
v Load images cv2.imread(path, mode)
samples
Image File Loading SECTION 2 PAGE 16
v Load images cv2.resize(image, shape)
samples
Image File Loading SECTION 2 PAGE 17
v Load images
samples
Image File Loading SECTION 2 PAGE 18
v Load images
samples
Image File Loading SECTION 2 PAGE 19
v matplotlib train
Image File Loading SECTION 2 PAGE 20
v matplotlib train
Outline
SECTION 1

OpenCV and NumPy 255


0

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

Unsigned: All values 8 bits:


are positive 𝐴 ∈ 0,255

Integer: All values


image1.png are whole number

Cast to a specified type


np.ndarray.astype(dtype = np.uint8)
Brightness Changes SECTION 3 PAGE 22
v Problem of out of range

data = 1 2.5 float64 0 data = 0 255


255

data = 1 2 uint8 data = 10 9

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)

(500, 1200) cv2.imwrite(path, img)


Brightness Changes SECTION 3 PAGE 26
v Load an image

(Height, Width, Channel)

img = cv2.imread(path, 1)

(500, 1200, 3) cv2.imwrite(path, img)


Brightness Changes
SECTION 3 PAGE 27
v Increase the brightness
of a grayscale image

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

200 + 100 = 44 200 + 100 = 300


uint8 uint8 float64 float64
Brightness Changes
SECTION 3 PAGE 32
v Increase brightness - Implementation - 1
Brightness Changes
SECTION 3 PAGE 33
v Increase brightness - Implementation - 2
Brightness Changes
SECTION 3 PAGE 34
v Decrease brightness - Implementation - 1
Brightness Changes
SECTION 3 PAGE 35
v Decrease brightness - Implementation - 2
Outline
SECTION 1

OpenCV and NumPy

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 data 0, 1 data 1: 3


0 1 0 1 0 1
0 1 2 0 1 2 0 1 2
1 3 4 1 3 4 1 3 4
2 5 6 2 5 6 2 5 6

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( ]

arr[y' :y( , x' :x( ]

heigh
𝑦

(𝑥$ , 𝑦$ )
𝑥
Drawing a Bounding Box
SUP. PAGE 43

You might also like