Data Structure
Tuple, Set and Dictionary
IOU, NMS using Tuple Text embedding using Set Histogram using Dictionary
Vinh Dinh Nguyen
PhD in Computer Science
2
Outline
Ø What is a Tuple in Python
Ø What is a Set in Python
Ø What is a Dictionary in Python
Ø Quizz
Ø Text Classification using Set
Ø Image Histogram using Dictionary
Ø Further Reading
3
Data Structure?
A data structure is a storage that is used to store and organize data. It
is a way of arranging data on a computer so that it can be accessed
and updated efficiently.
4
Review: From 1D to 2D List
Given that the teacher cannot remember students' names, please develop a
program to help her find a student's name based on his/her seat location in
the class.
Hàng 1/
Row 1 Vinh Ân
“Cô mời 2 bạn: ngồi ở vị trí hàng thứ 3, dãy thứ 1, và ngồi ở
vị trí hàng thứ 3, dãy thứ 2” lên bảng giải thích về 2D List
Hàng 2/
Row 2 Toàn Lâm
“Cô mời bạn Tám và bạn Chuyện” lên bảng giải thích về
Hàng 3/ Tám Chuyện 2D List
Row 3
Dãy 1/ Dãy 2/
Column 1 Column 2
5
1D List Solution
Hàng 1/
Row 1 Vinh Ân
Hàng 2/
Row 2 Toàn Lâm
Hàng 3/ Tám Chuyện
Row 3
Are there any easier solutions? This one is really
difficult for newcomers
Dãy 1/ Dãy 2/
Column 1 Column 2
6
2D List Solution
Teacher View
Progrogramer View
Column index 0 Column index 1 Column 1 Column 2
Row index 0
Vinh An Row 1 Vinh An
Row index 1 Toan Lam Row 2 Toan Lam
Row index 2 Tam Chuyen Row 3 Tam Chuyen
Use 1D List Vinh An
Use 1D List Toan Lam
student_list = [element1, element2, element3)
Use 1D List Tam Chuyen
7
2D List Solution
Teacher View
Column 1 Column 2
Row 1 Vinh An
Row 2 Toan Lam How to access element:
row 0 and column 1
Row 3 Tam Chuyen row 0 and column 2
row1_retrieve = student_list[0] Vinh An
Column index 0 Column index 1
Row index 0
Vinh An row1_retrieve[0] Vinh
Row index 1 Toan Lam row1_retrieve[1] An
Row index 2
Tam Chuyen
student_list[0][0] Vinh
Progrogramer View
student_list[0]1] An
8
2D List Solution
Column 1 Column 2
Row 1 Vinh An
Row 2 Toan Lam
Row 3 Tam Chuyen
Teacher View
Column index 0 Column index 1
Row index 0
Vinh An
Row index 1 Toan Lam
Row index 2
Tam Chuyen
Progrogramer View
Thanks! This one is easy to understand
9
Mutable Vs. Immutable
In Python, everything is treated as an object. Every object has these three attributes:
q Identity – This refers to the address that the object refers to in the computer’s memory.
q Type – This refers to the kind of object that is created.
q Value – This refers to the value stored by the object.
Mutable Immutable
We can change the value of a variable We can not change the value of a variable (which we
(which we have stored) have stored)
List, Dictionaries, Set, … String, Tuples, …
While ID and Type cannot be changed once it’s created, values can be changed for Mutable objects.
10
Mutable Vs. Immutable
vList is Mutable vString is Immutable
11
Mutable Vs. Immutable
vList is Mutable vString is Immutable
Object Interning In Python
Optimize memory usage
12
Tuple Motivation
v Develop a function to return personal information (multiple values)
# Function returning multiple values
def get_person():
name = "Camedinh" List Example
age = 2004
occupation = "Student"
return [name, age, occupation]
# Test
person = get_person()
person[0] = ” Your name has been hacked "
print(person[0], person[1], person[2])
Your name has been hacked, 2024, Student
v Can we prevent this happen?
13
Tuple Motivation
v Develop a function to return personal information (multiple values)
Tuple Example v How to use a Tuple
14
What is a Tuple
15
What is a Tuple
16
What is a Tuple
17
What is a Tuple
v Structure
tuple_name = (element-1, …, element-n)
Tuple unpacking
18
What is a Tuple
v Structure
tuple_name = (element-1, …, element-n)
( ) can be removed Tuple with one element
18
19
What is a Tuple
v + and * operators
count() - đếm số lần xuất hiện của một giá trị
index() - tìm vị trí xuất hiện của một giá trị
20
What is a Tuple Dùng hàm zip() cho tuple
len() - Tìm chiều dài của một tuple
Lấy giá trị min và max của một tuple
Sắp xếp các giá trị trong một tuple
list2tuple
21
Tuple Examples
Swapping two variables Memory requirement
tuple2list
Tuple slicing 21
22
Tuple Examples
Case 1: delta<0
v Example: Solve quadratic equation
Case 2: delta>0
Case 3: delta=0
Data is protected
22
22
23
Q&A Time
24
Outline
Ø What is a Tuple in Python
Ø What is a Set in Python
Ø What is a Dictionary in Python
Ø Quizz
Ø Text Classification using Set
Ø Image Histogram using Dictionary
Ø Further Reading
25
Set Motivation
List
1 0 2 1 0 2
Set
1 3 2 4 5 6
Lists and tuples are standard Python data types that store values in a sequence. Sets are another standard Python data type that also store
values. The major difference is that sets, unlike lists or tuples, cannot have multiple occurrences of the same element and store unordered values.
26
Set
v Create a set
Items with different data types
Using curly brackets
Set comprehension
27
Set
Access the items of a set Copy a set
28
Set
Add an item Insert a set to another set
Join two sets Not allow duplicate values
29
Set
difference function symmetric_difference
difference_update function symmetric_difference_update
30
Set
v Bitwise operator
31
Set
v 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.
[Link]
/[Link]?t#set
32
Set
v 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.
[Link]
33
Set
v Create a set
Unordered and unindexed Cannot contain unhashable types
34
Set
Set ßà List
and Tuple
???
???
35
Q&A Time
36
Outline
Ø What is a Tuple in Python
Ø What is a Set in Python
Ø What is a Dictionary in Python
Ø Quizz
Ø Text Classification using Set
Ø Image Histogram using Dictionary
Ø Further Reading
37
Dictionary Motivation
number_list = [11, 15, 35, 20, 18, 9]
11 15 35 20 18 9
Index 0 1 2 3 4 5
index
value
number_list[0] => 11
It's very hard to remember that the 1st element in a list has index 0, is there an easier
way to remember this?
number_dict ={”One”:11, “Two”:15, ”Three”:35, “Four”:20, “Five”:18, “Six:9” }
11 15 35 20 18 9
key "One Two Three Four Five Six
key
value
number_dict[“One”] => 11
38
Dictionary Motivation
39
Dictionary
Any immutable can be dictionary
v Structure bracket key. like string , number, tuple
dictionary_name = {key-1:value-1, …, key-n:value-n}
element comma colon
Create a dictionary
40
Dictionary
41
Dictionary
v Create a Dictionary
42
Dictionary
v Update a value v Copy a dictionary
43
Dictionary
v Hàm copy() chỉ sao chép v Sử dụng hàm deepcopy()
kiểu shallow trong module copy
44
Dictionary
v Get keys and values
Get keys
Get keys
Get keys and values
Get values
45
Dictionary
v Get a value by a key
Get value using get() function Get value and delete the corresponding item
46
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
Use del keyword to delete an item
47
Dictionary
v Key that does not exist
Try to delete a non-existing item Try to get an item by a non-existing key
48
Dictionary
setdefault() function example
Result ???
49
Dictionary
v Get a value via a key
Method 1 Method 2
50
Dictionary
Merge two dictionaries Check if a key exists
Remove empty items Dictionary comprehension
51
Q&A Time
52
Outline
Ø What is a Tuple in Python
Ø What is a Set in Python
Ø What is a Dictionary in Python
Ø Quizz
Ø Text Classification using Set
Ø Image Histogram using Dictionary
Ø Further Reading
53
Outline
Ø What is a Tuple in Python
Ø What is a Set in Python
Ø What is a Dictionary in Python
Ø Quizz
Ø Text Classification using Set
Ø Image Histogram using Dictionary
Ø Further Reading
54
Set in Text Classification
v Text classification
Preprocessing Machine
Text
Sample (standardization & Learning Output
Algorithm
vectorization)
”Make money” Preprocessing Embedding Fully-connected layer
0 1 2 4 5 6 7 4 8 Embedding features Support Vector Machine Spam
55
Set in Text Classification
- 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
word pad are/a ai we topic learning is cs
(2) Transform text into features
We are learning AI AI is a CS topic
Standardize, Vectorization we are learning ai ai is a cs topic
3 1 5 2 2 6 1 7 4
55
56
Set in Text Classification
vVocabulary Building
Python is a high-level, interpreted, general-purpose
programming language. Its design philosophy
emphasizes code readability with the use of significant Python Program
indentation. Python is dynamically-typed and garbage- (using Set
collected. It supports multiple programming structure)
paradigms, including structured, object-oriented and
functional programming.
Input Output
“Python is a high-level” 15 4 25 29 22
Text data Vocabulary
Numerical Data
57
Outline
Ø What is a Tuple in Python
Ø What is a Set in Python
Ø What is a Dictionary in Python
Ø Quizz
Ø Text Classification using Set
Ø Image Histogram using Dictionary
Ø Further Reading
58
Image in Computer Vision
Grayscale image
Binary Image
Color image
59 Image Coordinate System: Grayscale Image
Access pixel at a specific location
Read image
60
Image Histogram: Dictionary
Histogram Algorithm
61
Image Histogram: Dictionary
Histogram Algorithm
255 0 255 255 255 0 255
255 0 0 255 255 0 255
255 0 0 0 0 0 255
0 0 255 255 255 0 0
0 0 255 255 255 0 0
255 0 255 255 255 0 255
255 255 0 0 0 255 255
62
Image Histogram: Dictionary
Histogram Algorithm
63
Image Histogram: Dictionary
Histogram Algorithm
64
Summary
Data Structure Ordered Mutable Constructor Example
List Yes Yes [] or list() [5. 7, ‘yes’, 5.7]
Tuple Yes No () or tuple() (5.7, ‘yes’, 5.7)
Set No Yes {} or set() {5.7, ‘yes’ }
Dictionary No Yes {} or dict{} {‘key’: value}
Text embedding using Set Histogram using Dictionary
IOU, NMS using Tuple
65
References
66
66
67
Outline
Ø What is a Tuple in Python
Ø What is a Set in Python
Ø What is a Dictionary in Python
Ø Quizz
Ø Text Classification using Set
Ø Image Histogram using Dictionary
Ø Further Reading
68
69
IOU Calculation Using Tuple
70
IOU Calculation Using Tuple
71
IOU Calculation Using Tuple
# Tính diện tích của 2 box A, B
$ - 𝑥 $ ) ∗ (𝑦 $ - 𝑦 $ )
area1 = (𝑥!"# !%& !"# !%&
' '
area2 = (𝑥!"#
' -𝑥 '
!%& ) ∗ (𝑦!"# - 𝑦!%& )
# Tìm tọa đọ của vùng giao nhau (Intersection)
$ ( (
𝑥𝐴 = max(𝑥!%& , 𝑥!%& ) = 𝑥!%&
$ ( (
𝑦𝐴 = max(𝑦!%& , 𝑦!%& ) = 𝑦!%&
$ ( $
𝑥𝐵 = min(𝑥!"# , 𝑥!"# ) = 𝑥!"#
$ , 𝑦( ) = 𝑦 $
𝑦𝐵 = min(𝑦!"# !"# !"#
# Tính diện tích vùng giao nhau
𝑊 = 𝑥𝐵 - 𝑥𝐴
H = yB – yA
intersection_area = w*h
# Tính diện tích phần hợp nhau
union_area = area1 + area2 - intersection_area
# Dựa trên phần giao và phần hợp để tính IoU
IoU = intersection_area / union_area
72
Non-Maxima Suppression: List & Tuple
Non Maximum Suppression
Algorithm
List Tuple Step 1 : Select the prediction S with highest confidence score and remove
it from P and add it to the final prediction list keep. (keep is empty
initially).
Step 2 : Now compare this prediction S with all the predictions present
in P. Calculate the IoU of this prediction S with every other predictions
in P. If the IoU is greater than the threshold thresh_iou for any
prediction T present in P, remove prediction T from P.
Step 3 : If there are still predictions left in P, then go to Step
1 again, else return the list keep containing the filtered predictions.