Learn Autonomous Programming with Python Utilize Python’s capabilities in artificial intelligence, machine learning, deep... (P Divadkar, Varun) (Z-Library)
Learn Autonomous Programming with Python Utilize Python’s capabilities in artificial intelligence, machine learning, deep... (P Divadkar, Varun) (Z-Library)
Programming with
Python
Utilize Python’s capabilities in
artificial intelligence, machine
learning, deep learning and
robotic process automation
Varun P Divadkar
www.bpbonline.com
First Edition 2024
ISBN: 978-93-55517-630
https://rebrand.ly/nh3wzsj
The code bundle for the book is also hosted on GitHub at
https://github.com/bpbpublications/Learn-Autonomous-
Programming-with-Python.
In case there’s an update to the code, it will be updated on
the existing GitHub repository.
We have code bundles from our rich catalogue of books and
videos available at https://github.com/bpbpublications.
Check them out!
Errata
We take immense pride in our work at BPB Publications and
follow best practices to ensure the accuracy of our content
to provide with an indulging reading experience to our
subscribers. Our readers are our mirrors, and we use their
inputs to reflect and improve upon human errors, if any, that
may have occurred during the publishing processes
involved. To let us maintain the quality and help us reach
out to any readers who might be having difficulties due to
any unforeseen errors, please write to us at :
errata@bpbonline.com
Your support, suggestions and feedbacks are highly
appreciated by the BPB Publications’ Family.
Did you know that BPB offers eBook versions of
every book published, with PDF and ePub files
available? You can upgrade to the eBook version at
www.bpbonline.com and as a print book customer,
you are entitled to a discount on the eBook copy.
Get in touch with us at :
business@bpbonline.com for more details.
At www.bpbonline.com, you can also read a
collection of free technical articles, sign up for a
range of free newsletters, and receive exclusive
discounts and offers on BPB books and eBooks.
Piracy
If you come across any illegal copies of our works in
any form on the internet, we would be grateful if you
would provide us with the location address or website
name. Please contact us at
business@bpbonline.com with a link to the
material.
Reviews
Please leave a review. Once you have read and used
this book, why not leave a review on the site that you
purchased it from? Potential readers can then see and
use your unbiased opinion to make purchase
decisions. We at BPB can understand what you think
about our products, and our authors can see your
feedback on their book. Thank you!
For more information about BPB, please visit
www.bpbonline.com.
2. RPA Foundations
Introduction
Structure
Objectives
History of Robotic Process Automation
What is RPA
Components of RPA
Various RPA tools in the market
Comparison between various RPA tools
RPA Python package
Practical use case of RPA with Python
Conclusion
12. Hyperautomation
Introduction
Structure
Objectives
Defining hyperautomation: What it is and why it matters
The hyperautomation cycle: Key steps and processes
Exploring typical use cases for hyperautomation
Enhancing document understanding with optical
character recognition
Implementing conversational agents: The role of
chatbots
Advancing efficiency with robotic process automation
Navigating the challenges of hyperautomation
Practical use case in Python
Conclusion
Index
Chapter 1
Why Python for
Automation?
Introduction
This chapter introduces the reader to the significance of
Python as an open-source language and further delves
deeper to explain why it is the most powerful and
formidable tool for autonomous programming. The chapter
emphasizes Python’s inherent flexibility, adaptability, and
user-friendly nature, attributed to its high-level language
and straightforward syntax. It also throws insights into
Python’s flexibility and adaptability considerations and ease
of use as a high-level language with a simple syntax.
Further into the chapter, Python’s rich assortment of
libraries like ‘Pandas’, ‘NumPy’, ‘Matplotlib’ would be
discussed which are pivotal for machine learning. In
subsequent chapters, we will delve deeper into the practical
implementations of these libraries.
Structure
The chapter covers the following topics:
• Python as an open-source language
• Python’s repository of extensive libraries
• Python as a high-level language
• Portability aspect of Python
• Salient advantages of Python
Objectives
At the end of this chapter, you will clearly understand why
Python has been chosen as the language for autonomous
programming despite the availability of numerous other
alternatives from the myriad pool of programming
languages.
Conclusion
Now that we know why Python is the chosen favorite, what
else are we waiting for! In the next chapter, we will
introduce you to the exciting realm of Robotic Process
Automation (RPA) technology and witness the power of
Python in executing RPA processes independently. Without
further delay, let kick start our journey by moving to the
next chapter on RPA foundations!
Introduction
This chapter intends to build a foundation of Robotic
Process Automation (RPA), which is one of the hallmarks
of the current industry. Today, RPA technology can be
conveniently leveraged by an HR department to
automatically source candidates and screen their resumes,
thus reducing hours of manual effort. This chapter takes the
reader into the historical background and progress of robotic
technology and how it has led the industry to the current
state of RPA. Components of RPA have been covered in a
lucid manner in order to get the reader familiar with the
concepts. Various industry leaders in the RPA market like
Automation Anywhere, UiPath and Blue Prism have been
described in this chapter along with their comparative
advantages and disadvantages over each other. The end of
this chapter tries to highlight the role that Python plays in
enhancing the potential of RPA and discusses a practical use
case of how Python can be conveniently leveraged to
achieve the same RPA which these tools do.
Structure
The chapter covers the following topics:
• History of Robotic Process Automation
• What is RPA
∘ Components of RPA
∘ Various RPA tools in the market
∘ Comparison between various RPA tools
• RPA Python package
• Practical use case of RPA with Python
Objectives
At the end of this chapter, you would have a practical
understanding of Robotic Process Automation (RPA), its
various offerings in the market and how Python can be
efficiently utilized to achieve RPA.
Components of RPA
We have now understood that RPA is software enabling the
creation of bots for process automation. Like every other
software, RPA has certain components which are generic to
all the RPA products that we have in the market. Let us look
at each of these components:
• Recorder: This is the most important but basic and u
ser-friendly component of an RPA tool. This is similar t
o the Record Macro feature that Microsoft Excel prov
ides. In this feature, the user records the manual proc
ess while it is being performed. What the recorder doe
s is that it captures salient elements that are being na
vigated by the user on the screen like buttons, web pa
ge URLs and dropdowns and treats them as objects. It
also captures the properties of these objects. After the
recording is complete, the user has the flexibility to ed
it these properties and make them generic by passing
them into variables, thus transforming a hardcoded re
cording into a dynamic bot! The figure below is a scre
enshot of the recorder feature in UiPath:
Automatio
Parameter n Blue Prism UiPath
Anywhere
Macro Yes No Yes
recording
Communit Yes No Yes
y edition
Drag and Not for all Yes Yes
drop tasks.
(Requires
scripting)
Automatio
Parameter n Blue Prism UiPath
Anywhere
Architectur Client Client Web based
e server server orchestrat
or
Cost High High Pricing is
acquisition deploymen entry level
cost t cost
Table 2.1: Comparison of RPA tools in market
Figure 2.3: The word ‘news’ being typed into Google Search page
This line uses the type() method which enables the code to
type characters into the webpage element. The webpage
element is specified in the first argument which is
'//*[@name="q"]'. This is the XPath of the search textbox
in the Google window. The XPath which stands for XML Path
Language is basically an expression which is used to identify
an element in an XML document, in this case the search
textbox. We shall see how to obtain XPaths in the next
section containing the practical use case. The second
argument is news. This argument specifies the text that
needs to be entered. In this case, the text being entered is
‘news’.
The next line is:
r.click('//*[@name="btnK"]')
This line instructs the code to click the button titled Google
Search which shall produce the search results. The XPath
identifier for this button is //*[@name="btnK"].
After this, the next line is:
r.wait(5)
This line basically adds a wait time of 5 seconds to the
program until the web page is opened. This is necessary
because if the program prematurely executes the next line
without waiting for the process from the initial line to be
completed, then the program will throw an error.
The next line is:
r.snap('page', 'Captured_Image.png')
This line uses the snap() method to take a screenshot of
the page that is currently open in the website window. This
snapshot is then saved as an image file named
Captured_Image.png which is provided as the second
argument of the method. This image file would be saved in
the home directory of the program which is basically the
same folder where the Python file is saved. This screenshot
would basically display the search result obtained after
typing the word news in Google Search box.
The last line r.close() would close the rpa object and the
process thereafter.
In this way, we saw how the Python rpa package offers a
functionality to automate regular manual processes which
would have otherwise also been done using RPA tools like
UiPath, Automation Anywhere and Blue Prism. In this case
we took a simple example of opening a web page, producing
a search result, taking a screenshot, and saving the
screenshot as an image. This Python rpa package is an
excellent tool for those who are familiar with programming
and would want to unleash the potential of RPA through the
medium of writing code.
Let us now take a little complex but real-world use case of
visiting a training registration web page and filling
information of every participant from an Excel file. In this
use case we would build on the experience that we acquired
in the exercise above and leverage the use of Python loops
and Pandas dataframes to execute what we want.
We find that the name property has value fname which can
be used as an identifier in the type() method. The code
below would execute that for us. We enter the value from
variable first_name into the field First Name:
r.type('//*[@name="fname"]', first_name)
In a similar manner, we could enter the values from
variables last_name and email into fields Last Name and
Email respectively by capturing their identifiers using the
name property. The two lines of code below would execute
that for us.
r.type('//*[@name="lname"]', last_name)
r.type('//*[@name="email"]', email)
Next, we would want to mention the correct course for each
participant by selecting the appropriate field in the
dropdown. This value has been captured in the variable
course. Similar to the previous manner, we capture the
element identifier from the HTML code and enter it into the
code as shown below. Here, we note that since the field here
is a dropdown, we would need to use the select() method
instead of the type() method.
r.select('//*[@name="nf-field-22"]', course)
Similarly, we mention the month of enrollment by selecting
the appropriate month from the dropdown corresponding to
the value in variable month.:
r.select('//*[@name="nf-field-24"]', month)
For the remaining field How do you know about us?* which
is a radio button, we shall select ‘Others’ as the default
option for every participant. The code below would do that
for us. Note that we use the click() method as this is a
radio button.
r.click('//*[@id="nf-field-23-6"]')
Finally, we would need to click the Register button at the
bottom of the webpage. This field could not be shown in the
earlier snapshot due to space considerations. Hence, it is
being shown in the Figure 2.7 below:
Conclusion
In this chapter, we first went through the fundamental
concepts of Robotic Process Automation tools and then
compared various tools available in the market based on
important parameters. These tools offer drag and drop
functionality that enable a person to execute RPA processes
without much knowledge of coding. However, for Python
enthusiasts who want to execute the same automation
through scripting, we saw how the rpa open-source package
from Python does wonders in automating most common
processes like filling web page forms by looking at a
practical use case.
We are now on course to begin an exciting journey through
contemporary era’s most glamorous technological
buzzwords, and they are none other than Artificial
Intelligence and Machine Learning!
In the next chapter, we would be acquainting ourselves with
the fundamental concepts of Artificial Intelligence and
Machine Learning. We would also be studying commonly
used machine learning algorithms and later get introduced
to deep learning and neural networks. As eager as we could
get, let us now go to the next chapter!
Chapter 3
Getting Started with
AI/ML in Python
Introduction
This chapter introduces the reader to fundamental concepts
of artificial intelligence and machine learning. The chapter
begins with the historical evolution of artificial intelligence
and discusses the current and future scenario. The reader is
then introduced to the definition and basic concepts of
machine learning followed by machine learning algorithms
grouped into various categories including supervised and
unsupervised learning. Various Python libraries and their
applications in AI/ML frameworks shall provide the reader
with an idea of the significance of Python in AI/ML
implementations. Further pages into this chapter shall
discuss deep learning and its applications and the chapter
will end with a summary of neural networks, natural
language processing and transformers.
Structure
The chapter covers the following topics:
• Background and history of AI
• Machine learning concepts
• Supervised and unsupervised learning
• Popular Python libraries for ML
• Reinforcement learning
• Deep learning
• Introduction to neural networks
∘ Types of neural networks
• Natural language processing
• Transformers and large language models
Objectives
The main intention behind this chapter is to acquaint the
reader with basic theoretical understanding of machine
learning and deep learning concepts in an easy way which
shall enable the reader to effectively apply the acquired
knowledge to independently model machine learning
algorithms using Python in later chapters. Hence, this
chapter being a foundation oriented one, is more focused on
theory than about writing Python code. By the end of this
section, you will have a clear grasp of key principles in
artificial intelligence, machine learning, and deep learning,
setting the stage for hands-on applications.
Figure 3.4: Sample decision tree to finalize the decision of buying a house
• Random forests: This is a continuation of decision tr
ee algorithm but what we do here is we combine the r
esults of multiple decision trees to come up with the r
esult. The limitation with having a single decision tree
is that there is a possibility of overfitting. Overfitting is
the phenomenon where an algorithm fits the training
data set so rigidly that it does not have the bandwidth
to accommodate any flexibility to allow itself to fit wit
h equivalent ease on the testing dataset. Random fore
st is an example of an Ensemble Learning algorithm th
at uses the Bagging technique. Ensemble Learning is t
he technique of improving results by combining the re
sults of several models. Bagging is the technique whe
re different samples of training data are fed to the alg
orithm to obtain a different result for each sample. In t
his case, random samples of training data are fed to t
he algorithm. This results in the creation of multiple d
ecision trees that produce multiple outputs. The result
is decided by a majority vote process. The most frequ
ently occurring output is chosen as the result.
• Support vector machine: This is a classification alg
orithm that creates an optimal hyperplane that separa
tes the dataset in the best possible way into categorie
s or classes based on differentiable attributes. If the d
ata set is two dimensional with two classes, then the
hyperplane would be a straight line. The algorithm wo
rks by choosing the points closest to the line from bot
h classes and trying to maximize the distance betwee
n the points and the line called as margin. These poin
ts are called as ‘Support Vectors’ hence the name sup
port vector machine.
Figure 3.5: Support vector machine
Deep learning
The name deep learning is derived from process of
training multiple layers. The central mechanism of deep
learning is an Artificial Neural Network (ANN) which is
intended to mimic the neural learning mechanism in a
person. Deep learning involves the training of multiple
layers of neural networks which is achieved by adjusting the
weights of every neuron. More on this to come in later
chapters.
One might wonder why does one need to approach a deep
neural network in spite of the availability of all these
machine learning algorithms? The reason is that where
machine learning algorithms fall short of accuracy at
numerous instances, deep neural networks do an
exceptional job of achieving the desired result! Due to the
rise of high and efficient computational systems and
availability of large datasets, deep learning has become a
popular method of training complex multi layered neural
networks. Let us now try to understand the basic concepts
of a neural network.
Conclusion
This chapter has laid us a strong foundation of artificial
intelligence and machine learning concepts by taking us on
a progressive journey starting from the history of AI to basic
ML concepts and later from deep learning and neural
networks to NLP and Transformers. This foundation should
now enable us to develop algorithms on our own using
Python libraries in later chapters to come that are specially
devoted to machine learning and deep learning.
In order to develop and train good ML models, the first thing
we need is a good dataset. This data is obtained from
numerous sources, the most common one being web pages,
which makes extraction of data from web, or web scraping
the most essential skill before starting any ML project.
While extracting data from web pages, it becomes easy if
the web API is provided to us. However, in the absence of
such API, one should not miss out on the opportunity to
extract web page data. In the next chapter, we shall explore
the concept of web scraping, which is the process of
extracting data from web pages without using any API.
Hence, without wasting further time, let us jump into the
next chapter on web scraping using Python!
Chapter 4
Automating Web
Scraping
Introduction
This chapter discusses the process of extracting data from
the web using Python. Data is the central part of all projects
in machine learning and most of the data for learning
projects or real-world applications is sourced from the web.
Hence, knowledge of web scraping techniques is
fundamental to the process of implementing projects in AI,
ML, or data science. This chapter introduces the reader to
web scraping and discusses some popular Python libraries
like Requests and Beautiful Soup that are required to
perform web scraping. The chapter continues with salient
features of web scraping like inspection and extraction. The
chapter ends with a real-world use case project.
Structure
The chapter covers the following topics:
• What is web scraping
• Popular Python libraries for web scraping
∘ The request module in Python
∘ The Beautiful Soup Library
∘ Inspecting the web page
• Extracting information from the web page
• Legal considerations of web scraping
• Practical use case in Python
Objectives
This chapter will prepare the reader to comfortably use
Python libraries to perform web scraping for regular data
gathering tasks and familiarize the reader with commonly
used web scraping techniques.
x = requests.get('https://toscrape.com/')
if x.status_code == 200: #A status of 200 is returned
whenever a request is OK.A status of 404 means not found.
print(x.text)
The above piece of code would first import the requests
library, then use the get method of the requests module
and pass the URL as a parameter. In this case, we want to
see the contents of the toscrape.com webpage, which is
basically a web scraping sandbox, so we pass the respective
URL. Thereafter, the object that is returned is stored in the
variable x. Now, we use the text property of the object
stored in variable x to return the text that we wish to extract
from the webpage. The contents returned would be in an
html code format.
x = requests.get('https://toscrape.com/')
We would now want to import the Beautiful Soup library
using the following code:
from bs4 import BeautifulSoup
Next, we write the following piece of code:
if x.status_code == 200:
objSoup = BeautifulSoup(x.content, 'html.parser')
Note that instead of using x.text as done in the previous
exercise, we use x.content. The former is the response
content in Unicode whereas the latter one is the response
content in bytes.
Here, we have created a BeautifulSoup object named
objSoup by passing the below two arguments:
• x.content : This is the HTML object that is obtained e
arlier.
• html.parser : This is the HTML parser that we would
want to use.
Beautiful Soup supports the usage of multiple parsers. The
one that we have used here is the HTML parser from Python
included in the Python standard library.
The Table 4.1 below lists the various parsers that are
supported by Beautiful Soup and the way in which they are
used:
Figure 4.3: Developer’s tool visible at the right after clicking on ‘Inspect’
x = requests.get('https://toscrape.com/')
if x.status_code == 200: #A status of 200 is returned
whenever a request is OK.A status of 404 means not found.
objSoup = BeautifulSoup(x.content, 'html.parser')
print(objSoup.title) #This would print the title tag
print(objSoup.title.name) #This would print the name of
the tag
The output thus produced would look like this:
<title>Scraping Sandbox</title>
title
Now, let us try to extract information from the table below
which is situated at the bottom of the web page (refer to
below Figure 4.4):
Let us try to arrive at this text through code. Type the code
below in the editor:
import requests
from bs4 import BeautifulSoup
x = requests.get('https://toscrape.com/')
if x.status_code == 200:
objSoup = BeautifulSoup(x.content, 'html.parser')
We have created the BeautifulSoup object similar to the
previous exercise.
Now, let us use the find method to find the elements that
we are searching for. We first need to find the first div tag
with class="container" as this is the topmost tag within
the body. We do that using the code expression below:
objSoup.find('div',class_="container")
Within this, there are three div tags with class = "row", but
we need to access only the last one. How do we let Beautiful
Soup know this? We do this by first using the FindAll
method and then using indexing to get the nth element that
we want. Below is the modified code expression where we
use the index 2 to fetch the third element.
objSoup.find('div',class_="container").findAll('div',class_="r
ow")[2]
Now that we have understood how to navigate through the
HTML tree using the find and FindAll methods along with
indexing, let us write the complete statement of code to
navigate to the final text element that we require. Execute
the code below in the Python editor which is the final code:
import requests
from bs4 import BeautifulSoup
x = requests.get('https://toscrape.com/')
if x.status_code == 200:
objSoup = BeautifulSoup(x.content, 'html.parser')
print(objSoup.find('div',class_="container") \
.findAll('div',class_="row")[2] \
.find('div',class_="col-md-10") \
.findAll('div',class_="col-md-6")[1] \
.find('table',class_="table table-hover" \
).findAll('tr')[1].findAll('td')[1].text)
We observe that the output obtained is Microdata and
pagination. That is exactly what we wanted! However, do
not you think that this method of navigation was long and
unnecessarily tedious and complicated? Can we not have a
direct and shorter way to get this done? Of course, there is!
Since the data that we want lies in the table, why not we
just search for all tables in the HTML tree? Let us try to get a
list of all elements with the tag table using the code below:
objSoup.findAll('table')
However, we would see that the result would give us two
tables since we have a total of two tables in the web page.
In order to get the last table, we can use indexing in the
manner shown below. Here, index 0 means first and index 1
means second:
objSoup.findAll('table')[1]
Now that we have arrived at the table, we can continue with
the remaining expression as we did previously. The final
expression would look like as given below:
objSoup.findAll('table')[1].findAll('tr')[1].findAll('td')[1].text
Given below is the complete code for reference:
import requests
from bs4 import BeautifulSoup
x = requests.get('https://toscrape.com/')
if x.status_code == 200:
objSoup = BeautifulSoup(x.content, 'html.parser')
print(objSoup.findAll('table')[1].findAll('tr')[1].findAll('t
d')[1].text)
Output: Microdata and pagination
Introduction
Microsoft Excel automation is the commonly performed
automation exercise by organizations and individuals as
Excel is the commonly used spreadsheet tool. The chapter
begins with the openpyxl library, which is a Python library to
read/write Excel 2010 xlsx/xlsm/xltx/xltm files and then
takes the reader through the process of automating some
common tasks like creating a workbook, opening a
workbook, and performing various operations in a workbook
and worksheet. The chapter continues with the description
and usage of other Python libraries used for Excel
automation like xlwings and xlsxWriter. The relative
advantage of each library over the other has been briefly
discussed. The chapter ends with a real-world use case
project involving Excel automation with Python.
Structure
The chapter covers the following topics:
• Need for automating Excel using Python
• Introduction to openpyxl library
∘ Open and modify an existing workbook
∘ Access a cell using range name
∘ Merging a cell
∘ Looping through cells
∘ Working with Excel formulae using openpyxl
∘ Create charts using openpyxl
∘ Styling a chart
• Other Python libraries for Excel automation
∘ Comparison summary of Python libraries
• Practical use case in Python
Objectives
This chapter would introduce the reader to the process of
creating basic and intermediate scripts to automate regular
Excel processes by unleashing the power of open-source
Python libraries. Thereafter, the reader would be able to
comfortably explore these Python modules independently to
create custom scripts for more complicated tasks.
objWorksheet = objWorkbook.worksheets[0]
objWorksheet['A5'] = 'I have modified the contents of this
cell using the range name!'
objWorkbook.save("Learn_Excel_Automation.xlsx")
Here, we used objWorksheet['A5'] to access cell having
range A5 and modified its contents. On running the code in
the Python editor and opening the workbook, we would see
the text written in cell range A5 as shown in the Figure 5.3
below:
Merging cells
In order to see how to merge cells, we shall open the same
workbook that we saved in the earlier exercise and merge
cells. Type the code below into the Python editor:
from openpyxl import load_workbook
from openpyxl.styles import Alignment
objWorkbook =
load_workbook("Learn_Excel_Automation.xlsx")
objWorksheet = objWorkbook.worksheets[0]
objWorksheet.merge_cells('A1:D2')
cell = objWorksheet.cell(1,1)
cell.alignment = Alignment(horizontal='center',
vertical='center')
objWorkbook.save("Learn_Excel_Automation.xlsx")
In this code, the function merge_cells would merge the
cells contained within range A1:D2 into a single cell. The
text contained in the first cell This is my first task using
Openpyxl would be retained in the merged cell. In order to
align the text at the center, we have imported another
module called Alignment and passed the parameters
horizontal='center', vertical='center' to align the
contents at the center. You would agree that this is the same
as manually performing ‘Merge and Center’ in Excel. Here,
we have used openpyxl to automate this process.
On opening the saved workbook, you would see the cell
range A1:D2 merged into one cell as shown in the Figure 5.4
below:
dict_scores = {'A':
{'Mathematics':45,'Science':55,'English':75},\
'B':{'Mathematics':75,'Science':87,'English':87},\
'C':{'Mathematics':90,'Science':95,'English':58},\
'D':{'Mathematics':100,'Science':28,'English':80},\
'E':{'Mathematics':35,'Science':75,'English':90}}
col=2
for key in dict_scores:
student = dict_scores[key]
row=2
for subject in student:
score = student[subject]
objWorksheet.cell(row,col).value = score
objWorksheet.cell(row,col).alignment = \
Alignment(horizontal='center', vertical='center')
row+=1
col+=1
objWorkbook.save("Student Table.xlsx")
Here, we first create a dictionary named dict_scores that
stores the scores of every student in each of the subjects
Mathematics, Science and English. This dictionary contains
keys ‘A’, ‘B’, ‘C’, ‘D’,’E’ that correspond to the students.
Every value corresponding to this key is also a dictionary
that contains keys as subjects ‘Mathematics’, ‘Science’,
‘English’ and values as the respective scores obtained by
the student in these subjects. Next, we loop through each
student key and within this loop we further loop through
every subject key to obtain the scores of every student in
that subject.
Study the loop in the code carefully to see how the loop
navigates through individual columns for each student and
through every row within each column while entering the
scores. After running the code, open the workbook Student
Table.xlsx. You would observe that the scores have been
entered as shown in the Figure 5.6 below:
Figure 5.6: Students table populated with scores using Python loop
for i in range(4):
str_col_letter = objWorksheet.cell(row,col).column_lette
r
objWorksheet[str_col_letter + '5'] =translator.translate_f
ormula(str_col_letter + "5")
objWorksheet.cell(row,col).alignment =
Alignment(horizontal='center',vertical='center')
col+=1
objWorkbook.save("Student Table.xlsx")
Figure 5.9: Excel formula dragged to other cells using openpyxl Translator with
loops
Figure 5.10: Bar Chart showing the average score of each student
Styling a chart
The chart that we have created in the previous exercise is a
basic chart. However, we would also want to explore the
functionality that openpyxl provides in adding various styles
to the chart like color, border, and format.
Copy the code below into the Python editor:
import openpyxl
from openpyxl import load_workbook
from openpyxl.chart import BarChart,Reference
objWorkbook = openpyxl.load_workbook('Student
Table.xlsx')
objWorksheet = objWorkbook.worksheets[0]
chart = objWorksheet._charts[0]
chart.style = 8
objWorkbook.save("Student Table.xlsx")
Close the workbook ‘Student Table.xlsx’ and run the code.
On opening the workbook, we observe from the Figure 5.11
below that the chart color has been changed:
Figure 5.11: Exploring different chart colors using the ‘style’ attribute
month_list =
['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov',
'Dec']
stock_list = ['A','B','C','D','E']
objFormat_1=objWorkbook.add_format({'border':1})
objFormat_2=objWorkbook.add_format({'border':2})
objWorksheet.write_column('A2', stock_list,objFormat_2)
objWorksheet.write_row('B1', month_list, objFormat_2)
row=1
for stock in dict_Stock_Prices:
stock_prices = dict_Stock_Prices[stock]
objWorksheet.write_row(row,1, stock_prices, objFormat_
1)
row+=1
objWorkbook.close()
Here, we first prepare a dictionary having keys as the stock
names ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ and values as lists containing the
stock prices from Jan to Dec.
We also prepare two lists called ‘month_list’ containing the
month names and stock_list containing the stock names.
Next, we create two variables objFormat_1 and
objFormat_2 where we specify the formats of the border
that we pass as arguments to the write method of the
worksheet object. Here, ‘border':1 is a regular border while
'border':2 is a bold border.
Next, we call the write_column and write_row methods of
the worksheet object and pass the variables stock_list and
month_list respectively along with the border format
variable objFormat_2.
Here, it is interesting to note how xlsxwriter provides useful
methods like write_row and write_column that enables
one to directly pass an entire row or column data instead of
looping through individual values. Likewise, we also saw
how xlsxwriter provides excellent formatting features like
regular border and bold border creation using one single line
of code.
At the end of the code, we use the write_row method while
looping through every stock to enter the respective monthly
values for the stock. Here, we use objFormat_1 which is
regular border.
After running the code, we open the saved workbook and
observe that the dataset that we require has been created
in the tabular format as shown in the Figure 5.16 below:
Figure 5.16: Raw data entered in a tabular format using ‘xlsxwriter’ library of
Python.
Figure 5.18: Pie chart of the average price of stocks plotted using openpyxl.
Conclusion
Excel spreadsheets being the crux of office tasks, learning
spreadsheet automation is a great way to kick start one’s
journey in Python automation since maximum amount of
data is imported and maintained in spreadsheets. Hope this
chapter has achieved that objective in the most optimum
way!
Now, our next target would be to delve into the world of
messaging. Modern communication with the advent of social
media has brought to the forefront multiple avenues of
communication through WhatsApp along with the most
popular mailing platform Gmail. It is going to be exciting to
know how Python provides the functionality to automate
messaging through these platforms, which we will learn
about in the next chapter.
Introduction
The advent of communication and social media has led to
an urgent need for instant messaging. Gmail has been the
most widely used web-based email service for personal and
light professional use. This chapter starts with a basic
demonstration of automating the process of email creation
in Gmail using Python. The last decade has also witnessed
the rise of WhatsApp as the most sought-after messaging
app which has almost displaced the usage of the regular
SMS and become an indispensable part of people’s lives due
to its ability to provide quick messaging features to
individuals and groups. This chapter will throw some light on
automating the process of sending messages on WhatsApp
to individuals and groups using the PyWhatKit library
provided by Python. The chapter would then end with a
practical use case in Python.
Structure
The chapter covers the following topics:
• Prerequisites for Gmail automation
∘ Turning on 2-step verification
∘ Getting app password
∘ Sending a Gmail message
• Automating WhatsApp messaging
• Practical use case in Python
Objectives
By the end of this chapter, we shall master the essentials of
email and messaging automation using Python. We shall
learn to send emails with Gmail and messages on WhatsApp
through hands-on examples. Equipped with these skills, we
shall be ready to apply Python’s automation capabilities to
streamline communication tasks for personal and
professional use.
strSender = 'XXXXXXXX@gmail.com'
strReceiver = 'XXXXXXXX@gmail.com'
strPassword = 'XXXXXXXX'
objEmail = EmailMessage()
objEmail['From'] = strSender
objEmail['To'] = strReceiver
objEmail['Subject'] = strSubject
objEmail.set_content(strBody)
objContext = objSSL.create_default_context()
objSMTP =
smtplib.SMTP_SSL('smtp.gmail.com',465,context=objConte
xt)
objSMTP.login(strSender, strPassword)
objSMTP.sendmail(strSender,
strReceiver,objEmail.as_string())
Most of the code is self-explanatory. In the value for string
variable strSender enter your Gmail ID for which you
performed the 2-step verification and generated the app
password. For the variable strReceiver enter the email ID
of the person to whom you want to send the email message.
While for the variable strPassword enter the app password
that we just generated and saved in the previous section.
We use ssl library from Python in order to add a layer of
security to safeguard information by authenticating the
website. SSL basically stands for Secure Socket Layer
protocol.
To send the mail, we use the smtplib library from Python
which provides the functionality of sending email through
Simple Mail Transfer Protocol. We also create a variable
objSMTP and initialize it to the SMTP_SSL object of the
smtpblib module. We will then specify smtp.gmail.com as
the email server, 465 as the port and the objContext
variable as the context. Finally, we use the login and
sendemail methods to send the mail by entering the
appropriate variables email ID values and password.
Having completed this successfully, we are done with
everything required to send an email through Gmail!
To now test this functionality yourself, you might want to
use another Gmail ID of your own as the receiver email and
observe after running the code if that email has been sent
to the receiver’s email or not.
Now that we are familiar with creating and sending emails
through Gmail, let us move forward to another exciting topic
on automation of WhatsApp messaging!
Automating WhatsApp messaging
Python provides the PyWhatKit library which can be
conveniently used to automate the process of sending
WhatsApp messages. Before we start using this library, the
first step would be to install it using the command below:
pip install pywhatkit
Next, type the code below into the Python editor:
import pywhatkit
pywhatkit.sendwhatmsg_instantly("XXXXXXXXXXXXX","Hel
lo")
We can clearly observe that the code is short and contains
just two lines. In the first line, we import the pywhatkit
library and in the second line, we use the
sendwhatmsg_instantly method that takes in two
arguments which are the phone numbers of the receiver
and the message that needs to be sent.
On running the code, the WhatsApp web page would be
loaded at the first instance asking the user to login to
WhatsApp web by scanning the QR code. The way to login
to WhatsApp web is to go to the Settings option, select
Linked Devices and then click on the Link a Device
button. Thereafter the scanner would open which would
allow one to scan the QR code and log-in to WhatsApp web.
today = datetime.datetime.now().strftime("%m-%d")
strSender = 'XXXXXXXXX'
strPassword = 'XXXXXXXXXXX'
strSubject = 'Happy Birthday!'
strBody = """ Wish you a very happy birthday! """
# Looping through every row of the table
i=0
for row in df['Birthday']:
birthday = row.strftime("%m-%d")
objContext = objSSL.create_default_context()
objSMTP = smtplib.SMTP_SSL('smtp.gmail.co
m',465,context=objContext)
objSMTP.login(strSender, strPassword)
objSMTP.sendmail(strSender, strReceiver,obj
Email.as_string())
i+=1
The code is self-explanatory as comments have been added.
In the piece where the code loops through every row, an if
block has been added. It this if block that checks whether
the birthday of the person is today whose details have been
included in the row. If yes, then the code would send a
WhatsApp message saying, Happy birthday and a
corresponding birthday email on the person’s Gmail
address.
Conclusion
This chapter has laid the foundation for automating Gmail
and WhatsApp which are commonly used mailing and
messaging tools respectively. We went through the
pywhatkit library and the SMTP protocol for Gmail. For the
curious Python programmer, there is still a lot to explore in
these two areas and experiment with!
In the next chapter, we shall explore automation of PDF files
and images. Discussion around automation would be
incomplete without these two. PDF files are the most
common source of raw data sources for automation and
machine learning projects. Hence, understanding of the
process of extracting data from PDF documents using
Python assumes paramount importance. In parallel, image
documents also constitute an unstructured data format
which requires a different kind of work around to extract
data. Hence, without wasting any further time, the curious
reader would flip to the next page to start this extremely
interesting topic!
Introduction
This chapter demonstrates data reading from PDF files and
working with image files using Python. The chapter begins
with an introduction to the PyPDF library and takes the user
through various functions like reading, merging, rotating,
and splitting the PDF files. It also discusses some limitations
of the PyPDF library in specific cases and how to tackle
those limitations using other Python libraries like Tabula and
Textract. Thereafter, the user is introduced to the PIL library
in Python that deals with reading and transforming image
files. Further, the chapter continues with the very important
topic of optical character recognition (OCR) which is
implemented through the Pytesseract library. The last
section shall discuss another popular Python package
OpenCV used for computer vision. The chapter ends with
the demonstration of a practical use case in computer
vision.
Structure
The chapter covers the following topics:
• PyPDF library
• Read a PDF file using PyPDF2
• Rotate and merge PDF files
• Working with images using the PIL library
• Optical character recognition
• Working with OpenCV
• Practical use case in Python
Objectives
This chapter shall equip you with the skills to adeptly
extract data and manipulate PDF files using Python. You will
gain hands-on experience with optical character recognition
to convert images to text, culminating in practical exercises
and a comprehensive use case that showcase Python’s
capabilities in processing digital documents and images.
PyPDF library
PyPDF is a Python package that allows one to read and
transform PDF files. The original PyPDF underwent revisions
to form PyPDF2, PyPDF3 and PyPDF4. The revised versions
support Python 3. The version PyPDF2 was abandoned after
a point but was later revived and is now being maintained
actively. The latest version PyPDF4 however, does not have
full backwards compatibility with this PyPDF2. Hence, we
shall focus only on PyPDF2 in this chapter.
In order to start using this library, the first step is its
installation. The library can be installed using the command
below:
pip install pypdf2
Read a PDF file using PyPDF2
We shall use the file Stock Prices.pdf for this exercise to
read data using Python. Copy the code below into the
Python editor:
import PyPDF2
pdfReader = PyPDF2.PdfReader('Stock Prices.pdf')
pageObj = pdfReader.pages[0]
print(pageObj.extract_text())
On running the code above, we get the output in the
console as shown below:
Below i s a table showing the price of five stocks
A,B,C,D, and E from January to December. The first
column
contains the stock names and the remaining columns
contain the respective values of the stock from
January to December.
Stocks Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
Dec
A 357 457 187 831 779 338 129 508 407 748 511 609
B 14908 13408 17103 18886 19828 12098 17080
16850 15023 12405 15469 13800
C 60 64 54 93 87 74 96 92 83 85 70 88
D 1667 1962 1845 1535 1753 1767 1551 1893 1715
1707 1627 1532
E 2181 2333 2265 2274 2739 2601 2569 2520 2744
2234 2836 2230
Observe the above output carefully.
The first thing that comes to our notice is that the table that
is populated at the bottom is bereft of column lines that
separate the columns. Hence, it becomes difficult to read
the table.
The second observation we can make is that although the
output provides us with the extracted text, there are
challenges that will arise when it is directly used. We see
that in the first sentence Below i s a table.. the word is
has been populated erroneously by inserting a space
between i and s. This might occasionally happen because
the text in a PDF document is not like a stream of text that
we have in a word document. All that a PDF document
knows is that it has a set of characters that need to be
placed at positions relative to each other. This relative
positioning of characters might sometimes make it difficult
for the parser to figure out whether two characters
constitute the same word or whether they should be
separated by space. Each character in a PDF document is
enclosed by a bounding box. The position of this bounding
box is determined by an X and Y co-ordinate each for the
lower left and upper right points. Sometimes, the bounding
boxes of two adjacent characters might have a greater
separation than is generally expected between adjacent
characters. That might create difficulty for the parser to
identify that the adjacent characters belong to the same
word.
Whatever might be the challenges here, we need to find
some ways to address them. In such cases, we need to use
alternative Python libraries to get our job done.
In order to address the first challenge, we could use a library
called tabula that is specially meant for extracting tables.
Copy the code below into the Python editor:
from tabula import read_pdf
objTable = read_pdf("Stock Prices.pdf",pages="all")
df = objTable[0]
df.to_excel("Stock Prices.xlsx",index=None)
Here, we basically import the read_pdf module of the
tabula library. We have entered two arguments in this
module. The first argument is the name of the PDF
document which is Stock Prices.pdf. The second argument
is the pages. In this case, we specify all. This is a single
page document, but as a practice, specifying all should
extract information from all pages of the document. We then
store the value of this object into the objTable variable.
The important part of this code is the creation of the df
variable which we basically obtain by indexing the objTable
variable. In this case, we have just one table so we specify
objTable[0] to obtain the first table. It is important to note
that value returned here is a pandas dataframe which is
extremely advantageous from the point of view of exporting
it or transforming it.
Here, we export the dataframe to an Excel file named Stock
Prices.xlsx using the to_excel method of the dataframe
object. On opening the Excel, we see that the table in the
figure below has beautifully been exported to an Excel
spreadsheet:
Figure 7.1: Table from PDF exported to Excel document using tabula Python
module
Figure 7.2: PDF file page rotated by 90 degrees clockwise using Python code
objPDFMerger.write(open(strMergedFileName, 'wb'))
On running the code above, we find that a new file named
Stock Prices Merged File.pdf, has been created and the
file contains two pages. The first page is the page of the
original file Stock Prices.pdf and the second page is the
page of the file Stock Prices Rotated File.pdf.
In the above code, we have used the PdfMerger object of
the PyPDF2 module to get our task done. We create a list
called listPDFs and enter the original file and the rotated
file as elements of the list. The code then loops through
each of these files in the list and appends every file to the
PdfMerger object. At the end, we use the write method of
the PdfMerger object to save the merged pages to a new
file.
Figure 7.3: Image of a bird that we would be using in our section to study
img = Image.open('Test_OCR.jpg')
imagetext = pytesseract.image_to_string(img)
print(imagetext)
Let us try to understand the code step by step:
1. In the first two lines, we import the libraries that we re
quire, namely pytesseract and Image.
import pytesseract
from PIL import Image
2. Next, we open the image using the open method of th
e image module using the below code:
img = Image.open('Test_OCR.jpg')
3. Thereafter, we write an important line of code where
we specify the path where the tesseract.exe file has
been installed. This step is required as the code needs
to know the path of the tesseract executable.
4. Finally, we use the image_to_string method of the P
ytesseract library and pass the image as an argumen
t. We store the value thus obtained in the variable im
g. We use the print command to print the text from t
he image in the console.
On running the code, we get the output as shown belo
w:
Learning to work with
Pytesseract
We have now successfully extracted text from an image. It
is important to note here that this image was a simple one
where black text had been written on a plain white
background. However, things might get complicated when
we have a colored background and text, resulting in a
different contrast. In such a scenario, we might have to
process the image a little bit for tesseract to recognize it.
We shall use the image Colored_Image_OCR.jpg for this
purpose, which resembles the one shown in the Figure 7.6
below:
Figure 7.6: Colored image to be applied to tesseract OCR
img = Image.open('Colored_Image_OCR.jpg')
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files
(x86)\Tesseract-OCR\tesseract.exe'
imagetext = pytesseract.image_to_string(img)
print(imagetext)
On running the code above, we find that we get a blank
output. This means that tesseract was not able to figure out
any text in the image above that contains a colored
background and colored text.
We shall process the image in order to make it recognizable
for tesseract using the code below:
import pytesseract
from PIL import Image
img = Image.open('Colored_Image_OCR.jpg')
img = img.convert('L')
img = img.save('Processed_Colored_Image_OCR.jpg')
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files
(x86)\Tesseract-OCR\tesseract.exe'
imagetext =
pytesseract.image_to_string(Image.open('Processed_Colore
d_Image_OCR.jpg'))
print(imagetext)
On the running the code above, we get the output below:
Learning to work with
Pytesseract
This shows that we have been able to successfully extract
text from the image! How were we able to do this? If we
read the code carefully, we observe that just two additional
lines of code as shown below were able to provide us the
image processing that we require.
img = img.convert('L')
img = img.save('Processed_Colored_Image_OCR.jpg')
Basically, we used the convert method and pass L as an
argument. Here, L stands for grayscale, so basically this
converts the image that we have to a grayscale image
'Processed_Colored_Image_OCR.jpg' where only the
luminance is retained. If we open the image
'Processed_Colored_Image_OCR.jpg', we find that it
looks the way shown in the Figure 7.7 below:
Figure 7.7: Original Image processed to Graysacle
Conclusion
With this chapter, we have covered almost all types of office
documents that we regularly work with on our machines. By
now, we should be comfortable with data extraction using
web scraping, automation using Excel spreadsheets,
working with text and tables in PDF files as well as detecting
text in images, all this using specific Python libraries to
serve the purpose.
In the next chapter, we shall cover another important
section that deals with the automation of regular desktop
operations like moving files and folders as well as mouse
movements and clicks. Let us now turn the page and
continue this journey with unhindered curiosity!
Introduction
This chapter begins with an introduction of the os and shutil
modules in Python which are powerful tools to automate the
process of reading, writing, and moving files and folders.
Various methods of the shutil module shall be discussed
along with their unique functionalities. The chapter further
continues with PyAutoGUI library and the PyWinAuto library
which are used to implement regular mouse operations on
the computer. Comparative advantages and disadvantages
of these libraries have been discussed. The chapter ends
with a project that would make the reader familiar with the
usage of these libraries.
Structure
The chapter covers the following topics:
• The os module in Python
• The shutil module in Python
∘ Copy and move a file using Shutil
∘ Move files based on extension using shutil
• Using the PyAutoGUI library
∘ Implementing basic mouse functions using
PyAutoGUI
∘ Implementing basic keyboard functions using
PyAuthoGUI
∘ Exploring message box
• Practical use case in Python
Objectives
By the end of this chapter, the reader would be able to
comfortably navigate between folders and perform common
tasks with files using the automation capabilities offered by
Python libraries. The reader would also be able to execute
common mouse functionalities using Python automation.
objCWD = os.getcwd()
objWorkbook = xw.Book()
objWorkbook.activate(steal_focus=True)
time.sleep(3)
pyautogui.typewrite('Hello World!',0.5)
pyautogui.hotkey('enter')
pyautogui.hotkey('up')
pyautogui.hotkey('ctrl','x')
pyautogui.hotkey('down')
pyautogui.hotkey('ctrl','v')
pyautogui.hotkey('esc')
Let us understand what we did in the code above:
1. We imported the required libraries as shown below:
import pyautogui
import xlwings as xw
import time
2. Thereafter, we opened a blank Excel workbook using t
he xlwings library and set the steal_focus property o
f the activate function to True in order to make sure
that our blank Excel workbook remains the active appl
ication on the screen in our window. The code below d
oes it for us:
objWorkbook = xw.Book()
objWorkbook.activate(steal_focus=True)
3. Next, we give a delay of 3 seconds using the time.sle
ep function in order to allow the Excel application suffi
cient time to open and remain activated on the windo
w screen. Below is the code:
time.sleep(3)
4. Now, we use the typewrite function to type the text t
hat we wish to enter in the active cell A1 using the co
de below:
pyautogui.typewrite('Hello World!',0.5)
Here, the second argument 0.5 is basically the time in
terval in seconds between the typing of successive ch
aracters. Here, we have specified a time interval of 0.
5 seconds.
5. Finally, we perform some common operations using ke
ys. We perform cut and paste on the text in order to m
ove it from cell A1 to A2 using the code below:
pyautogui.hotkey('enter')
pyautogui.hotkey('up')
pyautogui.hotkey('ctrl','x')
pyautogui.hotkey('down')
pyautogui.hotkey('ctrl','v')
pyautogui.hotkey('esc')
6. On running the code above, we find that a blank Excel
workbook opens post which, the text 'Hello World!' i
s entered in cell A1. Next, this text is cut and pasted i
n cell A2. We achieved this entire procedure without u
sing the Excel API to write values to cells. Instead, we
simulated the keyboard typing activity to achieve this.
The final output has been shown in the Figure 8.3 belo
w:
Figure 8.3: Cut Paste operation using typewrite and hotkeys functions
import pyautogui
print(pyautogui.position())
Output:
Point(x=192, y=1058)
2. Next, keep the Python editor window open and type th
e text Word in the search bar so that MS Word opens i
n the search results. We would want to capture the co
ordinates of any random point on this search result ic
on so that we could click on it during automation.
As shown in the Figure 8.7 below, we observe that the
search result for MS Word is located roughly at line nu
mber 10 in the editor. This should give us the position
of the MS Word application. Place the cursor at line 10
in the Python editor and run the code below to get the
coordinates:
import pyautogui
print(pyautogui.position())
Output:
Point(x=265, y=377)
Figure 8.8: Text being typed into word document using Python library
PyAutoGUI
Conclusion
We have achieved something unique in this chapter when it
comes to practical utility of our learning. We have
accomplished a significant feat in desktop automation,
mastering common tasks such as file and folder
management and mouse and keyboard operations. We have
also explored the creation of message boxes, an essential
skill in interactive automation. With this chapter, we
conclude our exploration of desktop automation. Hereafter,
we shall focus on applying salient aspects of desktop
automation to enhance our existing applications as well as
to build new applications that increase the efficiency of
manual processes.
As we close this chapter, we not only take with us these
valuable automation skills but also prepare to apply them in
even more transformative ways. The skills you have learned
here lay a foundation for the exciting journey ahead, which
leads us further into the realm of machine learning. This
rapidly evolving field is reshaping technology in the 21st
century, offering innovative solutions to complex problems.
The next chapter shall commence a new and exciting
journey of Machine Learning that is turning out to be the
most creative, innovative, and game-changing technology of
the contemporary era as well as the 21st century. So, let us
turn the page and dive into the world of Machine Learning,
where we will explore how to harness these powerful
algorithms to enhance the functionality of our applications
and make significant strides in automation and data
analysis!
Introduction
This chapter makes a deep dive into the detailed
implementation of machine learning algorithms. It
introduces the reader to the concepts of supervised learning
and unsupervised learning. Thereafter, the algorithms are
also categorized into classification and regression
algorithms. This chapter further delves deeper into the
types of supervised learning algorithms like linear
regression, logistic regression, k nearest neighbors, Naive
Bayes, support vector machines and decision trees. The key
concept of gradient descent has also been discussed in
detail. In order to understand unsupervised learning, the
crucial concepts of dimensionality reduction, principal
component analysis and linear discriminant analysis have
been given priority before covering the unsupervised
learning algorithms of k means clustering and hierarchical
clustering. An important topic of Python ML libraries has
been included in the end to acquaint the reader of the
plethora of functionality that Python provides in this vast
arena of machine learning. The chapter concludes with a
use case in Python that shall make the user conversant with
building his own machine learning application in the future
using Python.
Structure
The chapter covers the following topics:
• Implementing supervised machine learning algorithms
using Python
∘ Linear regression
• Key concepts in machine learning models
• Logistic regression
• K nearest neighbors
• Naive Bayes
• Support vector machines
• Decision trees
• Implementing unsupervised learning algorithms using
Python
∘ Dimensionality reduction
∘ Principal component analysis
∘ Linear discriminant analysis
∘ K means clustering
• Real world use case project
Objectives
In Chapter 3, Getting Started with AI/ML in Python, we went
through key concepts in machine learning and obtained a
theoretical understanding of machine learning algorithms. In
this chapter, we shall implement these algorithms using
Python, thus providing a launching pad to the reader for
independently creating and deploying customized machine
learning applications in the real world.
Linear regression
We have already covered the theory behind linear
regression in Chapter 3, Getting Started with AI/ML in
Python. Now, we shall implement it using Python on some
sample data. We know that the goal of linear regression is to
fit a line to a sample of points, such that we could minimize
the sum of the squares of the vertical distances of the
points from the line. The equation shall assume the form
below. The predicted value y shall be represented as:
Y = c0 + c1*x1 + c2*x2 + c3*x3 + ….. + cn*xn.
Here, the set {c0, c1, c2, c3,……,cn} are the co-efficient
and the value cn is the intercept. In linear regression, we
basically obtain the optimum values of the coefficients and
the intercept. By optimum values, we mean those set of
values that would minimize the sum of the squared vertical
distances between the actual points and the points
predicted by the line.
To implement linear regression using Python library Scikit-
learn, copy, and run the code below into the Python editor:
from sklearn import linear_model
reg = linear_model.LinearRegression()
reg.fit(training_data, labels)
print(reg.coef_)
print(reg.intercept_)
Output:
[3.]
7.105427357601002e-15
Let us understand the code above. First, we imported the
linear_model module from the scikit_learn library. Next,
we created an instance of the LinearRegression class and
stored it in a variable named reg.
We created a sample 2D array and stored it in variable
training_data. We should note that the training data here
would always be a 2D array because it contains the features
or numbers of parameters as columns and number of
samples as rows. Thereafter, we create a 1D array called
labels which is basically the outputs corresponding to these
inputs. Note that the number of elements in this array
should always be equal to the number of samples in the
training data array.
Next, we use the fit method which takes in the
training_data and labels variables as arguments. This
method basically creates a best fit line based on least
squares regression. At the end, we use the coef_ and
intercept properties of the reg object to get the
coefficients and the intercept respectively, of the regression.
In this case, since the training data consists of only one
point in every sample, we should be having just one
coefficient since we have just one parameter in the
equation. Here, the coefficient that we have obtained is 3,
which should also be obvious from the training_data and
the labels arrays where every element in the labels is thrice
that of the training data. The intercept is expected to be
zero since the equation y=3x perfectly fits this line. The
intercept that we have obtained is 7.105427357601002e-
15 which is very close to zero.
Now that we have been able to fit a straight line to this set
of data points, our next step is to use this straight line to
predict the output of a new set of data points for which we
do not have labels, basically where the output is unknown.
We use the predict method of the LinearRegression class
for this purpose. Copy and run the code below in the Python
editor:
# Importing necessary libraries
from sklearn import linear_model
import numpy as np
Logistic regression
In order to study logistic regression using Scikit-learn, we
would be using the Iris dataset that is inbuilt within Scikit-
learn. Let us familiarize ourselves with the Iris dataset
before proceeding with the tutorial.
The Iris dataset consists of information related to flowers of
three species with 50 samples of each. Hence the total
samples in this dataset are 150. The features used in this
dataset are Sepal Length (cm), Sepal Width (cm), Petal
Length (cm) and Petal_Width (cm) which are stored in
an array named data. There is another feature called
target which basically represents the species of the flowers
which are setosa, versicolor and virginica. Here, it is
numerically labelled as 0, 1 and 2 respectively and stored in
another array named target. Now, we shall store this
dataset in a pandas dataframe so that it enables better
visualization and analysis. Copy and run the code below into
the Python editor:
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
df = pd.DataFrame(iris.data,columns=iris.feature_names)
df['target'] = iris.target
print(df)
Here, we first import the load_iris class from the datasets
module of the Scikit-learn library and then instantiate it by
initializing it to variable iris. Thereafter, we read it into a
pandas dataframe and define the columns property with the
feature_names key of the Iris dataset. Next, we also add
another column for the target variable by assigning it the
value of the target key of the Iris dataset. On running the
code above, we get the output as shown below:
sepal length sepal width ... petal width targ
(cm) (cm) (cm) et
0 5.1 3.5 ... 0.2
0
1 4.9 3.0 ... 0.2
0
2 4.7 3.2 ... 0.2
0
3 4.6 3.1 ... 0.2
0
4 5.0 3.6 ... 0.2
0
.. ... ... ... ... ..
.
145 6.7 3.0 ... 2.3
2
146 6.3 2.5 ... 1.9
2
147 6.5 3.0 ... 2.0
2
148 6.2 3.4 ... 2.3
2
149 5.9 3.0 ... 1.8
2
[150 rows x 5 columns]
The entire Iris dataset is populated in the output with 150
rows and 5 columns, which includes the column for the
target variable.
Now, let us implement logistic regression with Scikit-learn
using Iris dataset. We have already been through the theory
behind logistic regression in Chapter 3, so we would not
revisit it here. Just to refresh the concepts, basically the
logistic regression algorithm tries to fit a sigmoid curve that
attempts to classify the dataset into two categories. Any
arbitrary point on the curve provides an estimate of the
probability of the point existing closer to either of the
categories.
For this tutorial, we shall use only the first two species of
flowers as we are classifying the data into two categories.
Using the code below, we first create the input data and the
labels from the Iris data:
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()
X = iris.data[:100]
Y = iris.target[:100]
From the code above, we see that we take the first 100 rows
of the data. Next, we train the model using the
LogisticRegression classifier as shown in the code below:
from sklearn.linear_model import LogisticRegression
LR = LogisticRegression()
LR.fit(X,Y)
Now that we have fit the sigmoid curve to the dataset using
the fit method, the next step is to use the curve to predict
the output for a new row of data as shown in the code
below:
X_predict = [6.3,2.5,4.3,1.5]
Y_predict = LR.predict([X_predict])
print(Y_predict)
Output:
[1]
We see that the predictor has predicted this dataset to be
belonging to category [1], which is basically the versicolor
flower. But how do we know whether the predictor correctly
predicts the output? We can test this by dividing the input
dataset into training data and testing data. We accomplish
this using the train_test_split method available in the
model_selection module of the Scikit-learn library as
shown in the code below:
from sklearn.model_selection import train_test_split
X_Train, X_Test, Y_Train, Y_Test = train_test_split(X,Y,
test_size=0.3, random_state=100)
Here, we observe that we assign a value of 0.3 to the
test_size parameter, which means that 30 percent of the
data would be allocated as a testing sample whereas the
remaining 70 percent would be retained as the training
sample.
An important parameter to note here is random_state.
Here, we have assigned it a value of 100. However, this
parameter could take any other arbitrary value like 345, 400
or 1043. The only thing that needs to be kept in mind here
is that one should not change the value of this parameter in
subsequent runs of the code if one wishes to retain the
same clustering of training and testing data. The way
train_test_split works is that it arbitrarily chooses data
points into training and testing data and clusters them
together based on the test_size parameter. If we change
the value of the random_state parameter or do not specify
any value for the parameter, the train_test_split method
shall differently allocate data points into the training and
testing data set even though it should still retain the test
size, provided it has not been changed by the user.
Finally, we train the model on the training data only and
thereafter test it on one of the sample rows from the testing
data using the code below:
LR.fit(X_Train,Y_Train)
Y_Predict = LR.predict([X_Test[23]])
print('X_Test:' + str(X_Test[23]))
print('Y_Test:' + str(Y_Test[23]))
print('Y_Predict:' + str(Y_Predict))
Output:
X_Test:[5. 3.5 1.6 0.6]
Y_Test:0
Y_Predict:[0]
We observe that the predicted value matches the actual
value which is 0, thus confirming the accuracy of prediction.
One could try this on other sample rows of the Iris dataset
to confirm whether the predicted value matches with the
actual value. Another useful exercise would be to choose
another combination of flowers and try implementing the
same algorithm.
In this way, we have implemented logistic regression using
the Scikit-learn library of Python.
In the next section, we shall implement another interesting
classification algorithm which is k nearest neighbors.
K nearest neighbors
To recap, k nearest neighbors is a classification algorithm
that associates an arbitrary point to the classes that it is
nearest to the base on the distance. We shall use the same
Iris dataset to implement this algorithm in Python. Copy and
run the code below in the Python editor:
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
Y = iris.target
knn = KneighborsClassifier(n_neighbors=12)
knn.fit(X_Train, Y_Train)
Y_Predict = knn.predict([X_Test[15]])
print('Y_Predict:' + str(Y_Predict))
print('Y_actual:' + str(Y_Test[15]))
Output:
Y_Predict:[1]
Y_actual:1
We observe that the predicted value of the algorithm
matches the actual target value of the selected sample row.
An important criterion here is the selection of the value of
the parameter k. A general thumb rule in choosing the value
of k is k = sqrt(N), where N is the number of samples in the
training dataset.
Naïve Bayes
Let us implement Naïve Bayes using Scikit–learn. We learnt
in Chapter 3, Getting Started with AI/ML in Python that
Naïve Bayes is a classification algorithm based on Bayes
theorem conditional probability, which is basically the
probability of an event that is conditional, on another event.
Copy and run the code below into the Python editor:
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
Y = iris.target
NB = GaussianNB()
NB.fit(X_Train, Y_Train)
Y_Predict = NB.predict([X_Test[5]])
print('Y_Predict:' + str(Y_Predict))
print('Y_Test:' + str(Y_Test[5]))
Output:
Y_Predict:[2]
Y_Test:2
We observe that the predicted value matches the actual
value in the testing data. We can also check the accuracy of
the model on all points of the testing data using the
accuracy_score function of the metrics module of the
Scikit-learn library. Copy and run the code below in the
Python editor:
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
Y = iris.target
NB = GaussianNB()
NB.fit(X_Train, Y_Train)
iris = load_iris()
X = iris.data
Y = iris.target
Decision trees
We have already studied the theory behind decision trees in
Chapter 3 - Getting Started with AI/ML in Python. We shall
now use the same Iris dataset for implementing decision
trees in Python.
Copy and run the code below in the Python editor:
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import accuracy_score
iris = load_iris()
X = iris.data
Y = iris.target
DT_Model = DecisionTreeClassifier()
DT_Model.fit(X_Train,Y_Train)
Y_Predict = DT_Model.predict(X_Test)
accuracy = accuracy_score(Y_Predict, Y_Test)
print(accuracy)
Output:
0.9555555555555556
From the accuracy score of 95%, we see that the decision
tree classifier has been fairly accurate in its prediction.
Dimensionality reduction
Dimensionality reduction is an important part of machine
learning. A dataset with too many features might render the
model complex, thus resulting in overfitting, where the
model might fit too tightly on the training data resulting in a
large variance on the testing data. Hence, it is essential to
reduce the number of features or dimensions in a dataset to
include only those that are required or may represent the
entire dataset. This technique of reducing the number of
features in a dataset in a way that the essence of the data is
still preserved is called as dimensionality reduction. There
are different ways in which dimensionality reduction can be
achieved:
• Feature selection: In this method, only a subset of f
eatures is selected from the main sample such that th
ey are most relevant to the problem that is being solv
ed.
• Feature extraction: In this method, the original feat
ures are transformed in such a way that the new featu
res formed are less in number and represent the origi
nal dataset. principal component analysis (PCA) a
nd linear discriminant analysis (LDA) are the popu
lar methods under this category. In the next sections,
we shall learn about these methods.
iris = load_iris()
X = iris.data
Y = iris.target
explained_variance = pca.explained_variance_ratio_
print(explained_variance)
Output:
[0.72201925 0.23393497 0.03914811 0.00489767]
Let us now try to understand the code above:
• First, we load the Iris dataset and split it into training
and testing data as we always did for all other algorith
ms before.
• Next, we scale the data using the StandardScaler cl
ass of the preprocessing module of the Scikit-learn li
brary. This is used to standardize the features to unit v
ariance by subtracting the feature mean and dividing
the value thus obtained by the standard deviation. Sta
ndard scaling makes the features comparable on a lev
el basis and eliminates possible discrepancies betwee
n features arising due to factors like units of measure
ment. We use the fit_transform method of the Stan
dardScaler class on the training dataset and the tra
nsform method on the testing dataset.
• Thereafter, we use the PCA() class of the decomposi
tion module of the Scikit-learn library. This method ta
kes in the parameter n_components as the first argu
ment which is the number of principal components. In
our code, we have not specified anything so it would t
ake all components by default. We use the fit_transfo
rm method of the PCA class on the training dataset a
nd the transform method on the testing dataset.
• Finally, we use the explained_variance_ratio_ prope
rty of the PCA class that provides a summary of the v
ariance that each principal component is responsible f
or. By observing the output, we understand that the fi
rst principal component is responsible for the maximu
m variance of around 73% and the second principal co
mponent is responsible for the second largest varianc
e of around 23%. Together, they account for around 9
6% of the variance. The remaining two components ac
count for minimal variance of close to 4% and 1% res
pectively.
iris = load_iris()
X = iris.data
Y = iris.target
explained_variance = lda.explained_variance_ratio_
print(explained_variance)
Output:
[0.99073265 0.00926735]
As observed from the output, the LDA technique has used 2
components, and the first component is responsible for 99%
of the variance whereas the second component is
responsible for only around 1% of the variance.
K means clustering
K means clustering is one of the commonly used
unsupervised learning algorithms that groups unlabeled
points in a dataset into similar clusters. The name k means
is derived from the fact that k is an arbitrary number of
clusters that are chosen at the beginning of the algorithm.
Thereafter, we randomly choose k arbitrary centroids for
each of the clusters and associate every point in the dataset
with its nearest cluster based on the Euclidean distance
between the point and the cluster centroid. The goal here is
to optimize the position of these centroids. Next, we take
the mean of each of the points in the dataset belonging to a
particular cluster and then update the cluster center
locations to coincide with the means. This process is
iterated until no further convergence is possible.
We shall use the same Iris dataset to implement k means
clustering in Python so that we can assess the accuracy of
clustering. To implement the algorithm in Python using
Scikit-learn, copy, and run the code below in the Python
editor:
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
iris = load_iris()
X = iris.data
Y = iris.target
sse = {}
for i in range(1, 11):
kmeans = KMeans(n_clusters=i,
init='k-means++',
max_iter=300,
n_init=10,
random_state=0)
kmeans.fit(X)
sse[i]=kmeans.inertia_
plt.figure()
plt.plot(list(sse.keys()), list(sse.values()))
plt.xlabel("Number of clusters")
plt.ylabel("SSE")
plt.show()
Figure 9.1 shows a graph that we obtain after running the
code. This graph is used to obtain the optimum number of
clusters:
Figure 9.1: Finding the optimum number of clusters using the elbow method
print(y_kmeans)
Output:
[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
11111111
11111111111110020000000000000
00000000
00020000000000000000000000202
22202222
22002222020202200222220222202
22022202
2 0]
We observe from the output that the algorithm has
classified the dataset into 3 clusters and numbered them as
0, 1 and 2.
df = pd.read_csv(r"IPL_Auction_2022_FullList.csv")
df = df[['Specialism','Test caps','ODI caps','T20
caps','IPL','Bid']]
We observe that the column 'Specialism' contains
information about whether a player is a batsman, bowler,
wicket keeper or all-rounder. Though helpful, we would need
to make sure that the values here are transformed to
numeric codes as the model would not be able to process
string values.
We get that done using the code below:
mapping_dictionary ={'ALL-ROUNDER' : 0, 'BATSMAN' : 1,
'BOWLER' : 2, 'WICKETKEEPER':3}
df['Specialism'] =
df['Specialism'].map(mapping_dictionary)
X = df.drop('Bid',axis=1).values
Y = df['Bid'].values
Our next step is to choose the right classification algorithm
for this dataset. Since we are interested in only two
outcomes 'Sold' or 'Unsold', we can use a logistic
regression model for this dataset. As usual, we split the data
into training and testing sets and then train the model on
the training sets.
Below is the implementation:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
• Accuracy =
= 72.88%
•
F1 Score = = 82.3
4%
In this model we see that precision, recall, accuracy, and F1
Score are all high, which is desirable. The low false negative
rate is also desirable. However, we see a high false positive
rate and consequently a low true negative rate arising due
to a high number of false positives. One should alternatively
try different machine learning algorithms and calculate
these metrics to assess the accuracy of prediction of the
model.
With this exercise, we end this chapter and a wonderful
journey through machine learning algorithms using the
Scikit-learn library of Python. It is important to note that
apart from Scikit-learn, there are many other libraries like
Keras, TensorFlow and PyTorch that are used for specific
applications. However, the focus of this chapter has been
Scikit-learn which provides the novice learner with the
launching pad to begin the journey with machine learning.
This chapter should give us a strong foundation to proceed
with the next chapter on deep learning.
Conclusion
This chapter has given an overview of the Python
implementation of machine learning algorithms for the
novice learner and paved the way for further curiosity and
exploration using Scikit-learn. It introduces the reader to the
powerful capabilities that Scikit-learn provides which could
be unleashed to implement interesting projects in machine
learning. Apart from the exercises covered in this chapter,
there are a variety of other modules in Scikit-learn that the
reader could explore and consequently unleash the machine
learning capabilities of Scikit-learn. Real world problems can
also get increasingly complex when the number of variables
in a dataset is large or when the dataset itself is huge. This
could require selective usage of dimensionality reduction
techniques. It is important to understand that no model is
perfect, and that model improvement is an iterative
procedure.
No discussion around intelligent automation would be
complete without exploring deep learning. In the
contemporary era, deep learning applications have entered
every single layer of technological innovation such that they
promise to dominate the 21st century. The recent hype
around Large Language Models (LLMs) like ChatGPT and
BARD have taken the world of generative AI by storm.
The next chapter will introduce the reader to key concepts
in deep learning and show how neural networks are
implemented in Python. Related concepts like natural
language processing would also be covered. Hence, without
any further delay, we should flip the page and move to the
next chapter!
Introduction
This chapter provides a background on deep learning and neural
networks along with an implementation of a neural network in
Python. It is important to note that the study of deep learning is
vast, and it is impossible to discuss every single topic within the
scope of this book. This chapter intends to inform the reader about
the role that Python plays in easing the process of deep learning
computations. The chapter revisits the concept of a neural
network, which forms the crux of deep learning. The chapter shall
also walk the user through the step-by-step process of building a
neural network in Python. The key concept of backpropagation
shall be covered with an actual example in Python by taking the
perceptron as a starting point. The chapter continues with the
discussion of useful applications of deep learning like natural
language processing (NLP) and computer vision also provide a
list of popular Python libraries that are used in deep learning
applications like NLTK, Spacy and OpenCV to name a few. The
chapter then concludes with the study of a real-world use case in
deep learning.
Structure
The chapter covers the following topics:
• Implementing a neural network in Python
• Backpropagation
• Popular Python libraries for deep learning
• Deep learning applications
• Natural language processing
• Practical use case in Python
Objectives
We have already seen the architecture of a neural network in
Chapter 3, Getting Started with AI/ML in Python, where we
observed that successive layers of nodes consisting of inputs,
weights and biases combined with activation functions are
iteratively responsible for the self-learning mechanism of a neural
network. We also went through different types of neural networks
like convolutional neural network and recurrent neural network.
Towards the end, we will cover theoretical aspects of various
applications in deep learning like computer vision, natural
language processing and long short-term memory. With this
background, we shall now plunge deep into the Python
implementation of a basic neural network and its outputs
transformed using activation functions. The process of
backpropagation has been covered in quite detail in the context of
a single perceptron. Later, the exciting foray of natural language
processing shall be touched upon which provides a glimpse of the
useful applications of deep learning. By the end of this chapter, the
reader shall also be comfortable in independently exploring and
implementing deep learning algorithms.
+ lst_inputs[2]*lst_weights[2]+ \
lst_inputs[3]*lst_weights[3]) + dbl_bias
print(dbl_output)
Output:
11.71
On printing the output, the value obtained is 11.71. In this way, we
can implement a perceptron in Python.
In a similar way, let us use Python to implement a basic multi-layer
neural network as shown in Figure 10.2 below:
Here, we have three nodes in the input layer. In the middle layer,
we have four nodes and in the output layer we have two nodes. Let
us first focus on obtaining the values y1, y2, y3 and y4 at the four
nodes of the hidden layer. As the hidden layer contains four nodes
and the input layer contains four nodes, we shall be having four
lists of three elements each as the weights. As the hidden layer
contains four nodes, we shall have a single list of four elements as
the list that represents the bias values. The code below represents
these entities as variables:
lst_inputs = [3, 10, 9.7]
y2 = lst_inputs[0]*lst_weights_2[0]+\
lst_inputs[1]*lst_weights_2[1]+\
lst_inputs[2]*lst_weights_2[2] + lst_bias[1]
y3 = lst_inputs[0]*lst_weights_3[0]+\
lst_inputs[1]*lst_weights_3[1]+\
lst_inputs[2]*lst_weights_3[2] + lst_bias[2]
y4 = lst_inputs[0]*lst_weights_4[0]+\
lst_inputs[1]*lst_weights_4[1]+\
lst_inputs[2]*lst_weights_4[2] + lst_bias[3]
print(hidden_layer_output)
Output:
[14.095, 13.271, 11.9, 17.375]
We observe that in this case, the output is a list, and all the values
match the ones that we had individually obtained in the earlier
exercise. Essentially, what we have done here is create a single
variable for weights and initialize it to be a list of four lists, each of
which contains three elements corresponding to the weights.
Thereafter, we use np.dot to obtain the dot product of the inputs
and weights and then we add the biases to this product. Is it not
simple to do?
Now that we have successfully been able to implement the output
of a hidden layer in an artificial neural network, let us move to the
next step that we had discussed in Chapter 3, Getting Started with
AI/ML in Python, which deals with processing the output with an
activation function. The most common activation function is a
rectified linear unit (ReLU) function that returns the same input
if the input is positive, else it returns a value of zero.
Mathematically, it can be expressed as below:
ReLU
f(X) = max(0,X)
Now, let us implement this activation function using Python. We
just observed that the output obtained for the four nodes of the
hidden layer is [14.095, 13.271, 11.9, 17.375]. For the node y1,
the output is 14.095. This is just one value of output that is
obtained for a particular combination of inputs and weights.
However, several different values of the output could be obtained
for various combinations of inputs and weights. Let us assume a
list of arbitrary values of outputs where 14.095 would also be one
of them and apply the ReLU function to check the final output.
Copy and run the code below in the Python editor:
import numpy as np
print(lst_Activated_Output)
Output:
[ 0.5 0. 14.095 10.5 0. 0. 0. 0.135]
We observe that all the values less than zero have been converted
to zero by ReLU.
Backpropagation
In this section, we shall study an important concept related to the
efficient training of neural networks which is backpropagation.
What backpropagation does is that it efficiently trains the neural
network by backwardly propagating the errors from the output
nodes to the input nodes. It does this by defining the loss function
and calculating the impact of each of the weights of the neural
network on the loss function by means of gradient descent. We
have already studied gradient descent in Chapter 9 - Intelligent
Automation Part 1: Using Machine Learning on machine learning.
Here, we shall see how backpropagation also uses gradient
descent to calculate the optimum values of the weights and biases
that could do the task which the neural network is intended to do.
In order to perform mathematical calculations required for
backpropagation, this section assumes that the reader is
conversant with basic concepts of calculus like derivatives, partial
derivatives and chain rule.
We are already familiar with the process of calculating the output
of a node from a neural network. We calculate the dot product of
the weights and the inputs and then add the bias term at the end.
This process is alternatively called as forward pass. For the
neural network shown in Figure 10.2 the output for node y1
calculated using weights w11,w12,w13 and inputs x1,x2,x3 by the
process of forward pass would be w11*x1 + w12*x2 + w13*x3 +
b1 where b1 is the bias. The outputs for the other nodes would be
calculated in a similar way.
Now, the outputs calculated in this way would be the predicted
outputs. However, when we train the neural network, we train it
with the intention of producing an expected output. A measure of
the gap between the expected output and the predicted output is
called as error and the goal of training the neural network is to
optimize the weights in such a way that they minimize this error.
The process of optimizing these weights is called as
backpropagation.
As the purpose of this book is simply to demonstrate the
significance of Python in simplifying computations, we would not
be going into the actual implementation of backpropagation in the
context of a fully connected neural network; else the calculations
might span many pages and go beyond the scope of this book.
However, we shall acquaint ourselves with the general process of
backpropagation in the context of a single perceptron so that the
reader could expand the idea to implement backpropagation to a
fully connected neural network.
As shown in the above Figure 10.3, the node gets four values of
inputs as x1, x2, x3 and x4 with weights w1, w2, w3 and w4
respectively. The bias value is b0. Hence, the nodal sum or the
output would be given by the dot product of the inputs and weights
added to the bias.
If z is the output, we have:
z = w1 * x1 + w2 * x2 + w3 * x3 + b0
Thereafter, we subject this output to an activation function. Let us
choose the ReLU activation function which is defined as:
ReLU(z) = max(0,z)
Hence, the final output of the node could also be expressed in the
following way:
output = ReLU(w1*x1 + w2*x2 + w3*x3 + b0)
Mathematically, this could also be expressed in the manner below:
output = ReLU(sum(w1*x1, w2*x2, w3*x3, b0))
As discussed earlier, the neural network tries to realize an
expected output. The gap between the expected output and the
predicted output is called the error or also the cost function. Let
us define a mean square error in this simple case which is given by:
2.
3.
lst_bias = [0.2]
As a part of backpropagation, we also need to define the expected
output in order to calculate the error as well as the learning rate in
order to perform gradient descent. We initialize the parameters to
the values as shown below:
output_E = 15.00
learning_rate = 0.01
Thereafter we perform backpropagation using the code below:
output = ReLU(Sum(lst_inputs,arr_weights,lst_bias))[0]
Error_Value = Error(output_E,output)
dError_value = dError(output_E,output)
dRELU_value = dRELU(Sum(lst_inputs,arr_weights,lst_bias))
dSum_w1_value = dSum_w1(lst_inputs,arr_weights)
dError_w1 = dError_value*dRELU_value*dSum_w1_value
Finally, we would calculate the new value of w1 using gradient
descent as shown in the formula below:
w1 = arr_weights[0]
w1_new = w1 - learning_rate*dError_w1
This value of w1_new thus obtained would be the new value that
we would be using for the weight w1 in performing the next
iteration of forward pass and backpropagation thereafter. These
iterations would continue until we come to a point that the error
does not reduce much any further and the weight seems to
converge to a particular value. It is important to note here that the
learning rate is an important parameter for performing
backpropagation as too high or too low learning rates might not
provide the appropriate optimized values for the parameters.
The entire code has been given below for reference. Copy and
paste the code in the Python editor:
#Backpropagation
import numpy as np
import matplotlib.pyplot as plt
def ReLU(z):
return np.maximum(0,z)
def Sum(lst_inputs,arr_weights,lst_bias):
return np.dot(arr_weights, lst_inputs) + lst_bias
def Error(output_E,output):
return (1/2)*(output_E-output) ** 2
def dError(output_E,output):
return output_E-output
def dRELU(z):
return 1. if z > 0 else 0.
def dSum_w1(lst_inputs,arr_weights):
return lst_inputs[0]
lst_inputs = [3,10,9.7,0.95]
output_E = 15.00
learning_rate = 0.01
output = ReLU(Sum(lst_inputs,arr_weights,lst_bias))[0]
Error_Value = Error(output_E,output)
dError_value = dError(output_E,output)
dRELU_value = dRELU(Sum(lst_inputs,arr_weights,lst_bias))
dSum_w1_value = dSum_w1(lst_inputs,arr_weights)
dError_w1 = dError_value*dRELU_value*dSum_w1_value
w1 = arr_weights[0]
w1_new = w1 - learning_rate*dError_w1
print(w1_new)
Output:
0.49012999999999995
[ 3, 1]])
However, that is not all! Just wait until we see the beauty of
a tensor in PyTorch. In the section on backpropagation, we ob
served how tedious it is to calculate the derivatives of certai
n expressions at points. But the hassle is about to end with t
he backward method of PyTorch! Copy and run the code bel
ow into the Python editor:
import torch
x = torch.tensor(5.0, requires_grad=True)
y = x**3 +7*x + 9
y.backward()
print(x.grad)
Output:
tensor(82.)
What we did is plain simple. We initialized a tensor x to the v
alue 5.0. Next, we created a dependent variable y as a functi
on of x given by the equation:
y = x3 + 7x + 9
We can algebraically obtain the derivative of this function wit
h respect to x as shown below:
Next, we assign x = 5 in the above expression to obtain the
value of the derivative as:
encoder = LabelEncoder()
Y = encoder.fit_transform(Y)
Next, we perform one hot encoding of this encoded target output.
One hot encoding is the process of transforming an output into a
vector representation. Referring to the earlier example, Dog would
be converted to {0, 0, 1}, Cat would be converted to {0, 1, 0} and
Mouse would be converted to {1, 0, 0}.
from sklearn.preprocessing import OneHotEncoder
onehot_encoder=OneHotEncoder(sparse=False)
Y=Y.reshape(len(Y), 1)
Y=onehot_encoder.fit_transform(Y)
Next, we split the data into training data and testing data using our
regular train_test_split function from the model_selection
module of the Scikit-learn library as shown in the code below:
X_Train, X_Test, Y_Train, Y_Test = train_test_split(
X, Y, test_size = 0.3, random_state=100)
We use a test size of 0.3 and specify a random state of 100.
This step takes us to the end of the data preparatory phase. To
revise what we just did, we have loaded the Iris data, separated
the target variable from the features, encoded the target and
thereafter one hot encoded the target. At the end, we split the data
into training and testing dataset.
Now comes the most interesting part, that of training the model!
However, which model do we choose? A self-made neural network!
Here comes TensorFlow to the rescue!
We use the Sequential method of the Keras module of
TensorFlow to create our neural network as shown in the code
below:
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])
As evident from the arguments to the Sequential method, we
have created two dense layers with ReLU activation and one layer
with Softmax activation function.
The next step after creating a neural network model is always to
compile it as shown in the code below:
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
This step specifies certain important parameters required in the
neural network. As a next step, which is the most important one,
we train the model using the fit method as shown in the code
below:
model.fit(X_Train, Y_Train, batch_size=50, epochs=100)
Here, the number of iterations shall correspond to the number of
epochs.
Next, we assess the accuracy of the model using metrics Loss and
Accuracy as shown in the code below:
loss, accuracy = model.evaluate(X_Test, Y_Test, verbose=0)
print('Loss:', loss)
print('Accuracy:', accuracy)
Finally, we would also be interested in knowing whether the actual
values match with the predicted values. We perform this analysis
using the code below:
Y_Pred = model.predict(X_Test)
actual = np.argmax(Y_Test,axis=1)
predicted = np.argmax(Y_Pred,axis=1)
print(f"Actual: {actual}")
print(f"Predicted: {predicted}")
While this has so far been a piecemeal exercise, it would be
advantageous to have the entire code as single pieces so that it
can be run at one go.
Copy and run the code below into the Python editor:
#Practical Use Case in Python
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
import tensorflow as tf
from tensorflow.keras import layers
import pandas as pd
import numpy as np
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.utils import to_categorical
iris = load_iris()
X = iris.data
Y = iris.target
encoder = LabelEncoder()
Y = encoder.fit_transform(Y)
onehot_encoder=OneHotEncoder(sparse=False)
Y=Y.reshape(len(Y), 1)
Y=onehot_encoder.fit_transform(Y)
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
Y_Pred = model.predict(X_Test)
actual = np.argmax(Y_Test,axis=1)
predicted = np.argmax(Y_Pred,axis=1)
print(f"Actual: {actual}")
print(f"Predicted: {predicted}")
Output:
Epoch 1/100
3/3 [==============================] - 1s
6ms/step - loss: 3.1403 - accuracy: 0.3238
Epoch 2/100
3/3 [==============================] - 0s
3ms/step - loss: 2.8077 - accuracy: 0.3524
Epoch 3/100
3/3 [==============================] - 0s
3ms/step - loss: 2.6317 - accuracy: 0.3714
Epoch 4/100
3/3 [==============================] - 0s
2ms/step - loss: 2.4918 - accuracy: 0.4476
Epoch 5/100
3/3 [==============================] - 0s
3ms/step - loss: 2.3969 - accuracy: 0.4381
...
Epoch 99/100
3/3 [==============================] - 0s
2ms/step - loss: 0.3325 - accuracy: 0.9238
Epoch 100/100
3/3 [==============================] - 0s
3ms/step - loss: 0.3292 - accuracy: 0.9238
Loss: 0.30189675092697144
Accuracy: 0.9333333373069763
2/2 [==============================] - 0s
2ms/step
Actual: [2 0 2 0 2 2 0 0 2 0 0 2 0 0 2 1 1 1 2 2 2 0 2 0 1 2 1 0
121120010
1 2 2 0 1 2 2 0]
Predicted: [2 0 2 0 2 2 0 0 1 0 0 2 0 0 2 1 1 1 2 2 1 0 2 0 1 1
10121120010
1 2 2 0 1 2 2 0]
From the output, we observe that model has performed brilliantly
with an accuracy of 93.33 %. Note that the loss and accuracy
values might change every time we run the code and slightly differ
from the ones that have been obtained above. We also observe
that the actual output closely matches with the predicted output,
thus successfully justifying our use of this neural network to solve
the classification problem.
Conclusion
Nobody can deny that neural networks and deep learning require
extensive attention to detailed concepts and patience to master.
With myriad of Python packages, one could simply imagine the
immense possibilities to create and train computationally efficient
neural networks that solve various complex problems. These
neural networks along with their intricate concepts and substantial
learning curve, represent a challenging yet immensely rewarding
field! It would be worthwhile to say that this chapter has been the
most intensive in terms of exploration and application since it has
been a mix of both theory and programming examples.
Nevertheless, smooth seas never made skillful sailors, more so
appropriate in the context in the sea of deep learning! Through this
chapter, we have only scratched the surface of what is possible
with the numerous Python tools available for creating efficient
neural networks. Our journey through theory and practical
examples has been a testament to the fact that mastering deep
learning requires both dedication and practice. Even though we
have made a minuscule attempt in this chapter to introduce the
reader to deep learning and arouse interest in the territory of
Python implementation in deep learning, the next bold step of
regular practice, expansion and consolidation has been handed
over to the able hands of the enthusiastic reader!
Previous few chapters were slight deviations from our usual
purview of desktop and file automation because they intended to
cover core theory and related concepts in the scientific domain of
artificial intelligence, machine learning and deep learning. Now, we
shall be shifting our focus back to more application-centric themes.
In the upcoming chapter, we will delve into business process
workflow, exploring key concepts like orchestration and Python’s
pivotal role in automating business processes. Hence, without
further delay, let us turn the page to the next chapter!
Introduction
We have had sufficient discussions on the topic of
automation in the last several chapters of this book. Within
desktop automation we covered file and folder navigation,
we covered screen automation; we went through the
intricacies of automating processes in Excel documents, PDF
documents as well as in image documents. We also covered
automation through the web by getting familiar with the
process of data gathering using scraping techniques and
thereafter explored the arena of automated messaging to
individuals and groups. The hallmark of all the automated
processes that these chapters discussed was the way in
which Python assists in eliminating manual intervention in
them thus playing a crucial role in enhancing an
organization’s efficiency. However, we need to consider the
fact that these processes do not run individually in any
organization. The process workflow of any business or
organization is composed of many such processes that run
in a particular sequence and get triggered conditionally at
appropriate times. They might also run in tandem with other
processes. This is exactly where the need arises to have a
framework that can sequentially and efficiently align these
processes along the business workflow. This framework is
called orchestration. In this chapter, we will introduce you to
orchestration, underscoring how Python can be leveraged to
implement and enhance this vital aspect of business
process management.
Structure
The chapter covers the following topics:
• Understanding a business process workflow
• Introduction to orchestration
• Automation versus orchestration: Differences
• Orchestration platforms available in market
• Achieving orchestration with Python
∘ Prefect
∘ Luigi
• Practical use case in Python
Objectives
By the end of this chapter, the reader shall be conversant
with the concept of orchestration in a business process
workflow and learn to keep in mind the broader picture of
orchestration while automating regular tasks. A summary of
the available orchestration frameworks in market would also
be presented.
The reader shall also get a fair understanding of
implementing orchestration in a workflow using Python
since this chapter shall try at illustrating this concept
through specific examples in Python. The Python modules
that this chapter shall cover are the prefect and the luigi
modules. It shall also compare the two, based on relative
advantages and disadvantages.
As with any other chapter, this chapter shall end with a
practical use case in Python enabling the reader to apply
salient concepts from the learnings gained in the chapter.
Introduction to orchestration
We already had a look at the general concept and idea of a
business process workflow where individual processes are
sequentially connected to each other and triggered at the
appropriate time. Each of these individual processes might
themselves contain a series or a sequence of automated
steps. The process that manages these set of individual
tasks to create an amalgamated workflow is called as
orchestration.
In orchestration, multiple technologies and platforms might
be coordinated together based on specific rules and
circumstances. Orchestration also takes care of alternate
decisions that would need to be made during changing
circumstances. This could extend from simple to complex
systems. A typical example of orchestration is the creation
of an app and deployment of the app to connect to multiple
users. This could involve the triggering of multiple processes
within the network. Almost all large projects that get
deployed on the cloud involve orchestration of some sort.
It is important to note here that in orchestration, it is not
necessary that all the constituent tasks or processes are
themselves all automated processes. While most of them
might be automated, some of them might even be manual.
Conceptually, orchestration is simply the process of
effectively coordinating between individual tasks that
constitute a business process workflow. In the present day,
this shall be automated due to the availability of numerous
platforms and technology to do so. In this book, we shall be
studying this orchestration in the context of our Python
scripts. Hence, we shall be performing orchestration of only
those automated scripts that we regularly execute in
Python.
Having understood the concept of orchestration, let us now
take a quick glance of the salient differences between
automation and orchestration as that shall help us
consolidate our understanding further. Even though
automation and orchestration are complementary to each
other, there lie certain subtle differences between them.
The next section shall enlist and elaborate on these
differences.
Automation Orchestration
Number of Automation of a Orchestration is
tasks task is the the coordination
process where (mostly
human automated) of
intervention is multiple
eliminated from automated
a single task. tasks (few
might be
manual).
Complexity Automation is Orchestration is
relatively complex
simple as it is because it
based on requires
predefined rules coordination
for a single and adaptation
task. between
multiple tasks
in a workflow.
Deployment An automated An orchestrated
solution might solution usually
be deployed in- involves
house or locally deployment on
on a desktop. the network.
Typical use IT processes, DevOps, Cloud
cases financial
processes,
designing
processes
Table 11.1: Primary differences between automation and orchestration
Prefect
The Python prefect library enables one to define a
sequence of tasks in a flow and trace dependencies among
tasks. An example would make this clear. Let us take a very
simple case of performing mathematical calculations by
writing functions. The steps that we shall perform would be
as below:
1. Take any two integers with arbitrary values.
2. Add them together.
3. Multiply the sum thus obtained, by 2, consequently m
ake the sum amount double.
Let us try to program this procedure in Python. We would
basically have to define separate functions for adding the
numbers to get the sum and thereafter doubling the sum to
get the result. Let us arbitrarily select 5 and 10 as the two
numbers:
#Define the functions
def Add(intNum_1,intNum_2):
return intNum_1 + intNum_2
def Double(intNum):
return 2*intNum
intNum_1 = 5
intNum_2 = 10
Sum = Add(intNum_1,intNum_2)
Doubled_Sum = Double(Sum)
print(Doubled_Sum)
Output:
30
As observed, the program gives us the expected output of
30 which is obtained by adding 5 and 10 which gives 15
and then doubling the sum which gives 30. Now let us try to
achieve the same result by visualizing the same program as
a workflow using the prefect library of Python.
Follow the steps below to start using prefect:
1. To start using prefect, the first thing that needs to be
done is the installation of the library. It is important to
note that we would be using the initial version of pref
ect which is prefect==1.0.0 while studying example
s in this chapter. The latest version of prefect is an on
going project and has syntax and methods which are
different from the earlier version. So, while installing,
we would install the earlier version using the comman
d below:
pip install prefect==1.0.0
2. Next, we need to ensure that graphviz package has b
een installed for our respective machine and operatin
g system from the location below:
https://graphviz.gitlab.io/_pages/Download/Dow
nload_windows.html
3. While installing, select the option to add the location o
f the bin directory of the Graphviz installation to the
User Path when the installer prompts to do so. After i
nstalling, add the path to the dot.exe file within the b
in folder to the System path as well.
4. Finally, we would need to install the prefect[viz] Pyth
on package for visualizing the flow at the end. Install t
he package using the command below:
pip install "prefect[viz]"
5. We are all set now to use prefect. We would require t
he task and Flow modules from the prefect library w
hich we obtain by importing them using the line of co
de below:
from prefect import task, Flow
6. The first thing we would need to understand is that th
e functions that we defined in the program for summi
ng and doubling would now have to be defined as task
s. Even the print operation that we performed at the
end of the program to print the final answer would ne
ed to be defined in a separate function and denoted a
s a prefect task. The process to do this is simple. We r
etain the definition of the functions as they are. We si
mply add a line @task prior to the declaration of the f
unctions as shown in the code that follows:
@task
def Add(intNum_1,intNum_2):
return intNum_1 + intNum_2
@task
def Double(intNum):
return 2*intNum
@task
def Print_Value(strValue):
print(strValue)
7. Now that we have defined all our tasks, our next step
would be to execute our procedure sequentially. We d
o this by initializing the Flow module to a variable na
med flow and including our procedure in a with block
of the flow. The code below would make this concept c
lear:
with Flow('Mathematical-Operations') as flow:
intNum_1 = 5
intNum_2 = 10
Sum = Add(intNum_1,intNum_2)
Doubled_Sum = Double(Sum)
Print_Value(Doubled_Sum)
8. This defines the entire flow. Our final step would be to
execute the flow by using the run method of the Flo
w module as shown in the code below:
flow.run()
9. After the flow is executed, we would be interested in v
isualizing the flow graphically which would enable us t
o trace dependencies similar to a flowchart. We do thi
s using the visualize method as shown in the code be
low:
flow.visualize(filename="Mathematical Operations")
10.Here, we observe that the visualize method allows us
to pass an argument named filename where we speci
fy the name of the file as "Mathematical Operation
s" where the flow would be saved in a graphical forma
t in the same directory where the Python script is loca
ted or the home directory of the Python script.
After running the entire code sequentially in a Python
editor from all the steps mentioned above, we get the
output in the console as shown below:
Output:
[2023-07-22 16:19:05+0530] INFO - prefect.Flo
wRunner | Beginning Flow run for 'Mathematica
l-Operations'
[2023-07-22 16:19:05+0530] INFO - prefect.Tas
kRunner | Task 'Add': Starting task run...
[2023-07-22 16:19:05+0530] INFO - prefect.Tas
kRunner | Task 'Add': Finished task run for task
with final state: 'Success'
[2023-07-22 16:19:05+0530] INFO - prefect.Tas
kRunner | Task 'Double': Starting task run...
[2023-07-22 16:19:05+0530] INFO - prefect.Tas
kRunner | Task 'Double': Finished task run for ta
sk with final state: 'Success'
[2023-07-22 16:19:05+0530] INFO - prefect.Tas
kRunner | Task 'Print_Value': Starting task run...
30
[2023-07-22 16:19:05+0530] INFO - prefect.Tas
kRunner | Task 'Print_Value': Finished task run f
or task with final state: 'Success'
[2023-07-22 16:19:05+0530] INFO - prefect.Flo
wRunner | Flow run SUCCESS: all reference task
s succeeded
11.We observe that the event of running every function
within the flow is recorded sequentially with a date an
d time stamp similar to the log functionality. We also o
bserve that the output value of 30 has been printed w
ithin the console.
12.We would now want to observe the graphical flowchar
t of the workflow. If we go to the home directory of the
Python script, we will find that a file named "Mathem
atical Operations.pdf" has been created. On openin
g the file, we observe a flowchart as shown in Figure 1
1.2 below:
@task
def Add(intNum_1,intNum_2):
@task
def Double(intNum):
return 2*intNum
@task
def Print_Value(strValue):
print(strValue)
intNum_1 = 5
intNum_2 = 10
Sum = Add(intNum_1,intNum_2)
Doubled_Sum = Double(Sum)
Print_Value(Doubled_Sum)
flow.run()
flow.visualize(filename="Mathematical Operations")
Hope this tutorial on prefect has been an interesting one!
We have taken a very simple example as the main aim was
to clarify concepts on the execution of a workflow. An
interested reader should revisit earlier Python exercises
from this book and try converting them into prefect
workflows. As the workflows get more complicated, it surely
helps to observe the dependencies graphically in a prefect
flowchart. Now, let us explore another interesting Python
module called Luigi.
Luigi
Apache Luigi is a workflow management system with a
Python based API that enables complex data pipelines.
1. The first step that needs to be done prior to using lui
gi is the installation of the package using the comman
d below:
pip install luigi
We shall use the same example of mathematical oper
ations to understand how luigi works.
The fundamental module of the luigi package is the T
ask module which we import as a first step using the
code below:
import luigi
from luigi import Task
The way luigi works is by creating a class that takes i
n the Task argument, defining all the functions requir
ed within that class and including a run method withi
n the class that executes the workflow. We first define
the class as named Mathematical_Operations as sh
own below:
class Mathematical_Operations(Task):
We observe that we have defined the class that takes
in the Task object as the argument enabling us to per
form luigi activities within the class.
2. Next, we define two parameters intNum_1 and intN
um_2 within the class and initialize them to luigi.Par
ameter(). This would enable us to initialize the class
with dynamic parameters similar to what a constructo
r does within a class as shown below:
class Mathematical_Operations(Task):
intNum_1 = luigi.Parameter()
intNum_2 = luigi.Parameter()
3. Next, we shall define our regular Add function within t
he class that would utilize the parameters intNum_1
and intNum_2. We also define the Double function t
hat doubles the value returned to it. The updated clas
s would be the one shown below:
class Mathematical_Operations(Task):
intNum_1 = luigi.Parameter()
intNum_2 = luigi.Parameter()
def Add(self,intNum_1,intNum_2):
return intNum_1 + intNum_2
def Double(self,intNum):
return 2*intNum
4. Now we come to the most important part which is the
run function. It is this function that would execute the
procedure within our workflow using all the functions t
hat we defined within the class. The entire class along
with the run function is shown below:
class Mathematical_Operations(Task):
intNum_1 = luigi.Parameter()
intNum_2 = luigi.Parameter()
def Add(self,intNum_1,intNum_2):
return intNum_1 + intNum_2
def Double(self,intNum):
return 2*intNum
def run(self):
Sum = self.Add(self.intNum_1,self.intNum_2)
Doubled_Sum = self.Double(Sum)
print(Doubled_Sum)
5. Finally, at the end we call the build function of the lui
gi module which executes the workflow as shown belo
w:
luigi.build([Mathematical_Operations(intNum_1=5,in
tNum_2=10)],local_scheduler=True)
We pass two arguments into the build function. First
argument is the instance of the class
Mathematical_Operations and the second argument is
local_scheduler = True, which indicates that we would
want to run this task locally without connecting to the luigi
server.
The entire piece of code has been shown below for
reference. Copy and run the code in the Python editor:
import luigi
from luigi import Task
class Mathematical_Operations(Task):
intNum_1 = luigi.Parameter()
intNum_2 = luigi.Parameter()
def Add(self,intNum_1,intNum_2):
return intNum_1 + intNum_2
def Double(self,intNum):
return 2*intNum
def run(self):
Sum = self.Add(self.intNum_1,self.intNum_2)
Doubled_Sum = self.Double(Sum)
print(Doubled_Sum)
luigi.build([Mathematical_Operations(intNum_1=5,intNum_
2=10)],local_scheduler=True)
On running the code, we get the output as shown below:
===== Luigi Execution Summary =====
30
We observe that the value 30 has been populated in the
output console which is the value that we expected to
achieve in the result. The summary gives an indication of
the tasks that have run. Since we had just one class
Mathematical_Operations here, the summary mentions
just the particular task.
The complete output in the console would also consist of the
entire log with DEBUG and INFO statements as well as
username details which have not been shown here.
In this way, we have seen the basic framework of a luigi
module. The official documentation of luigi would discuss
several more useful features that would enable one to
incorporate more complex pipelines into it.
We have now seen both prefect and luigi modules and
observed through examples how they both enable the
incorporation of workflow management functionalities. We
shall move towards the end of this chapter now with a
practical use case in Python.
Conclusion
With this chapter, we are done deal that managing
workflows should no more be a hassle to the Python
programmer. The modules prefect and luigi that we
discussed in this chapter should be a starting point as well
as a launching pad for implementing simple and complex
workflows. At the end, more complexity means more
challenges which also means additional elements of
creativity! Workflow management has never been more
exciting, thanks to Python!
In the next chapter, we shall slowly begin our transition to
real world use cases. We have already been implementing
glimpses of those in all our chapters in the form of practical
Python use cases. However, the topic of the next chapter is
Hyperautomation which is the hallmark of the contemporary
technological landscape. The name itself slightly provides a
hint that the topic shall not leave any concept unattended,
whether it is process automation or workflow orchestration
or artificial intelligence or machine learning!
Hence, without further delay, let us turn the page!
Join our book’s Discord space
Join the book’s Discord Workspace for Latest updates,
Offers, Tech happenings around the world, New Release and
Sessions with the Authors:
https://discord.bpbonline.com
Chapter 12
Hyperautomation
Introduction
Until now, we have explored the intricacies of automation
when it comes to desktop, web, file, folder, data science
algorithms or workflow automation. Our journey in
automation until now has culminated into workflow
automation in Chapter 11, Automating Business Process
Workflows, where we have not just automated individual
processes but incorporated sequential procedures in a
workflow using Python libraries.
All these frontiers that we explored have been wonderful in
their own ways. However, when used in tandem with
cognitive intelligence provided by machine learning,
robotic process automation (RPA) and other AI tools,
they have the potential to make a remarkable impact on the
entire system. That is exactly what hyperautomation is!
This chapter shall introduce the reader to the concept of
hyperautomation, expand on the idea further and thereafter
cover salient examples in Python that illustrate real world
use cases.
Even though the examples might come under the previously
studied topics of natural language processing (NLP) and
robotic process automation, it would be an altogether novel
experience revisiting the topics from a different perspective
and under a different section.
Structure
The chapter covers the following topics:
• Defining hyperautomation: What it is and why it matte
rs
∘ The hyperautomation cycle: Key steps and
processes
∘ Exploring typical use cases for hyperautomation
∘ Enhancing document understanding with optical
character recognition
• Implementing conversational agents: The role of chat
bots
• Advancing efficiency with robotic process automation
• Navigating the challenges of hyperautomation
• Practical use case in Python
Objectives
By the end of this chapter, the reader would appreciate the
term hyperautomation as something which contains the
same technologies which we covered in earlier chapters but
have the potential to create something new when used in
tandem.
This chapter shall revisit some of the previously covered
topics like natural language processing, optical character
recognition and robotic process automation from a different
perspective and under a broader horizon of
hyperautomation and shall also attempt to tackle certain
previously uncovered sections related to these technologies.
The concept, design, and application of a conversational
agent or chatbot would be explored using Python, thus
adding a new dimension to the topic of natural language
processing. Applications like Email filtering would be
discussed, thus doing further justice to the usage of
machine learning for hyperautomation.
As with any other chapter, this one would also end with a
practical use case in Python of an interesting example in
hyperautomation.
print(text_output)
The entire code has been shown below for reference. Copy
and run the code in a Python editor and observe the output
in the console:
from pdf2image import convert_from_path
import pytesseract
images = convert_from_path(str_pdf_path)
print(text_output)
Output:
Below is a table showing the price of five stocks
A,B,C,D, and E from January to December. The first
column
contains the stock names and the remaining columns
contain the respective values of the stock from
January to December.
Stocks | Jan Feb Mar Apr May Jun Jul Aug Sep Oct
Nov Dec
A 357 457 187 831 779 338 129 508 407 748 511 609
C 60 64 54 93 87 74 96 92 83 85 70 88
obj_ocr = TesseractOCR(lang="eng")
Next, we use the extract_tables method of the obj_pdf
object which extracts all tables within the document using
OCR. Here, we pass the obj_ocr object as an argument to
the ocr parameter of the extract_tables method:
obj_pdf_tables = obj_pdf.extract_tables(ocr=obj_ocr)
Thereafter, we use the to_xlsx method of the obj_pdf
object to export the table to an Excel worksheet as shown in
the code below:
obj_pdf.to_xlsx("Extracted_Tables.xlsx", ocr=obj_ocr)
Here, we observe that the to_xlsx method as well requires
an input to the ocr parameter that we provide as the
obj_ocr object. The name of the output file that we require
is "Extracted_Tables.xlsx" and has been passed as the
first argument to the to_xlsx method.
The entire piece of code has been included on the next page
for reference. Copy and run the code in the Python editor
and observe that an Excel file named
"Extracted_Tables.xlsx" has been created in the same
directory where the code has been saved.
from img2table.document import PDF
from img2table.ocr import TesseractOCR
["Hello %1",]
],
[
r"(I'm|good|good,|I am)",
["How can I help you?\nPlease select one from
the following:" \
"\n1)New Software Installation\n2)Issue with
Existing Software",]
],
[
r"(.*) Installation",
["Could you please mention the name of the
software? Please type 'Software:'" \
"before mentioning the software name.
Thanks!",]
],
[
r"(.*) Existing",
["Please select the issue from the following:" \
],
[
r"Software:(.*)",
["Thanks for your patience. I am assignining this
ticket to someone from " \
"the IT team who shall help you install %1.Is
there anything else I could help you with?",]
],
[
r"No",
["Thank you. Have a great day!",]
],
]
The reader is advised to spend some time observing t
he statements and responses in this list so that could
provide a better idea of how the chatbot produces ans
wers to the questions. The reader is assumed to be fa
miliar with regex in Python.
Finally, we use the Chat class to instantiate the chatb
ot by passing pairs and reflections as arguments. At
the end, we use the converse method of the Chat cla
ss to start the conversation.
We also print an introductory statement prior to instan
tiating the Chat class in order to begin the conversati
on on behalf of the chatbot.
print("Hi, I'm your new chatbot friend and I'm here to
help you! May I know your name?")
pairs = [
[
r"My name is (.*)",
["Hello %1",]
],
[
r"(I'm|good|good,|I am)",
["How can I help you?\nPlease select one from
the following:" \
"\n1)New Software Installation\n2)Issue with
Existing Software",]
],
[
r"(.*) Installation",
["Could you please mention the name of the
software? Please type 'Software:'" \
r"(Version|Uninstall)",
["Thanks for your patience. Someone from the IT
team " \
"would reach out to you to resolve your issue.Is
there anything else" \
"I could help you with?",]
],
[
r"Software:(.*)",
[
r"No",
["Thank you. Have a great day!",]
],
]
pytesseract.pytesseract.tesseract_cmd = r"C:\Program
Files\Tesseract-OCR\tesseract.exe"
image = 'Captured_Image.png'
print(text)
Note that we have used config='--psm 11' in the last
argument of the image_to_string method of the
pytesseract library. Please refer to the tesseract manual
link provided in Chapter 7, Working with PDFs and Images
that discusses in detail the config types.
The entire code has been provided below for reference.
Copy and run the code in a Python editor:
import rpa as r
r.init()
r.url('https://c4.wallpaperflare.com/wallpaper/734/647/66
1/quote-motivational-wallpaper-preview.jpg')
r.snap('page', 'Captured_Image.png')
r.wait(5)
r.close()
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program
Files\Tesseract-OCR\tesseract.exe"
image = 'Captured_Image.png'
text = pytesseract.image_to_string(image, lang='eng',
config='--psm 11')
print(text)
Output:
DO YOUR BEST
AND
FORGET THE REST
®
We find that pytesseract has given us the output that we
require! With this, we end the use case and the chapter!
Conclusion
As interesting as this chapter is, it did not introduce any
fundamentally new concept. In fact, the central message of
this chapter has been that a completely new territory called
hyperautomation could be brought into existence simply
by applying the same fundamental technologies at salient
points in the business workflow, thus refining the workflow
with cognitive intelligence and automation. This chapter
also discussed the challenges that an organization would
need to address while incorporating hyperautomation.
In the next chapter, we shall revisit the RPA tool UiPath once
again, this time with Python as the center of focus! The
combination of UiPath with Python provides wonderful
flexibility to the user which would be the primary area of
discussion in the next chapter. So, without wasting anymore
time, let us turn the page and move to the next chapter!
Chapter 13
Python and UiPath
Introduction
The fact that we have previously covered the foundations of
robotic process automation (RPA) in Chapter 2, RPA
Foundations, should give us the prerogative to explore the
role of Python in enhancing the efficiency and flexibility of
RPA tools like UiPath. This chapter shall deal with this topic
in depth where the inclusion of Python scripting in RPA
enhancement would be demonstrated through a detailed
step by step guided walkthrough.
Even though RPA tools like UiPath provide specialized
versions that do not require programming for those
professionals who are not keen to program themselves, this
chapter would focus on the customizable version of RPA
software that enables one to include a programmable script
to enhance the efficiency and operations of the workflow.
In this chapter, we would discuss the different activities
within UiPath that constitute the sequence that enables
Python integration with UiPath. Each of these activities
would have their own unique attributes which we would
glance through in detail.
A foundation of Python integration with UiPath further fuels
the efficient implementation of hyperautomation, a concept
that we studied in Chapter 12, Hyperautomation earlier.
Structure
The chapter covers the following topics:
• Setting up the Python environment in UiPath
• Exploring Python activities in UiPath
• Creating the Python script
• Integrating Python with UiPath
Objectives
This chapter assumes that the reader is already familiar and
comfortable navigating basic functionalities in UiPath.
Although Chapter 2, RPA Foundations has provided an
overview and summary of different RPA functionalities, it
would be helpful to get acquainted with primary concepts
related to UiPath prior to beginning this chapter, as the
focus of this chapter would be the integration of Python with
UiPath.
By the end of this chapter, the reader would be comfortable
invoking Python scripts within the UiPath workflow, thus
getting a glimpse of the power of Python in enhancing
UiPath programs.
This chapter would be heavy with images! This is not
surprising since it would require the inclusion of captured
screenshots from every instance of the process. The step-
by-step guided navigation with elaborate images would
make it easy for the reader to understand the salient steps
in the procedure.
Setting up the Python environment in
UiPath
In order to use Python scripts in the UiPath environment, we
would need to perform a few settings as described in the
below steps and figures:
def Square(intNum):
intSquare = intNum*intNum
return intSquare
def Add(intNum1,intNum2):
Figure 13.23: Using Add method in the Invoke Python Method activity
20.As we are adding two numbers, we mention the value
in the attribute InputParameters as New List(Of O
bject)({100,150}), which would add the numbers 10
0 and 150.
21.Similarly, in the Name attribute, we mention the nam
e of the method as "Add".
22.Next, we save the sequence and run it in a similar wa
y as we ran it before. We again get the Output Mess
age Box as shown in Figure 13.24 below that populat
es the expected output of 250 which is the sum of the
two numbers 100 and 150:
With this simple exercise, are all done exploring the basic
infrastructure required to integrate Python with UiPath.
We should now be in a strong position to build more
complex applications that selectively utilize existing Python
scripts in a UiPath sequence, thus making the best use of
the functionalities of both Python and UiPath.
It is left to the choice of the enthusiastic programmer to
explore and experiment the inclusion of Python script in
other salient UiPath activities.
Hence with this, we conclude this chapter!
Conclusion
We are now done and dusted with the integration of UiPath
and Python! This chapter has been unique in few aspects.
Firstly, a single section has spanned multiple pages due to
its inherent nature of being a procedural walkthrough.
Secondly, the first aspect being attributed to the fact that
every stage in the procedure required detailed capturing of
important screenshots, thus making the entire chapter
image heavy. As mentioned previously in the introduction,
the inclusion of Python in the arena of Uipath greatly
accelerates the hyperautomation journey by enabling a
smooth utilization of the power of Python in the foray of
robotic process automation.
In the next chapter, we shall learn about the accessories
that are required while working on automation projects. The
chapter will be about architecting automation projects,
which would introduce us to the concept of a virtual
environment. The pip command will be revisited in this
chapter along with the topic of Docker.
So, without further delay, let us turn the page to the next
chapter!
Introduction
While building an automation project, there are several
components that one needs to take care of. These
components, whether it be an accessory, determine the
efficient working of the automation project. This chapter
discusses the various components that are required for
building the architecture of an automation project in Python.
Different accessory tools like these add up to the total
effectiveness of the automation project, which makes it
imperative for us to discuss them in this chapter.
This chapter is basically a collection of discrete topics, which
might be directly unrelated to each other, but collectively
turn out to be valuable in the context of the overall
automation task or project.
The process of setting up a virtual environment has been
discussed in this chapter. The pip command, which is the
gateway to installing any package in Python has been
revisited in this chapter in greater detail. The various
functionalities provided by the pip command have been
discussed through syntax and examples. Further essentials
like docker have also been highlighted upon at the end of
the chapter.
Structure
The chapter covers the following topics:
• Introduction to virtual environment
∘ Setting up a virtual environment
∘ Virtual environment directories
∘ Additional considerations
• Python PIP revisited
• Performing basic operations
∘ Working with the requirements.txt file
∘ Using Docker for containerization
Objectives
This chapter is basically the outcome of the realization that
certain important accessories which could not be visited in
other chapters need to be jotted down in one place. This
chapter does a great job of providing a useful appendix for
these accessory utilities.
By the end of this chapter, the reader shall have a firm
grasp on the concepts of a virtual environment, pip
command and docker. Many features like these have now
become a part of regular usage, but most of us remain
bereft of their theoretical understanding. This chapter shall
imbibe that additional know-how of these supplementary
topics, thus enabling the reader to effectively manage and
architect future automation projects.
In this chapter, the reader shall thoroughly get acquainted
with the process of creating virtual environments in Python,
using pip to perform various essential tasks, and thereafter
using the requirements.txt file within the virtual
environment.
Conclusion
In this chapter, our focus has been on brushing up all
accessory concepts that were otherwise not a part of
specific topics but are of immense importance considering
the architectural considerations of automation projects. The
concepts of virtual environment and detailed examples of
pip library commands shall provide a massive boost to the
overall utility of the automation project.
In the next chapter, we would explore another exciting
luxury provided by Python which is the PyScript framework,
which is an open-source framework that provides the
functionality to create front end web applications using
Python. If that sounds interesting enough let us turn the
page to the next chapter!
Introduction
In this chapter, we introduce the reader to an interesting
and ongoing project which is currently under development,
but garnering lots of attention called PyScript which is
basically a Python framework that allows us to write Python
code inside HTML directly. The advantage of PyScript is that
it allows one to run the Python code directly in the browser
without infrastructural barriers. PyScript does not require
any development environment other than a web browser
like Chrome and a text editor like Notepad. Additionally, we
do not install anything like we used to do earlier using the
pip installer.
Developers in Python who have background in JavaScript
would find it exciting to experiment with the functionality
provided by PyScript.
This chapter shall provide the basic awareness that is
necessary for the reader to explore this novel framework.
Structure
The chapter covers the following topics:
• Introduction to PyScript
• Creating a basic webpage using PyScript
• Adding working Python code to the webpage
• Using third party libraries with PyScript
• Referencing external Python files in PyScript
Objectives
This chapter is a worthwhile exploration of a novel
framework which shall take the reader through multiple
lucid examples from specific sections related to the PyScript
framework. Each of these sections would introduce the
reader to unique concepts which the reader shall find
extremely useful for further implementation.
The Python code in this chapter has been kept simple since
the key focus here would be to understand salient concepts
in PyScript. The sections and the corresponding exercises
have been carefully chosen in such a way that they would
clearly enlighten the reader about the concept that is
intended to be explained. The chapter would introduce the
reader to PyScript and basic concepts related to the
framework. Thereafter, specific exercises would introduce
the reader to the creation of a basic webpage in PyScript,
inclusion of a working Python code in PyScript, importing
third party libraries in PyScript and referencing external
Python code in PyScript.
By the end of this chapter, the reader would appreciate the
advent of PyScript as a novel idea to implement web
applications along with the presence of conventional web
development frameworks.
Introduction to PyScript
PyScript is an open-source web framework that allows one
to embed and execute Python code in the browser using
HTML tags. It enables dynamic generation of pages with
Python. For a long time JavaScript has been the language
that has dominated frontend development because of its
ability to run natively in the browser and interact with HTML
and CSS. Then arrived WebAssembly, which is an open
standard enabling the usage of binary code on the web. This
allowed developers to run languages like C and C++ in the
browser. Obviously, Python soon entered the race and came
up with this framework.
PyScript was developed by Anaconda and is built using
Pyodide, which ports CPython to WebAssembly. It is typically
used when one would want to move a Python application
from the backend to the frontend. Another great advantage
is the ability to use scientific packages from Python like
Scikit-learn, pandas and numpy. These work only in the back
end and not in the front end. However, PyScript allows one
to utilize these modules in the front end. Finally, PyScript
provides the ability to interact with the local file system
which JavaScript does not.
Listed below are basic prerequisites that the reader needs to
be having prior to beginning this chapter:
• Basic knowledge of HTML, CSS and JavaScript.
• The Chrome web browser, which is the suggested bro
wser in the official documentation of PyScript.
In this chapter, we shall gradually witness the involvement
of Python for web development in the form of the PyScript
framework by starting with basic HTML applications and
increasing the complexity of the examples thereafter.
In the next section, we shall create a basic web page using
PyScript.
</body>
</html>
Save the file as PyScript_Tutorial.html.
Next, we shall need to add the link tag and the script tag
which are described below:
• Link tag: This is the link for the stylesheet allowing o
ne to format the output of Python code. This is given a
s below:
<link rel="stylesheet" href="https://pyscript.ne
t/alpha/pyscript.css" />
• Script tag: This brings the JavaScript source file that
works with the browser for interacting with Python run
time. This is given as below:
<script defer src="https://pyscript.net/alpha/pys
cript.js"></script>
After incorporating both these tags, the modified HTML
script would look like the one below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
However, this does not complete the application as we are
yet to add the important section where Python code shall be
written. We implement this using the py-script tag as
shown below:
<py-script>
#python code
</py-script>
It is important to note that the Python code written with the
py-script tag needs to be indented. So, with this
knowledge, let us write our first Hello World script and
modify the HTML document accordingly to incorporate the
Python code within the py-script tag.
The modified HTML script would look like the one shown on
the next page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
'India':'New Delhi',
'Argentina':'Buenos Aires',
'Japan':'Tokyo',
'Germany':'Berlin',
'France':'Paris',
'Egypt':'Cairo'}
Next, we shall print the values of the dictionary keys as
shown below. These values are basically the capital cities of
the respective countries:
for str_country in dict_countries:
print(dict_countries[str_country])
We would need to include this code within the py-script
block in the HTML template PyScript_Tutorial.html that
we saved earlier. Save this file with a different name
Countries_List.html. The modified piece of HTML code is
shown in the next page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<py-script>
dict_countries = {'United States of America':'Washington
D.C.',
'India':'New Delhi',
'Argentina':'Buenos Aires',
'Japan':'Tokyo',
'Germany':'Berlin',
'France':'Paris',
'Egypt':'Cairo'}
</py-script>
Copy the code above in the HTML file and save the file. Run
the file and open it in the Chrome browser. We get the
output as shown in Figure 15.2:
<link
rel="stylesheet"href="https://pyscript.net/alpha/p
yscript.css" />
<script defer
src="https://pyscript.net/alpha/pyscript.js">
</script>
- numpy
</py-env>
</head>
<body>
<py-script>
import numpy as np
matrix_1 = np.random.rand(3,4)*10
matrix_2 = np.random.rand(3,4)*10
matrix_sum = matrix_1 + matrix_2
print('\nThis is a program to calculate the sum of two
matrices having randomly generated values:\n')
Figure 15.3: Numpy arrays and the result obtained after adding them
#Calculate Square
def Square(intNum):
intSquare = intNum * intNum
return intSquare
#Calculate Cube
def Cube(intNum):
intCube = intNum * intNum * intNum
return intCube
Next, we would want to reference the Python file in the py-
script block and perform the calculations. Below is the code
that we would include in the py-script block:
<py-script>
from Perform_Math import Square,Cube
intNum=5
intSquare = Square(intNum)
intCube = Cube(intNum)
print('\nThe number is:')
print(intNum)
print('\nThe square of the number is:')
print(intSquare)
print('\nThe cube of the number is:')
print(intCube)
</py-script>
We reference Perform_Math as a module and import the
functions Square and Cube into the Python code to
perform calculations and print the results thereafter.
Before running the HTML code, we would need to add
another block in the HTML file called py-config which
basically instructs the HTML code to fetch the appropriate
Python file. For this exercise, we shall be saving the Python
file and the HTML file in the same folder within the directory.
Below is the additional block of code corresponding to the
py-config tag that we would need to add to the HTML code.
<py-config>
[[fetch]]
files = ["./Perform_Math.py"]
</py-config>
We would also require making few more changes in href
and src within the HTML code where we specify the PyScript
releases. Unlike what we did in the previous exercise where
we used the /latest/pyscript.js release, we would be
linking to the PyScript static assets in release 2022.12.1
which is a later release with which the code seemed to work
properly. Below is the modified block of the HTML code in
which we include the title as well:
<title>Referencing External Python Files</title>
<link rel="stylesheet" href="https://pyscript.net/release
s/2022.12.1/pyscript.css" />
<script defer src="https://pyscript.net/releases/2022.1
2.1/pyscript.js"></script>
The complete HTML code has been given below for
reference. Copy the code in a text editor like notepad and
save the file with the name Perform_Math.html. As
mentioned earlier, we would want to make sure that the
HTML file is saved in the same folder where the Python file
is saved.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<py-config>
[[fetch]]
files = ["./Perform_Math.py"]
</py-config>
<py-script>
from Perform_Math import Square,Cube
intNum=5
intSquare = Square(intNum)
intCube = Cube(intNum)
print('\nThe number is:')
print(intNum)
print('\nThe square of the number is:')
print(intSquare)
print('\nThe cube of the number is:')
print(intCube)
</py-script>
</body>
</html>
On running the code in Chrome, we get an error as shown in
Figure 15.4 below:
Conclusion
As we conclude this chapter, we have laid a solid
groundwork for understanding PyScript, a cutting-edge
framework that is reshaping how Python interacts with web
technologies. While we have unlocked many possibilities
with PyScript, it is essential to remember that it is a
dynamic and evolving tool. Staying abreast of its
developments will be key to leveraging its full potential in
the future.
With the end of this chapter, we begin our journey to the
concluding chapter of the book. Reflecting on our journey
through this book, we have traversed a diverse landscape of
automation—from file and folder automation to delving into
the realms of RPA, AI, ML, and deep learning. Each chapter
has contributed a piece to this expansive mosaic of Python
automation knowledge.
As we approach the final chapter, our focus shifts to test
automation—a crucial aspect of software development. We
will explore the Selenium framework, a cornerstone of
browser automation, followed by insights into the Pytest
framework and a discussion on the Robotic framework in
Python. This concluding chapter promises to be the
culminating point of our comprehensive exploration of
Python automation.
It has been a marathon run so far, so one would not want to
miss out on the finish line. Let us step forward into the final
leg of our marathon, a journey that is as enlightening as it is
empowering. Onward to the finish line in our exploration of
Python automation! Hence, without further delay, here we
go to the last and final chapter!
Introduction
So here we are into the last chapter of the book! We shall
begin this chapter by delving into a topic that is most
commonly addressed at the end of software development
which is testing. The practice of testing software involves
creation of multiple test cases and performing of checks. As
this process gets manual, it becomes time consuming and
inefficient. This is where Python comes to the rescue with
open-source libraries to enable automation of this process.
We start by introducing Selenium, the world’s most popular
framework for automating web browsers. Python, among
other languages, is supported by Selenium, and we will
explore its capabilities through the Selenium Python
module. Following this, we will examine Pytest, another
powerful testing framework known for its simplicity and
scalability. Lastly, we will venture into the Python Robot
Framework, an interesting tool that offers a higher-level,
keyword-driven approach to test automation.
Structure
The chapter covers the following topics:
• Introduction to Selenium
∘ Setting up the Selenium
∘ Exploring web automation with Selenium Python
API
• The Pytest library
∘ Advantages and limitations of Pytest
• Python Robot Framework
∘ Running test cases in the Python Robot
Framework
Objectives
This concluding chapter of the book shall address the most
practically useful topic of test automation by introducing the
reader to frameworks like Selenium, Pytest and the Python
robotic framework. By the end of this chapter, the reader
shall not only have a basic understanding of these
frameworks but shall also appreciate the mammoth role that
Python plays in providing the functionality of test
automation. Each of these sections shall cover practical
examples that shall illustrate salient concepts and
demonstrate the use of Python libraries in execution of
required tasks.
The chapter shall aim to provide a foundation based on
which the reader would be able to independently explore
and implement further use cases using Python frameworks
like Selenium, Pytest and the Robot Framework, thus making
the reader get acquainted with an extremely useful skillset
that adds the final finishing touch to an automation project.
Introduction to Selenium
Selenium is an open-source software testing framework that
is used to automate web applications. It works on all
browsers, majority of the operating systems and the scripts
are written in various programming languages like Python,
Java, and C#.
In this chapter, we shall be focusing on the Chrome browser
and of course, the Python programming language.
There are four main components in Selenium as shown
below in Figure 16.1:
# Webdriver object
objDriver= webdriver.Chrome()
obj_Chrome_Options = webdriver.ChromeOptions()
obj_Chrome_Options.add_experimental_option("excludeSwi
tches",["enable-automation"])
obj_Service = Service(r"C:\Users\dell\chromedriver-
win64\chromedriver.exe")
obj_Driver=webdriver.Chrome(options=obj_Chrome_Option
s,service=obj_Service)
obj_Driver.get("https://www.google.co.in/")
On running the code above, we get the regular Chrome
window without the earlier message. We have now
understood the setup and procedure to instantiate a
Chrome browser with Selenium and shall further explore
web automation with Selenium.
obj_Chrome_Options = webdriver.ChromeOptions()
obj_Chrome_Options.add_experimental_option("exclu
deSwitches",["enable-automation"])
obj_Service = Service(r"C:\Users\dell\chromedriver-
win64\chromedriver.exe")
obj_Driver=webdriver.Chrome(options=obj_Chrome_
Options,service=obj_Service)
obj_Driver.get("https://www.wikipedia.org/")
5. Next, we would want to import the additional libraries
that we discussed earlier. They have been included be
low once again for reference. These can be added at t
he top of the module along with the other libraries:
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
6. Finally, we would want to include the lines of code tha
t would perform the web element search, entering of t
ext and thereafter pressing the enter key. These lines
have been shown below along with their respective co
mments:
#Get the search textbox element
text = obj_Driver.find_element(By.ID,"searchInput")
#Type text into the textbox element
text.send_keys("Machine Learning")
#Press the enter key
text.send_keys(Keys.ENTER)
The entire code has been shown below for reference. Copy
and run the code into a Python editor:
#Exploring browser automation with Selenium
obj_Chrome_Options = webdriver.ChromeOptions()
obj_Chrome_Options.add_experimental_option("excludeSwi
tches",["enable-automation"])
obj_Service = Service(r"C:\Users\dell\chromedriver-
win64\chromedriver.exe")
obj_Driver=webdriver.Chrome(options=obj_Chrome_Option
s,service=obj_Service)
obj_Driver.get("https://www.wikipedia.org/")
Pytest library
Pytest is an open-source Python framework used for unit
testing, which is basically the process of performing
automated testing to validate that a single component of a
software works as intended. While many other Python
frameworks like unittest and doctest are used for this
purpose, the advantage of Pytest is that it has a large
community to support its open-source development. Hence,
we would discuss the Pytest framework in this chapter. In
this section, we would explore some sample examples that
would illustrate the efficient usage and implementation of
Pytest in unit testing.
The first step that needs to be performed prior to using the
library is its installation using the pip command as shown
below:
pip install pytest
Once the library is installed, we would move ahead with
writing some piece of code. In this section, we shall take a
simple example of a test that checks whether a number
equals a certain target number.
Let us create a simple function that calculates the square of
a number. Below is the Python code for the function.
def Square(intNum):
intSquare = intNum*intNum
return intSquare
Copy the code above in a Python editor and save the file by
the name Calculate_Square.py.
Next, we create another Python file within the same
directory. In this new file, we would be importing this file as
a module and using the Square function.
Copy the code below in a new file within the Python editor.
#Perform test
from Calculate_Square import Square
def test_Square():
intNum = 10
intSquare = Square(intNum)
assert intSquare == 100
Now comes the important part. While saving the file, we
should name the Python file in such a way that the name
should start with test_ or end with _test. The way Pytest
works is that it checks for the Python file in the particular
directory that starts with test_ or ends with _test. Let us
save this file with the name Square_test.py.
Below is a quick explanation of the code in the file
Square_test.py.
1. First, we import the file Calculate_Square as a modu
le and import the Square function from the module u
sing the code below:
from Calculate_Square import Square
2. Next, we define a function named test_Square that c
hecks whether the square of a number being calculate
d using the imported Square function equals 100. Not
e that Pytest requires the function name to be prepen
ded with test_.
Here, define a variable intNum and initialize it to an i
nteger value of 10 using the code below:
intNum = 10
Next, we pass intNum as an argument to the functio
n Square to calculate the square of the value held in
variable intNum and assign the result thus obtained t
o another variable named intSquare.
Finally, we use the most important assert function. Th
is is a Python function that validates whether the valu
e on the left-hand side would equal the value on the ri
ght-hand side. In this case, it checks whether the valu
e held in the variable intSquare would equal 100.
Next, let us test the assertion using the Pytest library.
3. Follow the steps below to implement the test using th
e Pytest library:
a. Create a folder within the C:\ drive and name the
folder as Pytest_Tutorial.
b. Save both the files Calculate_Square.py and
Square_test.py in this folder.
c. Open the prompt and make the directory
C:\Pytest_Tutorial as the current directory using
the command below:
cd "C:\Pytest_Tutorial"
The snapshot of the prompt after entering the
command above has been shown below in Figure
16.5 below:
Conclusion
As we turn the final page of this book, we reach not just the
end of a chapter but the culmination of an extensive and
enriching journey through the world of Python programming
and automation! This chapter, rich with images and detailed
instructions, has strived to demystify test automation,
ensuring concepts are accessible and examples are
straightforward. This chapter has made a gentle attempt at
introducing important concepts in test automation while
keeping the examples as simple as possible. Hope this
concluding chapter serves the purpose of efficiently closing
in on the exploration of Python programming applications by
providing an enlightening edge to salient concepts!
Throughout this book, the motive has been to keep the code
snippets and the examples super simple so that the key
focus of the reader remains on grasping the concepts.
We’ve ventured through a diverse landscape – from
industrial automation to software testing, hyperautomation,
orchestration, RPA, desktop and file automation, and delved
into the depths of machine learning and deep learning. This
book promises to serve as a ready manual for quick
reference of key concepts in each of these topics. It is one of
the very few attempts at covering a wide variety of topics at
a moderately high level but sufficiently deep level. It stands
as a comprehensive guide that navigates a variety of topics
with a balance of breadth and depth.
As you continue on your Python automation journey, please
revisit these pages whenever you wish to seek clarity or
inspiration. Remember, the journey of learning and
exploration never truly ends – there is always more to
discover and master. Last but not least, we thank you for
joining us on this educational adventure! We wish you all
the best as you forge ahead, armed with new knowledge
and insights. May your path in Python automation be as
rewarding as it is enlightening! On this note, the book
concludes by wishing you all the very best in your future
endeavors on this exciting journey of Python automation!
A
algorithmic processing
Amazon Elastic Kubernetes Service (Amazon EKS)
Artificial General Intelligence (AGI)
artificial intelligence (AI)
about
history
Artificial Neural Network (ANN)
automation
versus orchestration
automation anywhere
bot creator
bot runner
control room
B
backpropagation
Beautiful Soup
bias
about
versus variance
Blue Prism
control room
object studio
process studio
bots
business process workflow
C
cell
accessing, with name range
merging
used, for looping
chart
creating, with openpyxl
styling
chat
chatbot
chat
reflections
role
rule based chatbot
self-learning chatbot
Community Edition
confusion matrix
conversational agents
implementing
convolution
convolutional layer
Convolutional Neural Network (CNN)
about
convolutional layer
fully connected layer
pooling layer
cost function
D
data processing
data science
decision tree
deep learning (DL)
about
application
Python libraries
dimensionality reduction
about
feature extraction
feature selection
Document Object Model (DOM)
E
Excel
automating, with Python
Excel formulae
working, with openpyxl
existing workbook
modifying
opening
external Python files
referencing, in PyScript
F
forward pass
fully connected layer
G
Gmail automation
2-step verification
app password, obtaining
Gmail message, sending with Python
prerequisites
Gmail message
sending, with Python
gradient descent
graphical processing unit (GPU)
Graphical User Interface (GUI)
grayscale image
H
hyperautomation
about
challenges
defining
document, enhancing with optical character recognition
process
use cases
I
images
working, with PIL library
information technology (IT)
integrated development environment (IDE)
Internet of Things (IoT)
J
JavaScript Object Notation (JSON)
K
Keras
keyboard functions
hotkey()
implementing, with PyAutoGUI library
typewrite()
K means clustering
k nearest neighbors
K nearest neighbors
kube
Kubernetes
Kubernetes framework, components
cluster
node
pod
replication controller
L
large language models (LLMs)
learning rate
least squares regression
linear discriminant analysis (LDA)
linear regression
logistic regression
Long Short Term Memory (LSTM)
looping
through cell
luigi library
luigi module
M
machine learning (ML)
about
concepts
Python libraries
machine learning (ML) models
bias
gradient descent
key concepts
variance
margin
matplotlib
message box functions
alert()
confirm()
exploring, with PyAutoGUI library
mouse functions
click()
implementing, with PyAutoGUI library
mouseDown()
mouseUp()
moveTo()
scroll()
N
Naïve Bayes
name range
used, for accessing cell
natural language processing (NLP)
about
algorithmic processing
data processing
lemmatization
part-of-speech (POS) tagging
segmentation
stemming
stop words removal
tokenization
Natural Language Toolkit (NLTK)
neural network
about
architecture
hidden layer
implementing, in Python
input layer
output layer
types
neural network, types
Convolutional Neural Network (CNN)
Long Short Term Memory (LSTM)
Recurrent Neural Network (RNN)
nodes
Nomad
Numerical Python (NumPy)
O
Object-Oriented Programming (OOP)
OpenCV
openpyxl
used, for creating charts
used, for Excel working
openpyxl library
Openshift
Openshift Dedicated
Openshift Online
Open-Source Computer Vision (OpenCV)
about
working with
optical character recognition (OCR)
orchestration
about
versus automation
orchestration platforms
about
Amazon Elastic Kubernetes Service (Amazon EKS)
Kubernetes
Nomad
Openshift
overfitting
P
pandas
PDF file
merging
reading, with PyPDF2 library
rotating
PIL library
used, for images working
Platform as a service (PaaS)
pooling layer
principal component analysis
PyAutoGUI library
used, for exploring message box functions
used, for implementing basic mouse function
used, for implementing keyboard functions
using
PyPDF2 library
used, for reading PDF file
PyPDF library
PyScript
about
basic webpage, creating
external Python files, referencing
third party libraries
Pytest library
about
advantages
limitation
Python
about
advantages
high-level language
integrating, with UiPath
libraries
portability aspect
used, for automating Excel
used, for implementing neural network
used, for implementing supervised machine learning
algorithms
used, for implementing unsupervised learning algorithms
used, for sending Gmail message
Python activities
exploring, in UiPath
Python code
adding, to webpage
Python environment
setting up, in UiPath
Python Imaging Library (PIL)
Python libraries
Beautiful Soup library
for deep learning
for Excel automation
for machine learning (ML)
Keras
Natural Language Toolkit (NLTK)
OpenCV
PyTorch
requests module
Scikit Learn
Spacy
TensorFlow
Theano
web page, inspecting
web scraping
xlsxwriter library
xlwings library
Python orchestration
about
luigi module
perfect library
Python OS module
about
function
functions
Python pip package
about
basic operation, performing
Docker containerization, using
requirements.txt file, working
Python Robot Framework
about
test cases, running
Python script
creating
Python shutil module
about
file, copying
file, moving
used, for moving file based extension
PyTorch
pywhatkit
R
random forests
Record Macro
Rectified Linear Unit Function (Relu)
Recurrent Neural Network (RNN)
reflections
reinforcement learning
Remote Control (RC)
RGB image
Robotic Process Automation (RPA)
about
history
Python package
tools
use case
Robotic Process Automation (RPA), components
about
bot runner
control center
development studio
plugins and extensions
recorder
Robotic Process Automation (RPA), tools
about
automation anywhere
Blue Prism
UiPath
rule based chatbot
S
Scientific Python (SciPy)
scikit-learn
scrapy
Selenium
Selenium, components
Selenium GRID
Selenium IDE
Selenium RC
Selenium WebDriver
Selenium GRID
Selenium IDE
Selenium Python API
setting up
web automation, exploring
Selenium RC
Selenium Recorder
Selenium WebDriver
self-learning chatbot
software as a service (SaaS)
Spacy
supervised learning
about
decision tree
K nearest neighbors
linear regression
logistic regression
Naïve Bayes
random forests
support vector machine
supervised machine learning algorithms
implementing, with Python
support vector machines
T
tabula library
TensorFlow
textract library
text recognition
Theano
transformers
Turing Test
U
UiPath
about
Python activities, exploring
Python environment, setting up
used, for integrating Python
underfitting
unsupervised learning
unsupervised learning algorithms
dimensionality reduction
implementing, with Python
K means clustering
linear discriminant analysis (LDA)
principal component analysis
V
variance
about
versus bias
virtual environment
about
additional consideration
directories
setting up
Visual Basic for Applications (VBA)
W
web page
information, extracting
web scraping
about
legal statement
Python libraries
WhatsApp message
automating
X
xlsxwriter library
xlwings library