Python Crash Course for Machine Learning
Python Crash Course for Machine Learning
• Define and call functions, grasp function parameters, and return values
• Implement basic error handling
• Work with common Python libraries and frameworks (e.g., NumPy and pandas)
Recommending Music Titles 4
M a c h i n e L e a r n i n g Te c h n i q u e s f o r Te x t
• Useful packages
• NumPy provides efficient storage and computation for multidimensional data
arrays
• SciPy contains a wide array of numerical tools such as numerical integration
and interpolation
• Pandas provides a DataFrame object along with a powerful set of methods to
manipulate, filter, group, and transform data
• Matplotlib provides a useful interface for creation of
publication-quality plots and figures
• Scikit-Learn provides a uniform toolkit for applying common machine
learning algorithms to data
[Link]
[Link]
[Link]
Yes No
Tip : You can open
to close you eye to
represent this
information
Yes No
Tip : You can open
to close you eye to
represent this How can we represent the four directions north, est,
information south and west?
Yes No
Tip : You can open
to close you eye to
represent this How can we represent the four directions north, est,
information south and west?
❖ There are two different binary numbers with one bit (one eye):
0
1
❖ There are two different binary numbers with one bit (one eye):
0
1
000 (decimal 0)
001 (decimal 1)
010 (decimal 2)
011 (decimal 3)
100 (decimal 4)
101 (decimal 5)
110 (decimal 6)
111 (decimal 7)
Python Crash Course 27
How many different numbers?
000 (decimal 0)
001 (decimal 1)
010 (decimal 2)
011 (decimal 3)
100 (decimal 4)
101 (decimal 5)
110 (decimal 6)
111 (decimal 7)
tuatara
Python Crash Course 27
How many numbers can we represent with one byte?
1 byte
1 byte
2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 28 = 256
1 byte
2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 28 = 256
0 0 0 0 0 0 0 0 =0
1 1 1 1 1 1 1 1 = 255
Python Crash Course 28
How many numbers can we represent with one byte?
1 byte
2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 28 = 256
0 0 0 0 0 0 0 0 =0
1 1 1 1 1 1 1 1 = 255
Python Crash Course 28
Built-in data types
• Python comes with several built-in data types that define the nature
of a variable
• Here are some common ones:
• int: Integer type for whole numbers
• float: Floating-point type for decimal numbers
• str: String type for text
• bool: Boolean type for representing truth values (True or False)
• list: Ordered collection of items
• tuple: Immutable ordered collection of items
• dict: Dictionary type for key-value pairs
• set: A collection of unique elements with no duplicate values
• Variable names
• Should start with a letter (a-z, A-Z) or an underscore (_)
• Subsequent characters can be letters, numbers, or underscores
• Case-sensitive: my_var and My_Var are different
• Avoid reserved words
• Do not use Python reserved words (keywords) as variable names.
• Examples of reserved words: if, else, for, while, class, etc.
• Descriptive and Readable:
• Choose names that are descriptive and convey the purpose of the variable
• Use meaningful words or abbreviations for clarity
a=3; b=2
if (a>b):
print("a is larger than b")
a=2; b=2
if (a>b):
print ("a is larger than b")
else:
print("a is less than or equal to b")
my_list = [1,2,3,4]
Output:
1
2
3
Python Crash Course
4 36
while loop
• Functions are groups of code that have a name and can be called
• They can be defined with the def statement
• Example:
def fibonacci(N):
nums = []
a, b = 0, 1
while len(nums) < N:
a, b = b, a + b
[Link](a)
return nums
print(fibonacci(10))
Output:[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Python Crash Course 38
Know your type
Built-in types
v=1 # integer
v = 0.5 # float
v = True # boolean
v = 'name' # string
v = [1,4,3,2] # list
v = (1, 2, 3) # tuple
v = {'one':1, 'two':2, 'three':3} # dictionary
v = {2, 3, 5, 7} # set
Python Crash Course 39
Regular expressions
list = [1,4,3,2]
max_value = list[0]
for n in list:
print(n)
if n > max_value:
max_value = n
print(max_value)
Output:
1
4
3
2
4
Python Crash Course 43
Find the max value in a list
list = [1,4,3,2]
max_value = list[0]
for n in list:
print(n)
if n > max_value:
max_value = n
print(max_value)
Output:
1
4
3
2
4
Python Crash Course 43
Tasks
Let’s practice! • Basic Python
[Link]
hub/PacktPublishing/Machine-
Learning-Techniques-for-
Text/blob/main/python-crash-
couse/[Link]
NumPy ndarray
NumPy ndarray
pandas DataFrame
Images
Images
Audio
Images
Audio
Text
3D tic-tac-toe
3D tic-tac-toe
4D tic-tac-toe
Python Crash Course 49
Numerical representation of B/W images
0 (black)
Python Crash Course
x 50
Numerical representation of B/W images
0 (black)
Python Crash Course
x 50
Display the image on the screen
A pixel
83 82 82 82 82 82 \
82 82 82 81 81 81
82 82 81 81 80 80 \
82 82 81 80 80 79
80 79 78 77 77 77
\
80 79 78 78 77 77 \
\
\
\
Red
R: 255
G: 0
B: 0 Green
R: 0
G: 255
B: 0
Blue
R: 0
G: 0
B: 255
Blue
R: 0
G: 0
B: 255
Numerical image
Numerical image
A color image is composed
of three tables for each
RGB color
2 123
88 9142 045 7466 167
124 4198 5169 2340 240 250
81 10 47 43 221 12 53 97 32 123
123 254
42 453 123
66 167 28 1980 6911 010 0234 250
173 89
50 80 19 0 74 124 4 51 234 156
254 312 1230 119
28 0101 1199 1065 144
234 25012 5476 67
123 42 34 67 167 198 69 0 0 188
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 156
124 198
176 18845 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
81
199
10
2
47
123
88
43
9142
221
045
12
7466
53
167
124
97
111
4198
32
5169
123
2340
123
99
123
80
42
123
254
19
34
254
42
312
0
453
1230
67
74
167
123
66
119
28
167
124
198
28
0101
4
69
1980
1199
51
0
6911
1065
234
0
010
144
234
156
0234
250
188
12
250
173
5476
89
67
average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 124156 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
81
199
10
2
47
123
88
43
9142
221
045
12
7466
53
167
124
97
111
4198
32
5169
123
2340
123
99
123
80
42
123
254
19
34
254
42
312
0
453
1230
67
74
167
123
66
119
28
167
124
198
28
0101
4
69
1980
1199
51
0
6911
1065
234
0
010
144
234
156
0234
250
188
12
250
173
5476
89
67
average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 124156 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
81
199
10
2
47
123
88
43
9142
221
045
12
7466
53
167
124
97
111
4198
32
5169
123
2340
123
99
123
80
42
123
254
19
34
254
42
312
0
453
1230
67
74
167
123
66
119
28
167
124
198
28
0101
4
69
1980
1199
51
0
6911
1065
234
0
010
144
234
156
0234
250
188
12
250
173
5476
89
67
average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 124156 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
(81+10+47+50+80+19+123+42+34)/9=54
81
199
10
2
47
123
88
43
9142
221
045
12
7466
53
167
124
97
111
4198
32
5169
123
2340
123
99
123
80
42
123
254
19
34
254
42
312
0
453
1230
67
74
167
123
66
119
28
167
124
198
28
0101
4
69
1980
1199
51
0
6911
1065
234
0
010
144
234
156
0234
250
188
12
250
173
5476
89
67
average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 124156 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
(81+10+47+50+80+19+123+42+34)/9=54
81
199
10
2
47
123
88
43
9142
221
045
12
7466
53
167
124
97
111
4198
32
5169
123
2340
123
99
123
80
42
123
254
19
34
254
42
312
0
453
1230
67
74
167
123
66
119
28
167
124
198
28
0101
4
69
1980
1199
51
0
6911
1065
234
0
010
144
234
156
0234
250
188
12
250
173
5476
89
67
average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 124156 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
(81+10+47+50+80+19+123+42+34)/9=54
(10+47+43+80+19+0+42+34+67)/9=38
81
199
10
2
47
123
88
43
9142
221
045
12
7466
53
167
124
97
111
4198
32
5169
123
2340
123
99
123
80
42
123
254
19
34
254
42
312
0
453
1230
67
74
167
123
66
119
28
167
124
198
28
0101
4
69
1980
1199
51
0
6911
1065
234
0
010
144
234
156
0234
250
188
12
250
173
5476
89
67
average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 124156 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
(81+10+47+50+80+19+123+42+34)/9=54
(10+47+43+80+19+0+42+34+67)/9=38
…
Python Crash Course 56
Images in Python
184 19 10 73 163 61 10 200 212 20
81 10
2
123
47
123
88
254
42
43
9142
453
221
045
123
66
12
7466
16728
53
167
124
1980
97
4198
6911
32
5169
010
2340
123
0234
240
250
173
250
89
Replace the center pixel with the
50
123
80
42
254
19
34
312
0
1230
67
74
167
119
28
124
198
0101
4
69
1199
51
0
1065
234
0
144
234
156
250
188
12 5476 67 average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 124156 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
81 10
2
123
47
123
88
254
42
43
9142
453
221
045
123
66
12
7466
16728
53
167
124
1980
97
4198
6911
32
5169
010
2340
123
0234
240
250
173
250
89
Replace the center pixel with the
50
123
80
42
254
19
34
312
0
1230
67
74
167
119
28
124
198
0101
4
69
1199
51
0
1065
234
0
144
234
156
250
188
12 5476 67 average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
159 044 875 5582 056 156 198 45 2266 0
12 0
0
159
119
35
44
101
5
99
82
65
56
144
156
12
124
198
176
76
45
188
10
66 0
Every filter applies a numerical
0 35 0 87 55 0 124 176 188 77
81 10
2
123
47
123
88
254
42
43
9142
453
221
045
123
66
12
7466
16728
53
167
124
1980
97
4198
6911
32
5169
010
2340
123
0234
240
250
173
250
89
Replace the center pixel with the
50
123
80
42
254
19
34
312
0
1230
67
74
167
119
28
124
198
0101
4
69
1199
51
0
1065
234
0
144
234
156
250
188
12 5476 67 average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
159 044 875 5582 056 156 198 45 2266 0
12 0
0
159
119
35
44
101
5
99
82
65
56
144
156
12
124
198
176
76
45
188
10
66 0
Every filter applies a numerical
0 35 0 87 55 0 124 176 188 77
81 10
2
123
47
123
88
254
42
43
9142
453
221
045
123
66
12
7466
16728
53
167
124
1980
97
4198
6911
32
5169
010
2340
123
0234
240
250
173
250
89
Replace the center pixel with the
50
123
80
42
254
19
34
312
0
1230
67
74
167
119
28
124
198
0101
4
69
1199
51
0
1065
234
0
144
234
156
250
188
12 5476 67 average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
159 044 875 5582 056 156 198 45 2266 0
12 0
0
159
119
35
44
101
5
99
82
65
56
144
156
12
124
198
176
76
45
188
10
66 0
Every filter applies a numerical
0 35 0 87 55 0 124 176 188 77
81 10
2
123
47
123
88
254
42
43
9142
453
221
045
123
66
12
7466
16728
53
167
124
1980
97
4198
6911
32
5169
010
2340
123
0234
240
250
173
250
89
Replace the center pixel with the
50
123
80
42
254
19
34
312
0
1230
67
74
167
119
28
124
198
0101
4
69
1199
51
0
1065
234
0
144
234
156
250
188
12 5476 67 average of each neighborhood
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
159 044 875 5582 056 156 198 45 2266 0
12 0
0
159
119
35
44
101
5
99
82
65
56
144
156
12
124
198
176
76
45
188
10
66 0
Every filter applies a numerical
0 35 0 87 55 0 124 176 188 77
2 123
88 9142 045 7466 167
124 4198 5169 2340 240 250
81 10 47 43 221 12 53 97 32 123
123 254
42 453 123
66 167 28 1980 6911 010 0234 250
173 89
50 80 19 0 74 124 4 51 234 156
254 312 1230 119
28 0101 1199 1065 144
234 250 12 5476 67
123 42 34 67 167 198 69 0 0 188
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 156
124 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
2 123
88 9142 045 7466 167
124 4198 5169 2340 240 250
81 10 47 43 221 12 53 97 32 123
123 254
42 453 123
66 167 28 1980 6911 010 0234 250
173 89
50 80 19 0 74 124 4 51 234 156
254 312 1230 119
28 0101 1199 1065 144
234 250 12 5476 67
123 42 34 67 167 198 69 0 0 188
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 156
124 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
199 202
123
5988 8891 2980 6574 124
111 104 51
123 234
99 190 Increase contrast?
2 88 9142 045 7466 167
124 4198 5169 2340 240 250
81 10 47 43 221 12 53 97 32 123
123 254
42 453 123
66 167 28 1980 6911 010 0234 250
173 89
50 80 19 0 74 124 4 51 234 156
254 312 1230 119
28 0101 1199 1065 144
234 250 12 5476 67
123 42 34 67 167 198 69 0 0 188
12 00 11935 1010 9987 6555 1440 124
12 76176 188
123 134
254 3 123 28 0 11 10 234 250 67
0 159
35 044 875 5582 056 156
124 198
176 188 45 2266 0
12 0 119 101 99 65 144 12 76 10
159 44 5 82 56 156 198 45 66 0
0 35 0 87 55 0 124 176 188 77
[Link]
hub/PacktPublishing/Machine-
Learning-Techniques-for-
Text/blob/main/python-crash-
couse/[Link]
• Series
• A 1-dimensional labeled array
• Supports many data types
• Get and set values by index label
• DataFrame
• A 2-dimensional labeled data structure
• A dictionary of Series objects
• Columns can be of potentially different types
• Get and set values by row and column label
[Link]
hub/PacktPublishing/Machine-
Learning-Techniques-for-
Text/blob/main/python-crash-
couse/[Link]
64 Detecting Spam Emails
M a c h i n e L e a r n i n g Te c h n i q u e s f o r Te x t
Questions?