[Go to site: main page, start]

0% found this document useful (0 votes)
31 views121 pages

Python Crash Course for Machine Learning

Uploaded by

zawadzkip5
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)
31 views121 pages

Python Crash Course for Machine Learning

Uploaded by

zawadzkip5
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

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

Module 0: Python Crash Course


Dr. Nikos Tsourakis
Course outline

• Module 0: Python Crash Course • Module 6: Teaching Machines to


Translate
• Module 1: Intro to Machine Learning
• Module 7: Summarizing Wikipedia
• Module 2: Detecting Spam Emails Articles
• Module 3: Classifying Topics of • Module 8: Detecting Hateful and
Newsgroup Posts Offensive Language
• Module 4: Extracting Sentiments from • Module 9: Generating Text in Chatbots
Product Reviews
• Module 10: Clustering Speech-to-Text
• Module 5: Recommending Music Titles Transcriptions

Python Crash Course 2


Overview

• Python is programming language chosen for its versatility, readability,


and a vibrant community
• With applications in web development, data science, and artificial
intelligence, Python is an excellent language for beginners
• This crash course provides a rapid introduction to Python,
emphasizing practical skills through hands-on exercises
• It serves as a foundation for further learning and offers skills
applicable across various industries, opening doors to diverse career
opportunities

Python Crash Course 3


Module objectives

After completing this module, you should be able to:


• Understand Python’s fundamental syntax and control flow
• Comprehend essential data structures and their utility

• Define and call functions, grasp function parameters, and return values
• Implement basic error handling

• Read from and write to files in Python


• Request and parse user input

• 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

Section 1: The Python programming language


What is Python?

• Python is a high-level, interpreted programming language known for


its simplicity, readability, and versatility
• Created by Guido van Rossum in the late 1980s, Python has become
one of the most popular languages for both beginners and
experienced developers
• Its syntax is designed to be clear and concise, emphasizing
readability and reducing the cost of program maintenance
• Python supports multiple programming paradigms, including
procedural, object-oriented, and functional programming, making it
adaptable to various use cases

Python Crash Course 6


What is Python?

• With a vast standard library and a thriving ecosystem of third-party


packages, Python is widely employed in web development, data
science, artificial intelligence, automation, and more
• Its ease of learning and powerful capabilities make Python an ideal
choice for individuals and organizations seeking an accessible yet
robust language for diverse applications
• Major online platforms like Google, Dropbox, Instagram, and Spotify
& YouTube — all had worked with this programming language
• You can write Python programs using a simple text editor, or use an
Integrated Development Environment (IDE)
• E.g.: Visual Studio Code
Python Crash Course 7
Python code for data science

• 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

Python Crash Course 8


Python help

[Link]

Python Crash Course 9


Python help

[Link]

Python Crash Course 9


Python help

[Link]

Python Crash Course 9


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

Section 2: Understanding Jupyter notebooks


Jupyter Notebooks
• Jupyter Notebooks provide an interactive
computing environment that allows users
to create and share documents containing
live code, equations, visualizations, and
narrative text
• Jupyter supports various programming
languages, including but not limited to
Python, R, and Julia
• Notebooks are organized into cells, each of
which can contain code, text (formatted
using Markdown), or rich media outputs

Python Crash Course 11


Ad-hoc markup languages

Python Crash Course 12


Markup languages

“In computer text processing, a markup language is a system for


annotating a document in a way that is syntactically distinguishable
from the text, meaning when the document is processed for display,
the markup language is not shown, and is only used to format the text”
[Wikipedia]

Python Crash Course 13


Hypertext Markup Language

• Hypertext Markup Language (HTML) is the standard markup


language for documents designed to be displayed in a web browser
• The “information about content” in HTML is implemented via HTML
tags

Python Crash Course 14


eXtensible Markup Language

• eXtensible Markup Language (XML) is a markup language that


defines a set of rules for encoding documents in a format that is both
human-readable and machine-readable
• It is designed to store and transport data
• Uses the same notion of tags

Python Crash Course 15


LaTeX

• LaTeX is a software system for document preparation


• The writer uses plain text as opposed to the formatted text found in
"WYSIWYG" word processors like Microsoft Word
• Uses markup tagging conventions to define the general structure of
a document, to stylise text, etc.

Python Crash Course 16


Markdown overview

• Markdown is a lightweight markup language for creating formatted


