Week 2 - Friday - Data Structure - Updated
Week 2 - Friday - Data Structure - Updated
All-in-One Course
Data Structure
Tuple, Set & Dictionary
3
Outline
Tuple
Set
Dictionary
Summarize
Further study
4
•Immutable: •Mutable:
• Nhóm này gồm có: Numbers, String, Tuple, và các kiểu • Nhóm này gồm có: List, Set, Dict và các kiểu
immutable
Credit by: khác do người dùng định nghĩa
https://realpython.com/python-mutable-vs-immutable-types/ mutable khác do người dùng định nghĩa
• Giá trị không thể thay đổi • Giá trị có thể thay đổi 5
Problem in using List
Tuples
6
Solve by Tuples
7
What is a Tuples?
8
9
10
11
AI VIETNAM
All-in-One Course
Tuple
Structure
12
AI VIETNAM
All-in-One Course
Tuple
Structure
13
Do you remember zip() function
Oh No, a new terminology? What is it?
14
Image credit: https://www.freecodecamp.org/
Look back to Iteration in Python
When writing computer programs, you often need to repeat a given piece of code
multiple times. To do this, you can follow one of the following approaches:
Repeating the target code as many times Putting the target code in a loop that runs as many
as you need in a sequence times as you need
Limitations? iterables
18
Discuss more detail in next lecture
Built-in Functions
zip()
the iterators are also iterables which act as their own iterators
1 5
data1 = 1 2 3
2 6
data2 = 5 6 7
3 7
https://colab.research.google.com/drive/1yZIgm8v5pkCk0u2kksV9qiP_-ELGrw3q?usp=sharing
22
Dùng hàm zip() cho tuple
Tuple
len() - Tìm chiều dài của một tuple
23
AI VIETNAM
All-in-One Course
Tuple
Immutable however
0 1 2
add1
t 1 2 add2
0 1
3 10
24
AI VIETNAM
list2tuple
All-in-One Course
Tuple Examples
Swapping two variables Memory requirement
tuple2list
Tuple slicing 25
AI VIETNAM
All-in-One Course
Tuple Case 1: delta<0
Case 2: delta>0
Case 3: delta=0
Data is protected
26
Case study
Tuple in Object Detection and Localization
(x1,y1)
(x2,y2)
Y
28
Case study
Tuple in Object Detection and Localization
29
Giả sử ta có 2 boxes với thông tin như sau: 𝐴
𝑥𝐴 = max(𝑥𝑚𝑖𝑛 𝑏
, 𝑥𝑚𝑖𝑛 𝑏
) = 𝑥𝑚𝑖𝑛
𝐴 𝐴 𝐴 𝐴
•Box A có tọa độ (𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 , 𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ) => Tuple 𝐴
𝑦𝐴 = max(𝑦𝑚𝑖𝑛 𝑏
, 𝑦𝑚𝑖𝑛 𝑏
) = 𝑦𝑚𝑖𝑛
𝐵 𝐵 𝐵 𝐵
•Box B có tọa độ (𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 , 𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ) => Tuple
(xA,yA)
𝐴 𝐴
(𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 )
𝐵 𝐵
(𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 )
(xB,yB)
𝐴 𝑏 𝐴
𝑥𝐵 = min(𝑥𝑚𝑎𝑥 , 𝑥𝑚𝑎𝑥 ) = 𝑥𝑚𝑎𝑥
𝐴 𝑏 𝐴
𝑦𝐵 = min(𝑦𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ) = 𝑦𝑚𝑎𝑥
𝐴 𝐴
(𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 )
𝐵 𝐵
(𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 )
𝑊 = 𝑥𝐵 - 𝑥𝐴
𝐴∩𝐵 =𝑊∗𝐻
H = yB – yA
A B
Area
30
Giả sử ta có 2 boxes với thông tin như sau:
𝐴 𝐴 𝐴 𝐴
•Box A có tọa độ (𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 , 𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ) => Tuple 𝐴
𝑥𝐴 = max(𝑥𝑚𝑖𝑛 𝑏
, 𝑥𝑚𝑖𝑛 𝑏
) = 𝑥𝑚𝑖𝑛
𝐵 𝐵 𝐵 𝐵
•Box B có tọa độ (𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 , 𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ) => Tuple 𝐴
𝑦𝐴 = max(𝑦𝑚𝑖𝑛 𝑏
, 𝑦𝑚𝑖𝑛 𝑏
) = 𝑦𝑚𝑖𝑛
(xA,yA)
𝐴 𝐴
(𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 )
𝐵 𝐵
(𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 )
(xB,yB)
𝐴 𝑏 𝐴
𝑥𝐵 = min(𝑥𝑚𝑎𝑥 , 𝑥𝑚𝑎𝑥 ) = 𝑥𝑚𝑎𝑥
𝐴 𝑏 𝐴
𝑦𝐵 = min(𝑦𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ) = 𝑦𝑚𝑎𝑥
𝐴 𝐴
(𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 )
𝐵 𝐵
𝐵
(𝑥𝑚𝑎𝑥 𝐵
- 𝑥𝑚𝑖𝑛 𝐵
) ∗ (𝑦𝑚𝑎𝑥 𝐵
- 𝑦𝑚𝑖𝑛 ) (𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 )
A B
𝐴 𝐴 𝐴 𝐴
(𝑥𝑚𝑎𝑥 - 𝑥𝑚𝑖𝑛 ) ∗ (𝑦𝑚𝑎𝑥 - 𝑦𝑚𝑖𝑛 )
31
# Tính diện tích của 2 box A, B
𝐴 𝐴 𝐴 𝐴
area1 = (𝑥𝑚𝑎𝑥 - 𝑥𝑚𝑖𝑛 ) ∗ (𝑦𝑚𝑎𝑥 - 𝑦𝑚𝑖𝑛 )
𝐵 𝐵 𝐵 𝐵
area2 = (𝑥𝑚𝑎𝑥 - 𝑥𝑚𝑖𝑛 ) ∗ (𝑦𝑚𝑎𝑥 - 𝑦𝑚𝑖𝑛 )
𝐴 𝑏 𝐴
𝑥𝐵 = min(𝑥𝑚𝑎𝑥 , 𝑥𝑚𝑎𝑥 ) = 𝑥𝑚𝑎𝑥
𝐴 𝑏 𝐴
𝑦𝐵 = min(𝑦𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ) = 𝑦𝑚𝑎𝑥
H = yB – yA
intersection_area = w*h
Non-Maximum Suppression
34
Input
List P of prediction BBoxes of the form (x1,y1,x2,y2,c),
where (x1,y1) and (x2,y2) are the location of the BBox
and c is the predicted confidence score of the model.
Ouput
Return a list keep of filtered prediction BBoxes.
35
AI VIETNAM
All-in-One Course Case study
(List and Tuple for Non-maximum Suppression)
Algorithm
36
https://learnopencv.com/non-maximum-suppression-theory-and-implementation-in-pytorch/
AI VIETNAM
All-in-One Course Case study
(List and Tuple for Non-maximum Suppression)
List Tuple
37
https://colab.research.google.com/drive/1hqSe6gRXhrp-S6G-kLC2arNuk4cQrHTA#scrollTo=-KCCLTpGxiqH
Outline
Tuple
Set
Dictionary
Summarize
Further study
38
39
Problem in List
40
Problem in List
41
Problem in List
43
AI VIETNAM
All-in-One Course
Set
Create a set
Items with different data types
Set comprehension
44
https://colab.research.google.com/drive/1GDLtw5mI0ZC6y-lBqJZXw3khkfee_lrY?usp=sharing
AI VIETNAM
All-in-One Course
Set
45
List of the set operations available in Python
46
AI VIETNAM
All-in-One Course
Set
Add an item Insert a set to another set
47
AI VIETNAM
All-in-One Course
Set
difference function symmetric_difference
48
AI VIETNAM
All-in-One Course
Set
Bitwise operator
49
AI VIETNAM
All-in-One Course
Set
Remove an item
remove(item)
Remove an item from the set. Set comprehension
discard(item)
Remove an item from the set if it is present.
https://docs.python.org/3/library
/stdtypes.html?t#set
50
AI VIETNAM
All-in-One Course
Set
Remove an item
remove(item)
Remove an item from the set. discard(item)
Raises KeyError if elem is not contained in the set. Remove an item from the set if it is present.
https://docs.python.org/3/library/stdtypes.html?t#set 51
AI VIETNAM
All-in-One Course
Set
Create a set Trời ơi, lại 1 thuật ngữ mới? unhashable là gì
đây ta
52
53
A hashable Python object is any object that has a hash value — an integer identificator of
that object which never changes during its lifetime. Most of Python’s immutable built-in
objects are hashable; mutable containers (such as lists or dictionaries) are not;
181
530343892119126197
5256052183123979819
54
AI VIETNAM
All-in-One Course
Set
Set List
and Tuple
???
55
???
56
Case study 1:
Finding a List of Uniques Words
Appearing in the Paragraph
57
AI VIETNAM
AI Course
Set
Convert text to numbers
58
Outline
Tuple
Set
Dictionary
Summarize
Case study
59
Problem in a List, Set
• We want to store following parameters for YOLO machine learning-
based algorithms: batch size, learning rate, momentum, decay. Which
data structure we can use?
Limitations by using
List
60
Problem in a List, Set
• We want to store following parameters for YOLO machine learning-
based algorithms: batch size, learning rate, momentum, decay. Which
data structure we can use?
Solve by Dictionary
61
Case study:
Find frequency of each word
in a paragraph
62
AI VIETNAM
All-in-One Course
Dictionary
Structure bracket
Create a dictionary
63
64
65
AI VIETNAM
All-in-One Course
Dictionary
Create a Dictionary
66
AI VIETNAM
All-in-One Course
Dictionary
67
AI VIETNAM
All-in-One Course
Dictionary
68
AI VIETNAM
All-in-One Course
Dictionary
Get keys and values
Get keys
Get keys
69
AI VIETNAM
All-in-One Course
Dictionary
Get a value by a key
Get value using get() function Get value and delete the corresponding item
70
AI VIETNAM
All-in-One Course
Dictionary
popitem() - lấy ra một phần tử ở cuối dictionary clear() - xóa tất cả các phần tử của một dictionary
71
AI VIETNAM
All-in-One Course
Dictionary
Key that does not exist
72
AI VIETNAM
All-in-One Course
Dictionary
Result ???
73
AI VIETNAM
All-in-One Course
Dictionary
Get a value via a key
Method 1 Method 2
74
AI VIETNAM
All-in-One Course
Dictionary
Merge two dictionaries Check if a key exists
75
Case study:
Find frequency of each word
in a paragraph
76
77
AI VIETNAM
All-in-One Course Case study:
(Dictionary in Image Histogram)
Histogram Algorithm
78
AI VIETNAM
All-in-One Course Case study:
(Dictionary in Image Histogram)
Histogram Algorithm
79
AI VIETNAM
All-in-One Course Case study:
(Dictionary in Image Histogram)
Histogram Algorithm
80
AI VIETNAM
All-in-One Course Case study:
(Dictionary in Image Histogram)
Histogram Algorithm
81
Outline
Tuple
Set
Dictionary
Summarize
Further study
82
83
AI VIETNAM
All-in-One Course
Summarize
PYTHON TUPLES VS LISTS
TUPLES LIST
The items are surrounded in Syntax The items are surrounded in
parathesis () square brackets []
84
AI VIETNAM
All-in-One Course
Summarize
85
Outline
Tuple
Set
Dictionary
Summarize
Further study
86
AI VIETNAM
AI Course Case study (at home):
(Set in Text Classification)
Text classification - 50,000 movie review for sentiment analysis (data)
- Consist of: + 25,000 movie review for training
+ 25,000 movie review for testing
- Label: positive – negative = 1 – 1
“A wonderful little production. <br /><br />The filming technique is very unassuming- very old- positive
time-BBC fashion and gives a comforting, and sometimes discomforting, sense of realism to the
entire piece…..”
“This show was an amazing, fresh & innovative idea in the 70's when it first aired. The first 7 or 8 negative
years were brilliant, but things dropped off after that. By 1990, the show was not really funny
anymore, and it's continued its decline further to the complete waste of time it is today….”
“I thought this was a wonderful way to spend time on a too hot summer weekend, sitting in the air positive
conditioned theater and watching a light-hearted comedy. The plot is simplistic, but the dialogue is
witty and the characters are likable (even the well bread suspected serial killer)….”
“BTW Carver gets a very annoying sidekick who makes you wanna shoot him the first three negative
minutes he's on screen.” 87
AI VIETNAM
AI Course Case study (at home):
(Set in Text Classification)
Text classification
Preprocessing
Text Sigmoid
Sample (standardization & 𝑧 activation
Output
vectorization)
88
Embedding
- Example corpus
sample1: ‘We are learning AI’
sample2: ‘AI is a CS topic’
(1) Build vocabulary from corpus
index 0 1 2 3 4 5 6 7 8
word pad are ai we topic learnin is cs a
g
(2) Transform text into features
We are learning AI AI is a CS topic
Standardize
Vectorization
3 1 5 2 0 2 6 8 7 4
89
AI VIETNAM
All-in-One Course
Sorting item=7
90
AI VIETNAM
item=(‘g’, 13)
All-in-One Course
Sorting
91
AI VIETNAM
item=(‘g’, 13)
All-in-One Course
Sorting
list1 = a g e h b
list2 = 16 13 18 11 15
a g e h b
list3 =
16 13 18 11 15
item
92
AI VIETNAM
All-in-One Course
Lambda function
Syntax
lambda arguments : expression
93
AI VIETNAM
item=(‘g’, 13)
All-in-One Course
Sorting
Using lambda function
94
95