text
• Markdown is designed to be as easy-to-read and easy-to-write as
possible. Benefits:
• Interactivity and exploration (run code in a step-by-step manner)
• Mixed content (mix of code, text, and visualizations)
• Collaboration and sharing (easily shareable)
• Often converted into the corresponding HTML
• Supported by Jupyter:
• Headings, Blockquotes, Code section, Mathematical Symbol,
Line Break, Bold and Italic Text, Horizontal Lines, Ordered
List, Unordered List, Internal and External Link, Table
Python Crash Course 17
Markdown example syntax
Symbols versus HTML

# (Header 1, title) <h1>Header 1,title</h1>


**This is bold text** <b>This is bold text </b>
*This is italic text* <i>This is italic text </i>
--- <hr>
__[text <a
link]([Link] href="[Link]
Link to Google</a>
$\sqrt{k}$ -

Python Crash Course 18


Markdown example cell

Python Crash Course 19


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

Section 3: Basic Python


What are variables?

• In Python, a variable is a named location in the computer's memory


that stores a value
• Think of it as a container or a label that you can use to refer to a
specific piece of data
• Unlike some other programming languages, Python does not require
explicit declaration of the variable type
• You can simply assign a value to a variable, and Python will
determine its type dynamically

Python Crash Course 21


What are variables?

Bulk Cargo Corrugated Pads Easy-Fold Mailers 32 ECT Lightweight

Multi-Depth Storage File Moving Boxes Side Loaders

Haz Mat Wine Shippers Insulated Shippers Wood Crates

Python Crash Course 22


What are variables?

integer float string boolean

list tuple dictionary set

array series date-time dataframe

Python Crash Course 23


Why does it matter?

Python Crash Course 24


Why does it matter?

Many “boxes” in a single process!

Python Crash Course 24


Why does it matter?

Many “boxes” in a single process! Many “containers” is a single server!

Python Crash Course 24


Why does it matter?

Many “boxes” in a single process! Many “containers” is a single server!

✓Understand which data Big Data


type to use ✓But also assess the impact
✓Comprehend the type of your choices on the
returned by the libraries underlying platform
ML
Python Crash Course 24
How can we use an eye to indicate yes or no?

Python Crash Course 25


How can we use an eye to indicate yes or no?

Tip : You can open


to close you eye to
represent this
information

Python Crash Course 25


How can we use an eye to indicate yes or no?

Yes No
Tip : You can open
to close you eye to
represent this
information

Python Crash Course 25


How can we use an eye to indicate yes or no?

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?

Python Crash Course 25


How can we use an eye to indicate yes or no?

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?

north east south west


Python Crash Course 25
How many different numbers?

❖ There are two different binary numbers with one bit (one eye):
0
1

Python Crash Course 26


How many different numbers?

❖ There are two different binary numbers with one bit (one eye):
0
1

❖ There are four different binary numbers with two bits:


00 (decimal 0)
01 (decimal 1)
10 (decimal 2)
11 (decimal 3)

Python Crash Course 26


How many different numbers?

❖ There are two different binary numbers with three bit:

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?

❖ There are two different binary numbers with three bit:

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

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

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

Python Crash Course 29


Type casting

• Transform a floating point number to an integer


• int(3.7)
• Transform an integer to a floating point number
• float(2)
• Transform a string to a floating point number
• float("3.2")
• What is the result of
• int(float("3.2"))
• Can be applied between certain built-in types, if there is a logical and
meaningful conversion between them

Python Crash Course 30


Type casting

• Transform a floating point number to an integer


• int(3.7) → 3
• Transform an integer to a floating point number
• float(2)
• Transform a string to a floating point number
• float("3.2")
• What is the result of
• int(float("3.2"))
• Can be applied between certain built-in types, if there is a logical and
meaningful conversion between them

Python Crash Course 30


Type casting

• Transform a floating point number to an integer


• int(3.7) → 3
• Transform an integer to a floating point number
• float(2) → 2
• Transform a string to a floating point number
• float("3.2")
• What is the result of
• int(float("3.2"))
• Can be applied between certain built-in types, if there is a logical and
meaningful conversion between them

Python Crash Course 30


Type casting

• Transform a floating point number to an integer


• int(3.7) → 3
• Transform an integer to a floating point number
• float(2) → 2
• Transform a string to a floating point number
• float("3.2") → 3.2
• What is the result of
• int(float("3.2"))
• Can be applied between certain built-in types, if there is a logical and
meaningful conversion between them

Python Crash Course 30


Type casting

• Transform a floating point number to an integer


• int(3.7) → 3
• Transform an integer to a floating point number
• float(2) → 2
• Transform a string to a floating point number
• float("3.2") → 3.2
• What is the result of
• int(float("3.2")) → 3
• Can be applied between certain built-in types, if there is a logical and
meaningful conversion between them

Python Crash Course 30


List methods
list: Ordered collection of items The count() method returns the number of
elements with the specified value
• my_list.count(1)
The append() method appends an element to the
end of the list
• my_list.append(5)
The remove() method removes the first
occurrence of the element with the specified value
• my_list.remove(5)
The pop() method removes the element at the
specified position
• my_list.pop(0)
Python Crash Course 31
Naming rules

• 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

Python Crash Course 32


Naming rules

• Snake case for variables


• Standard convention is to use snake_case for variable names
• Example: user_name, total_count, average_value
• Camel Case for functions and methods
• Use camelCase for function and method names
• Example: calculateTotal, getUserInfo, processData
• UPPERCASE for constants
• Constants should be in uppercase with underscores separating words.
• Example: MAX_SIZE, PI_VALUE, DEFAULT_THRESHOLD
• Consistency matters
• Maintain consistency in naming styles throughout your codebase
Python Crash Course 33
Naming rules

• Snake case for variables


• Standard convention is to use snake_case for variable names
• Example: user_name, total_count, average_value
• Camel Case for functions and methods PEP 8 – Style
• Use camelCase for function and method names Guide for
• Example: calculateTotal, getUserInfo, processData Python Code

• UPPERCASE for constants


• Constants should be in uppercase with underscores separating words.
• Example: MAX_SIZE, PI_VALUE, DEFAULT_THRESHOLD
• Consistency matters
• Maintain consistency in naming styles throughout your codebase
Python Crash Course 33
Conditional statements

Making decisions using if statement


The logic:
If something is true, then take the action
Example:

a=3; b=2

if (a>b):
print("a is larger than b")

Output:a is larger than b

Python Crash Course 34


Conditional statements

The if else statement


The logic
If something is true, then take action 1. Otherwise, take action 2
Example:

a=2; b=2

if (a>b):
print ("a is larger than b")
else:
print("a is less than or equal to b")

Output:a is less than or equal to b

Python Crash Course 35


for loop

The for statement


The logic
Use loops to execute a series of repeatable actions
Example:

my_list = [1,2,3,4]

for number in my_list:


print(number)

Output:
1
2
3
Python Crash Course
4 36
while loop

The while statement


The logic
Use loops to execute a series of repeatable actions
Example:
a = 0

while (a < 3):


print("Welcome to Mars!")
a += 1
Output:
Welcome to Mars!
Welcome to Mars!
Welcome to Mars!

Python Crash Course 37


while loop

The while statement


The logic
Use loops to execute a series of repeatable actions
Example:
a = 0

while (a < 3):


print("Welcome to Mars!")
a += 1
Output:
Welcome to Mars! while True:
Welcome to Mars! print("Attack!!!")
Welcome to Mars!

Python Crash Course 37


while loop

The while statement


The logic
Use loops to execute a series of repeatable actions
Example:
a = 0

while (a < 3):


print("Welcome to Mars!")
a += 1
Output:
Welcome to Mars! while True:
Welcome to Mars! print("Attack!!!")
Welcome to Mars!
Output:
Attack!!! ...
Python Crash Course 37
while loop

The while statement


The logic
Use loops to execute a series of repeatable actions
Example:
a = 0

while (a < 3):


print("Welcome to Mars!")
a += 1
Output: Infinite loop
Welcome to Mars! while True:
Welcome to Mars! print("Attack!!!")
Welcome to Mars!
Output:
Attack!!! ...
Python Crash Course 37
Functions

• 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

Built-in data structures

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

• Regular expressions are expressions that are made to match a


specific pattern in a piece of text. E.g.:
• verify the authenticity of emails, phone numbers, and URLs

Python Crash Course 40


Regular expressions

• Regular expressions are expressions that are made to match a


specific pattern in a piece of text. E.g.:
• verify the authenticity of emails, phone numbers, and URLs

Python Crash Course 40


Regular expressions

• Regular expressions are expressions that are made to match a


specific pattern in a piece of text. E.g.:
• verify the authenticity of emails, phone numbers, and URLs

Python Crash Course 40


Email matching

Python Crash Course 41


Algorithms

• Algorithm is a process or set of rules to be followed in calculations or


other problem-solving operations, especially by a computer
• Pseudocode is an informal high-level description of a program or an
algorithm
• For example: Find the max value in the list = [1,4,3,2]
• Pseudocode:
1. Iterate over all elements in the list
2. Check whether the previous maximum value is larger than the current list
element
a. If True store this value

Python Crash Course 42


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

44 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

Section 4: The NumPy library


Python Crash Course 46
Python Crash Course 46
Python Crash Course 46
Python Crash Course 46
Python Crash Course 46
Python Crash Course 46
Python Crash Course 46
Python list

Python Crash Course 46


Python list

NumPy ndarray

Python Crash Course 46


Python list

NumPy ndarray

pandas DataFrame

Python Crash Course 46


Arrays in data science

Python Crash Course 47


Arrays in data science

Python Crash Course 47


Arrays in data science

Images

Python Crash Course 47


Arrays in data science

Images

Audio

Python Crash Course 47


Arrays in data science

Images

Audio
Text

Python Crash Course 47


NumPy arrays

• Data manipulation in Python is nearly synonymous with


NumPy array manipulation
• Even newer tools like Pandas are built around the NumPy
array
• More efficient for storing and manipulating data
• Arrays are very frequently used in data science, where speed
and resources are very important
• The array object in NumPy is called ndarray
• It provides a lot of supporting functions that make working with
ndarray very easy
Python Crash Course 48
Array dimensions

Python Crash Course 49


Array dimensions

3D tic-tac-toe

Python Crash Course 49


Array dimensions

3D tic-tac-toe

4D tic-tac-toe
Python Crash Course 49
Numerical representation of B/W images

• Each element of the table


represents only the quantity of the
light
• The intensity values of the pixels
vary from 0 (black) to 255 (white)
• All values are saved in the image
255 (white)
file
y

0 (black)
Python Crash Course
x 50
Numerical representation of B/W images

• Each element of the table


represents only the quantity of the
light
• The intensity values of the pixels
vary from 0 (black) to 255 (white)
• All values are saved in the image
255 (white)
file
y
In practice encoding algorithms are
used to reduce the image size

0 (black)
Python Crash Course
x 50
Display the image on the screen

How can we render the image table 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 \
\
\
\

Python Crash Course 51


Pixels

• Pixels are the elements that make up


each digital image. They are placed
horizontally and vertically
• As they differ in size, each monitor has a
different number of pixels per inch (PPP)

Python Crash Course 52


Pixels

• Pixels are the elements that make up


each digital image. They are placed
horizontally and vertically
• As they differ in size, each monitor has a
different number of pixels per inch (PPP)

Python Crash Course 52


RGB color scheme

Red
R: 255
G: 0
B: 0 Green
R: 0
G: 255
B: 0

Blue
R: 0
G: 0
B: 255

Python Crash Course 53


RGB color scheme

256 x 256 x 256


=16.8M colors
Red
R: 255
G: 0
B: 0 Green
R: 0
G: 255
B: 0

Blue
R: 0
G: 0
B: 255

Python Crash Course 53


Numerical representation of images

Numerical image

pixel 10 10 16 28 RGB


 9 656 70 56  43
 32 99
26
54 70
37  78
 67 
96 56 colors
15  256013902296
  67
  21  54 47  42 
32 158587853943  92
54  65 65 39 
Python Crash Course
32 65 87 99 54
Numerical representation of images

Numerical image
A color image is composed
of three tables for each
RGB color

pixel 10 10 16 28 RGB


 9 656 70 56  43
 32 99
26
54 70
37  78
 67 
96 56 colors
15  256013902296
  67
  21  54 47  42 
32 158587853943  92
54  65 65 39 
Python Crash Course
32 65 87 99 54
Images in Python

How can we blur the image?

Python Crash Course 55


Images in Python

How can we blur the image?

184 19 10 73 163 61 10 200 212 20

199 202 5988 8891 2980 6574 124


111 104 51
123 234
99 190

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

159 44 5 82 56 156 198 45 66 0

Python Crash Course 55


Images in Python
184 19 10 73 163 61 10 200 212 20

202 5988 8891 2980 6574 124 104 51 234 190

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

240 250 Replace the center pixel with the


50

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

159 44 5 82 56 156 198 45 66 0

Python Crash Course 56


Images in Python
184 19 10 73 163 61 10 200 212 20

202 5988 8891 2980 6574 124 104 51 234 190

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

240 250 Replace the center pixel with the


50

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

159 44 5 82 56 156 198 45 66 0

Python Crash Course 56


Images in Python
184 19 10 73 163 61 10 200 212 20

202 5988 8891 2980 6574 124 104 51 234 190

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

240 250 Replace the center pixel with the


50

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

159 44 5 82 56 156 198 45 66 0

(81+10+47+50+80+19+123+42+34)/9=54

Python Crash Course 56


Images in Python
184 19 10 73 163 61 10 200 212 20

202 5988 8891 2980 6574 124 104 51 234 190

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

240 250 Replace the center pixel with the


50

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

159 44 5 82 56 156 198 45 66 0

(81+10+47+50+80+19+123+42+34)/9=54

Python Crash Course 56


Images in Python
184 19 10 73 163 61 10 200 212 20

202 5988 8891 2980 6574 124 104 51 234 190

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

240 250 Replace the center pixel with the


50

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

159 44 5 82 56 156 198 45 66 0

(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

202 5988 8891 2980 6574 124 104 51 234 190

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

240 250 Replace the center pixel with the


50

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

159 44 5 82 56 156 198 45 66 0

(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

199 202 5988 8891 2980 6574 124


111 104 51
123 234
99 190

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

159 44 5 82 56 156 198 45 66 0

Python Crash Course Source: [Link] 57


Images in Python
184 19 10 73 163 61 10 200 212 20

199 202 5988 8891 2980 6574 124


111 104 51
123 234
99 190

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

159 44 5 82 56 156 198 45 66 0


transformation to the image

Python Crash Course Source: [Link] 57


Images in Python
184 19 10 73 163 61 10 200 212 20

199 202 5988 8891 2980 6574 124


111 104 51
123 234
99 190

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

159 44 5 82 56 156 198 45 66 0


transformation to the image

Python Crash Course Source: [Link] 57


Images in Python
184 19 10 73 163 61 10 200 212 20

199 202 5988 8891 2980 6574 124


111 104 51
123 234
99 190

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

159 44 5 82 56 156 198 45 66 0


transformation to the image

Python Crash Course Source: [Link] 57


Images in Python
184 19 10 73 163 61 10 200 212 20

199 202 5988 8891 2980 6574 124


111 104 51
123 234
99 190

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

159 44 5 82 56 156 198 45 66 0


transformation to the image

Python Crash Course Source: [Link] 57


How can we make the image brighter?

184 19 10 73 163 61 10 200 212 20

199 202 5988 8891 2980 6574 124


111 104 51
123 234
99 190

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

159 44 5 82 56 156 198 45 66 0

Python Crash Course 58


How can we make the image brighter?

184 19 10 73 163 61 10 200 212 20

199 202 5988 8891 2980 6574 124


111 104 51
123 234
99 190

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

159 44 5 82 56 156 198 45 66 0


+ 10
+ 10
+ 10

Python Crash Course 58


How can we make the image brighter?

184 19 10 73 163 61 10 200 212 20

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

159 44 5 82 56 156 198 45 66 0


+ 10
+ 10
+ 10

Python Crash Course 58


Tasks
Let’s practice! • Basic Python
• NumPy

[Link]
hub/PacktPublishing/Machine-
Learning-Techniques-for-
Text/blob/main/python-crash-
couse/[Link]

59 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

Section 5: The pandas library


Overview

• Pandas is a newer package built on top of NumPy


• Adds data structures and tools designed to work with table-like data
• Provides tools for data manipulation: reshaping, merging, sorting,
slicing, aggregation etc.
• Provides an efficient implementation of a DataFrame:
• multidimensional arrays with attached row and column labels
• often with heterogeneous types
• and/or missing data
• Implements a number of powerful data operations familiar to users
of both database frameworks and spreadsheet programs

Python Crash Course 61


Pandas data structures

• 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

Python Crash Course 62


Data ingestion

• Pandas supports data ingestion from various sources, including


CSV files, Excel files, SQL databases, JSON, HTML, and more
• Use pd.read_csv() to read data from a CSV (Comma-Separated
Values) file
• Use pd.read_excel() to read data from an Excel file
• Use pd.read_sql() to read data from a SQL database query
• Use pd.read_json() to read data from a JSON file or a JSON string
• Use pd.read_html() to perform web scraping and read HTML tables

Python Crash Course 63


Tasks
Let’s practice! • Basic Python
• NumPy
• pandas

[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?

You might also like