Lets - Learn - AI - StepUp - Module - NITI AAYOG
Lets - Learn - AI - StepUp - Module - NITI AAYOG
Lets - Learn - AI - StepUp - Module - NITI AAYOG
ARTIFICIAL
INTELLIGENCE
AUGUST 2020 | STEP-UP MODULE
DR. RAJIV
KUMAR
VICE CHAIRMAN,
NITI AAYOG
SHRI
AMITABH
KANT
CEO,
NITI AAYOG
The Fourth Industrial Revolution is transforming the global
innovation and production systems. The future depends
heavily on our individual and collective capacities to use
the rapidly emerging technologies. Developing nations
like India have a unique opportunity to spring to the next
levels of development by investing in the right skills and
innovation frameworks and preparing for the future.
The Base Module of ATL Artificial Intelligence is a perfect tool that is empowering young
minds to tinker with AI while learning the basics through hands-on learning. It has
been designed in an easy-paced manner that can be absorbed by students to build a
strong foundation. It aims to provide practical experience in a variety of sub-fields while
addressing the diverse needs and interests. The subject of AI opens up innumerable
options and students must know what they are to make the appropriate choices based
on their objectives. The focus on math, programming, and problem-solving is done in a
way, keeping in mind the overall construct of the academic curriculum in AI.
And now we have the Step-up Module. It builds on the success of the Base Module. One
of the projects included has been done by a student and it is their way of continuing
to engage after completing the Base Module. It also gives ample assurance that this
approach is effective, and it continues to inspire students in their learning journey by
challenging them to tackle bigger blocks along the way.
Here’s wishing good luck to the creators of the Step-up Module and the students.
AMIT AGGARWAL
CEO, SSC NASSCOM & CO-ARCHITECT
NASSCOM FUTURESKILLS
A few years from now, we will witness AI becoming larger than the internet revolution.
AI has an unprecedented potential of disruption, the impact of which is far beyond our
imagination. It is critical to introduce the world of new technologies to the bright &
young minds of India, to fasten the pace of the AI revolution that we are yet to witness.
It is with this ideology, NASSCOM together with NITI Aayog rolled out the AI Base
Module, a progressive learning resource, earlier this year to ignite and develop the skills
and interests of students in the field of AI and ML.
This Step-Up Module, is an advanced version built upon the success of the base module.
Much to our delight, one of the projects included in the module has been designed by a
student who completed the base module. It is indeed such a pleasure to see one of the
learners get inspired to innovate.
The practical experience, which is an integral part of the program seeks to motivate
students to explore various sub-fields or interests that will encourage them to cultivate
and pursue disciplines that relate to skills in AI and ML. The module is designed to equip
our next generation of talent with basic AI skills and to make data fluency an integral
part of their learning journey.
I’d like to take a moment to thank all our Industry members for their whole hearted
participation and contribution to the program. At the Tech Sector Skills Council
NASSCOM, we have always emphasized on the need for giving real-world and industry-
relevant experience to the students. NASSCOM’s FutureSkills® initiative believes that
building a strong foundation on emerging technologies like AI, is critical for embarking
upon a journey towards making India a hub of digital talent.
I urge all students to make the most of this module and think of bold ideas and innovative
applications of AI. I am hopeful that this module will groom our students to play an
instrumental role in the next wave of innovations and breakthroughs driven through AI
in India.
Preface
The collaboration between Atal
Innovation Mission, NITI Aayog and
NASSCOM is a special one. It is one
of the most prolific examples of the
collaborative spirit that the Industry,
academia and the Government of India
share. At its core, we believe that to create
a country of highly-skilled workforce and
innovators, industry must step-up and Ronak Jogeshwar Yudhisther Yadav
introduce cutting-edge knowledge at a (Innovation Lead - Atal (Head - Strategic Initiatives,
young age through the school education Tinkering Labs, Atal NASSCOM FutureSkills)
Innovation Mission, NITI Aayog)
ecosystem.
Towards this goal, Atal Innovation Mission is setting up state-of-the-art Atal Tinkering
Labs (ATls) in districts across India. The ATL ecosystem enables students to become
problem solvers by giving access to 21st century technology and skills within their
or nearby schools. These makerspaces house various tools such as microprocessors,
programmable boards, IoT components, sensors and actuators, rapid prototyping tools,
3D printers and various learning kits.
Artificial Intelligence is one of the key technologies of the 4th Industrial revolution
which will impact Nation's sustainable development goals. A firm action to ensure our
young minds are equipped to use AI to maximize the benefit is the need of the hour.
Training in AI at this young age will result in innovators, researchers, entrepreneurs and
the workforce that will be able to successfully create, invent, and improve.”
The ATL AI Step-Up module is a continuum to the ATL AI Base Module (Link : https://aim.
gov.in/Lets_learn_AI_Base_Module.pdf) where the students who have grown an appetite
towards the technology can become experts of the Artificial Intelligence Technology.
Our special gratitude to Mr. S Manish Singh and his team at PROGILENCE Capability
Development and Ms. Naba Suroor, Atal Innovation Mission, NITI Aayog for working
relentlessly to curate and scribe the AI Base and Step-Up Modules.
We believe that this module shall pave the way for India to become a technology
powerhouse of the world and produce a legion of skilled technocrats. So, let’s celebrate
the spirit of curiosity as the key to innovation and entrepreneurship.
Happy Tinkering J
TABLE OF
CONTENTS
UNIT 1 1-48
Reconnecting with Artificial Intelligence
1.1 Base Module Recap
1.2 AI Applications
1.3 Process of Building an AI
1.4 Skills to Build AI Solutions
1.5 Project: Graphical Programming and Air Draw
UNIT 2 51-72
Ethics in AI
2.1 What does ‘Ethical AI’ mean?
2.2 Fairness
2.3 Robustness
2.4 Privacy
2.5 Explainability
2.6 Governance
UNIT 3 75-122
Machine Learning
3.1 Introduction to Machine Learning
3.2 Gathering Data
3.3 Preparing the Data
3.4 Choosing the Model
3.5 Neural Networks and Deep Learning
3.6 Introduction to Maths for ML
3.7 Training
3.8 Evaluating
3.9 Parameter Tuning
3.10 Prediction
3.11 Machine Learning as a Service
UNIT 4 123-143
Natural Language Processing (NLP)
4.1 Introduction to Natural Language Processing (NLP)
4.2 Natural Language Understanding
4.3 Application of NLP
4.4 Use Cases
4.5 How Does NLP work
4.6 Elements of NLP
UNIT 5 145-169
Computer Vision (CV)
5.1 Introduction to computer vision
5.2 Computer Vision Applications
5.3 Challenges of Computer Vision
5.4 Optical Character Recognition
5.5 OpenCV
Project: Face Recognition using Scratch
Project: Attendance System using Face Recognition
UNIT 6 171-212
Databases and SQL
6.1 Databases
6.2 SQL
UNIT 7 215-255
Python
7.1 Introduction to Python
7.2 Integrated Development Environment (IDE)
7.3 Core Programming concepts
7.4 Essential Data Science Libraries
7.5 Troubleshooting Defects in Python Code
Practice for Writing Code
UNIT 8 257-279
Algorithms
8.1 Introduction to Machine Learning Algorithms
8.2 Algorithms used in Supervised Learning
8.3 Algorithms used in Unsupervised Learning
8.4 Algorithms used in Reinforced Learning
Acknowledgement280-282
Disclaimer: The content in this handbook has been curated from various online sources. We do not
intend to infringe on any copyrights.
Note: This document contains several weblinks and associated QR codes. The relevant documents
and video can be accessed either by clicking the weblink or by scanning the QR code using QR
code scanner application, which can be downloaded on any smartphone via the application store.
Please note that there are third party links in this module and Atal Innovation Mission or NITI
Aayog or NASSCOM does not endorse any person(s) or organisations mentioned on or related to
these links.
Let's Learn Artificial Intelligence - Step-Up Module
UNIT 1
RECONNECTING WITH ARTIFICIAL
INTELLIGENCE
Topic Covered
Base Module Recap | AI Applications | Process of Building an AI |
Skills to Build AI Solutions | Project: Graphical Programming and Air Draw
1.1 BASE MODULE RECAP
Welcome back to the fascinating and ever-evolving world of Artificial Intelligence.
Most of you would have been through the Base Module for ‘Let’s Learn AI’, by now.
This ‘Step-Up’ module seeks to take you further in your journey to learn how to use
and build Artificial Intelligence.
We hope you remember and have retained what we learned in the Base module. Let
us have a fun QUIZ to help you recall a few key AI concepts that we have learnt so far.
1. Search for seven things that pertain to what AI machines could understand or do
from the following word search puzzle. The words may be given horizontally,
vertically or diagonally.
P R E D I C T D
L A N G U A G E
E N T R E A A C
A U D T L A G I
R T L Y E S I D
N C L Z F R U E
O V I S I O N F
I M A G E Y B S
2
Let's Learn Artificial Intelligence - Step-Up Module
3
3. Fill the numbered boxes with the correct answers, based on the clues given
CLUES ANSWERS
4. Complete the crossword given below by using the clues provided on the next
page. The clues are given for the words that can be read ‘Across’ and ‘Down’.
2
3
4 5
10
11
4
Let's Learn Artificial Intelligence - Step-Up Module
CLUES:
ACROSS: DOWN:
3: It is the process of finding out the 1: It is a set of step-by-step instructions
relationships between different to solve a problem created along
parts of the data and the patterns with rules that decide which path
that can be recognized in it. to take when a decision is faced.
11: It is a type of Algorithm used to find 11: It refers to a collection of large and
a specific value in an ordered list, by complex datasets that may be
dividing the list into two parts, each complex due to its volume or its
time it searches. variety.
5
5. Fill-in the blanks for the paragraph given below, using the words that are
provided in the adjacent box.
Now you will recall that in the Base module we looked at various aspects of
AI, experienced various demo models, initiated programming for AI with an
introduction to Scratch and Python Programming. We introduced ourselves
to the three different types of Machine learning (Supervised, Unsupervised
and Reinforcement Learning). We looked at algorithms including
sorting and classification algorithms, We also looked at the
importance of algebra, probability and statistics for AI and ML and
finally explored some ethical dimensions related to AI use.
Now that you remember the basics, let us revisit how AI is being
used in various fields. While you explore the current applications,
think about areas where you would like to apply AI.
6
Let's Learn Artificial Intelligence - Step-Up Module
1.2 AI APPLICATIONS
AI has already penetrated many aspects of our world. It is getting integrated into our
lives so seamlessly that often we do not even realize that we are using AI.
Here are some areas where AI is currently being used.
Explore the links under each area of application and then try and list the various as-
pects of AI being used to solve the problems.
7
Healthcare
AI has already being used widely in healthcare, whether it is for making a faster and
better diagnosis, patient care, clinical research, drug development or insurance. AI is
enhancing benefits for healthcare providers, patients and the society at large.
EXPLORE
https://indiaai.in/article/how-icmr-leverages-
ibm-s-watson-for-rapid-response-on-
https://healthcareweekly.com/ covid19
trend-watch-artificial-intelligence-
and-healthcare/
https://www.youtube.com/watch?time_
continue=11&v=zz3rbMvjilU&feature=emb_
logo
https://www.forbes.com/sites/
bernardmarr/2017/05/16/how-ai-
and-deep-learning-is-now-used-to-
diagnose-cancer/#6c8af2dcc783 https://www.ted.com/talks/pratik_shah_how_
ai_is_making_it_easier_to_diagnose_disease
Astronomy
Astronomers are using Artificial Intelligence to organize and categorize the images of
galaxies taken by the satellites. It is also being used to locate objects such as gravita-
tional lenses in space or craters on the moon.
EXPLORE
https://www.sciencedaily.com/ https://astronomy.com/news/2018/03/artificial-
releases/2020/05/200512151951. intelligence-identifies-6000-new-craters-on-
htm the-moon
8
Let's Learn Artificial Intelligence - Step-Up Module
Agriculture
AI technologies are being used to devise and implement ways to grow healthier crops,
with more yield, control pests, monitor the soil and growing conditions, assist farmers
with data, help with the workload as well improve the food supply chain.
EXPLORE
https://indiaai.in/article/when-data- https://www.youtube.com/
surpasses-intuition-the-future-of- watch?v=oot55bK62pk
precision-agriculture
https://thecsrjournal.in/artificial- https://www.youtube.com/
intelligence-in-agriculture-in-india/ watch?v=GSvT940uS_8
Education
AI is being used for personalized testing and development of personalized teaching
materials based on each student’s needs. It is also being used to take over the admin-
istrative work of teachers so that they can spend more quality time on actual teaching.
EXPLORE
https://www.theedadvocate.org/7- https://indiaai.in/videos/can-ai-
ways-that-artificial-intelligence- transform-school-education-in-
helps-students-learn/ india-last-week-on-indiaai-ep-4
https://cognii.com/ https://www.youtube.com/
watch?v=rMYiSnp3-Lg
9
Entertainment
Entertainment services are using ML/AI algorithms to provide recommendations and
use Natural Language Processing to generate automated subtitles. In video games,
AI is being used to analyze a player’s actions to predict their future moves and adapt
to the player’s behaviour.
EXPLORE
https://www.bbntimes. https://cogent.co/blog/
com/technology/artificial- recommendations-with-machine-
intelligence-in-the- learning-2/
entertainment-industry
https://emerj.com/ai-sector- https://www.youtube.com/
overviews/ai-in-movies- watch?v=YXHxJIGX86E
entertainment-visual-media/
Business
Where it is to market and sell, give customers a better experience or develop
cost-effective and more efficient ways of product manufacture and delivery, AI is
transforming businesses.
EXPLORE
https://bernardmarr.com/default.
asp?contentID=1546
https://indiaai.in/article/how-
ai-is-helping-the-world-of-
hyperlocal-commerce
https://blog.griddynamics.com/how-
we-built-a-conversational-ai-for-
ordering-flowers/
10
Let's Learn Artificial Intelligence - Step-Up Module
Art
AI is being used for generating art. One such method is the generative adversarial net-
work (GAN), using two AI neural networks: a generator, which produces images, and a
discriminator, which judges the paintings based on the data of existing paintings and
artistic styles.
EXPLORE
https://www.youtube.com/ https://www.youtube.com/watch?v=I-
watch?v=kRS3NZbPRW8 EIVlHvHRM
https://www.ted.com/talks/pierre_barreau_how_ai_could_compose_a_
personalized_soundtrack_to_your_life/transcript
Data Security
AI is helping CyberSecurity professionals stay ahead of hackers and other security
threats. It is being used to make Data secure; for example, AEG bot, AI2 Platforms are
used to determine software bug and cyber-attacks in a better way.
EXPLORE
https://emerj.com/ai-sector-overviews/ https://www.youtube.com/
artificial-intelligence-and-security- watch?v=MXHfD9a45DU
applications/
11
Social Media
AI can organize and manage massive amounts of data on social media sites. It can
analyze lots of data to identify the latest trends, hashtag, and requirements of different
users.
EXPLORE
https://medium.com/humansforai/the- https://www.youtube.com/
impact-of-artificial-intelligence-on-social- watch?v=udECXLtsBbM
media-579345b6f751
Chatbots
AI-powered Chatbots are software that can simulate a conversation (or a chat) with a
user in natural language. This may be done through messaging applications, websites,
mobile apps or through the telephone. This helps businesses save time and costs.
EXPLORE
https://www.ibm.com/cloud/ https://discover.bot/bot-talk/beginners-
learn/chatbots-explained guide-bots/how-chatbots-work/
Welfare
AI is being put to use for a variety of social welfare works, including health, environmental
sustainability, and public welfare.
EXPLORE
https://www.aiforpeople.org/projects/
https://cra.org/ccc/artificial-
intelligence-social-good-
speakers/
https://www.ted.com/talks/ali_kashani_a_
friendly_autonomous_robot_that_
delivers_your_food
12
Let's Learn Artificial Intelligence - Step-Up Module
Automobiles
AI is, of course, being used in self-driving cars. Apart from that, it is also being used for
driver safety and assistance, predictive car maintenance, manufacturing process, to
name a few.
EXPLORE
https://www.techradar.com/ https://emerj.com/ai-sector-overviews/ai-in-the-
news/self-driving-cars automotive-industry-an-analysis-of-the-space/
https://www.ted.com/talks/sajan_saini_how_do_
self_driving_cars_see
Aviation
AI is being used as surrogate operators for flight crafts, in combat and training
simulators, to support systems for decision making and management of missions.
The AI is then used for post-processing of the simulator data and offering summaries.
EXPLORE
https://www.youtube.com/watch?time_continue=9&v=D8NlYPtPgwA&feature=emb_
logo
Deepfakes
These are programs that animate the face of a target person, transposing the facial
expressions of another person on it. The technology includes animating and synching
the lips of people. Some famous past examples include Deepfakes of Barack Obama
and Vladimir Putin.
EXPLORE
https://www.youtube.com/ https://www.ted.com/
watch?v=FCCisdXNqHU&feature=emb_logo talks/danielle_citron_how_
deepfakes_undermine_truth_
and_threaten_democracy
13
ACTIVITY 1
Create an infographic showing how AI might be used in the future. Choose any
of the areas mentioned above.
Include the following information about your chosen area on the infographic:
• Current ways AI is used in the industry
• Ways AI can be used in the future for this industry
• Current and future benefits of AI usage to this industry
Students will use at least three different sources and cite each at the bottom of
the infographic.
14
Let's Learn Artificial Intelligence - Step-Up Module
ACTIVITY 2
Now pick up at least three applications from the cases we have seen above and
answer the following questions for each of them:
1. What was the objective of the project?
2. What AI abilities or capabilities are being used in this example?
3. What is the process you think must have been followed while creating
this solution?
4. What are the benefits of using AI in this example?
5. What are the Risks and Considerations do you think that one has to
keep in mind while developing and using this AI solution?
6. What are the requirements for developing an AI and categorize them into:
a. Data b. Hardware c. Software d. Platforms e. Others
Once you have answered the questions for the cases, combine your answers for
the six questions in a master list. Now compare your list with that of your friends
and other course mates. Discuss the cases and answers with them, expand and
refine your list.
While answering the above, bear in mind that AI working involves fast and contin-
uous processing of large amounts of data and intelligent algorithms, where the
software automatically learns from patterns or features in the data.
AI is a broad field that includes several theories, methods and technologies, as
well as the following major domains:
Natural
Cognitive Computer Language
Computing Vision Processing
(NLP)
15
Machine Learning is the part of AI that involves automating analytical model
building. It uses various methods such as neural networks, statistics, operations
research and physics in its attempt to find hidden insights in given data without
explicitly being programmed for what conclusions to reach and the path to follow.
Neural Network can be defined as a type of machine learning comprising
interconnected units (like human brain neurons) used to process information
using an algorithm that allows the computer to learn by incorporating new data.
The network of units responds to external inputs, relaying information between
each unit. The process is iterative, in that it requires multiple passes at the data to
find connections and derive meaning from undefined data.
Deep Learning uses massive neural networks with many layers of processing
units, relying on considerable computing power along with improved training
techniques to learn complex patterns in large amounts of data. Typical applications
include computer vision and speech recognition.
Cognitive Computing is that subfield of AI which strives for natural, convincing,
human-like interaction with machines. The primary objective of using AI and
cognitive computing is for a machine to simulate human behaviour and mental
cognitive processes using its ability to interpret images and speech – and then
respond coherently and appropriately.
Computer Vision uses pattern recognition from data and deep learning neural
networks for image recognition in pictures or video. As machines process, analyze
and understand images, they are expected to capture images or videos in real-
time and interpret their surroundings.
Natural Language Processing (NLP) is the subfield of AI that aims to develop
the ability of computers to analyze, understand, process and generate human
language, including speech for various purposes. The subsequent stage of NLP
may be seen as natural language interaction, where humans can communicate
with computers using everyday language, intelligently.
Additionally, several technologies enable and support AI, such as:
Graphical Application
Processing Internet of Advanced Programming
Units Things (IoT) Algorithms Interfaces
(GPU) (API)
16
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
https://education. https://www.youtube.com/watch?time_
microsoft.com/en-us/ continue=391&v=OVvTv9Hy91Q&feature=emb_
course/e328f120/8 logo
17
1.3 PROCESS OF BUILDING AN AI
The core process of developing an AI using machine learning primarily remains similar
for various applications. It may involve minor variations based on the context, purpose
and approach. Let us look at the process steps:
Here are some videos that will give you an explanation on the same. You will see that
while different people may name and categorize the operations involved differently, it
follows the same core process.
EXPLORE
https://www.youtube. https://www.youtube.com/watch?time_
com/watch?time_ continue=152&v=nKW8Ndu7Mjw&feature=emb_
continue=295&v=SGg5i0kQx- logo
8&feature=emb_logo
18
Let's Learn Artificial Intelligence - Step-Up Module
2 Computer Programming
5 Data Science
6 Algorithms
7 Databases
8 Ethics
9 Cloud Computing
So, if you are eager to start building AI, you will have to gain a deeper understanding
of various fields of AI and develop skills within these areas. Let us now continue our
journey to acquire some of these skills.
19
PROJECT: GRAPHICAL PROGRAMMING AND AIR
DRAW
In the base module, you were also introduced to graphical programming using Scratch.
Let us revisit some of its concepts before we begin our first project.
You can find more details about the basics of graphical programming at ATL Gaming
Module:
https://atlgamingmodule.in/courseInfo/1
20
Let's Learn Artificial Intelligence - Step-Up Module
STAGE
The stage is a background or a backdrop for your projects (the programs you’ll create).
The Stage is where the Sprite moves, draws, and interacts with other sprites and/or
hardware.
SPRITE
A Sprite is an object or a character that performs different actions in the projects. It
understands and obeys the instructions that you’ll give them in your projects. It can
move and can be moved to any place in the stage (you can try by clicking on them and
moving them around).
21
BACKDROP
It is similar to Sprite, but with some limitations like it cannot move. It has its own set of
scripts, images, and sounds.
BLOCKS
Blocks are like jigsaw puzzle pieces that fit into each other. They are predefined
commands used to create programs by simply dragging and dropping them below
one another in the scripting area.
SCRIPT
A script is a program or a code in Scratch based graphical programming lingo. It is
a set of ‘blocks’ that are arranged below one another in a specific order to perform a
task or a series of tasks.
The scripting area is where you will create your scripts.
22
Let's Learn Artificial Intelligence - Step-Up Module
You can learn about how to set up the platform, the installation process, and creating
the account from the links provided above.
Scratch can also be used for physical computing and can be linked to computers such
as Microbit, Arduino and Raspberry Pi.
The links for learning about these are given below:
EXPLORE
https://scratch.mit.edu/microbit https://create.arduino.cc/projecthub/
mjrobot/physical-computing-
scratch-for-arduino-72661a
https://www.makeuseof.com/tag/
raspberry-pi-scratch/
Besides, we would like you to explore another similar graphical programming language
called Snap.
ABOUT SNAP
Snap! (formerly BYOB) is a visual, drag-and-drop programming language. It is an
extended reimplementation of Scratch (a project of the Lifelong Kindergarten Group
at the MIT Media Lab) that allows you to Build Your Own Blocks. It also features first
23
class lists, first class procedures, and first class continuations. These added capabilities
make it suitable for a serious introduction to computer science for high school or
college students.
EXPLORE
Now let us work on a simple AI project called Air Draw and experience this on various
platforms to explore how different programs and platforms can be used to achieve the
same result.
PROJECT DESCRIPTION
Control a pen using your hand gesture and draw characters on the stage in real-time.
LEARNING OUTCOMES
1. Understand how graphical programming works.
2. Understand programming concepts like loops and conditional statements.
3. How to use a pre-trained model in your project.
REQUIREMENTS
Laptop/Computer with a camera/webcam along with internet connection
24
Let's Learn Artificial Intelligence - Step-Up Module
3. Stretch
While doing this project using Stretch 3, we need to add a Facemesh2Scratch
extension.
ART is in the AIR; literally. Learn to draw your favorite pictures in the air using the tip of
your finger and trace it on your computer screen. Let’s begin!
25
2. Next, click on the Add Extension button and add Human Body Detection
extension.
3. The face detection models will be downloaded, which may take a few seconds
to load.
26
Let's Learn Artificial Intelligence - Step-Up Module
4. Once downloaded you will get a message saying “Model loaded successfully”.
5. Next, we need to add the Pen Extension to draw on the stage.
6. Again, click on the Add Extension button and select Pen extension.
27
2. We will need to make a few changes in the costume of the Pencil Sprite. Select
the Costume Palette to edit the costume.
3. Next, drag the pencil into the costume area, such that the tip of the pencil should
be on the center dot.
28
Let's Learn Artificial Intelligence - Step-Up Module
5. Click on Choose A Sprite button and select the Paint option. Use a rectangle and
a text tool to make the sprite.
11. The stage is set. Click the green flag to get the camera feed on the stage.
29
Recognize Image
30
Let's Learn Artificial Intelligence - Step-Up Module
1. Click on the Pen Up sprite and place when this sprite clicked block into the
scripting area.
2. Next, place a broadcast () block from the events palette below it. Select message1
from the drop-down.
3. Next, go back to the Pencil sprite, place the when I receive () block into the
scripting area. Select message1 from the drop-down.
4. From the Pen extension, snap the pen up block below it.
31
5. Repeat the steps 1 to 4 for the other two buttons too. There will be a few changes
that you’ll need to make.
6. In the broadcast () block of the Pen Down Sprite select New message and type
message2.
7. Into the Pencil sprite, select message2 from one of the two when I receive ()
block.
8. Remove the pen up block and snap the pen down block below it.
9. Into the Erase All sprite, select New message and type message3 in the
broadcast () block.
32
Let's Learn Artificial Intelligence - Step-Up Module
10. Finally, go to the Pencil sprite, select message3 from the when I receive () block.
11. Remove the pen up block and snap the erase all block below it.
Now, as the script is complete, let’s understand the logic behind it, i.e. how it works.
As soon as you press the green flag, the program will start executing and it turns
ON the camera/webcam to search the hand. As per our script/code, the Pencil sprite
will continuously follow the x and the y coordinates of the top of the index finger.
Next, when you press Pen Up, the pen will be deactivated, and thus, the pencil will
stop drawing/writing. On pressing Pen Down, the pencil will start writing again and
the Erase All button is to clear the mess and keep your screen clean to redraw the
wonderful creations.
Now you’re all set to make your surroundings your canvas and try making various
shapes, numbers, or alphabets. You can also record the output by pressing the Record
button and can share your creations with your friends and family.
33
STEPS WITH MACHINE LEARNING FOR KIDS PLATFORM
Similarly, we can create the above project with another platform, i.e. Machine learning
for Kids. This time let’s try to draw using Nose in place of the Index finger.
34
Let's Learn Artificial Intelligence - Step-Up Module
Repeat the steps from 1 to 6 under the heading Adding the Sprites from the first
platform to add the pencil sprite and the below 3 buttons.
35
Setting Up the Stage
15. The stage is set. Click the green flag to get the camera feed on the stage.
36
Let's Learn Artificial Intelligence - Step-Up Module
Repeat the steps from 1 to 11 under the heading Activating the Buttons from the first
platform to activate, i.e. assign actions to button.
Now, that the script is completed, let’s have a look at the logic behind it, i.e. how it
works. It’s almost similar to the previous project, the only difference is that we are
using a different platform; hence the blocks and extensions are different. Also, in this
project, we are using our Nose to draw the images.
37
Now make your nose the brush and try making various shapes, numbers or alphabets
on the platform of your choice.
38
Let's Learn Artificial Intelligence - Step-Up Module
3. Next, click on the Add Extension button and add Facemesh2Scratch extension.
39
Adding the Sprites
Repeat the steps from 1 to 6 under the heading Adding the Sprites from the first
platform to add the pencil sprite and the below 3 buttons.
The stage is set. Click the green flag to get the camera feed on the stage.
40
Let's Learn Artificial Intelligence - Step-Up Module
5. Place people count reporter block from the Facemesh2Scratch Palette into the
first input and write 0 in the second input of the greater block.
6. Drag and drop set x to () block from the Motion Palette into the if arm.
7. Place x of person no: (), keypoint no: () block from the Facemesh2Scratch Palette
into the input space. Choose 1 from the first drop-down and 5 from the second.
8. Finally, place a set y to () block from the Motion Palette.
9. Drag and drop y of person no: (), keypoint no: () block from the Facemesh2Scratch
Palette into the input space. Choose 1 from the first drop-down and 5 from the
second.
41
Activating the Buttons
Repeat the steps from 1 to 11 under the heading Activating the Buttons from the first
platform, to activate, i.e. assign an action to buttons.
Now, that the script is completed, let’s have a look at the logic behind it, i.e. how it
works. It’s almost similar to the previous two projects, the only difference is that we are
using a different platform; hence the blocks and extensions are different.
Now make your nose the brush and try making various shapes, numbers or alphabets
on the platform of your choice.
42
Let's Learn Artificial Intelligence - Step-Up Module
As you can see from this project, the same project can be coded and run with variations,
on different platforms to achieve the same outcome. Similarly, there are many different
platforms, approaches, service providers, APIs and IDEs, which one can use to achieve
the project outcome. It is always advisable to learn about the various options available
and develop skills in using these, to pick the ones most suited to the objective and
other requirements.
Explore More
Make scripts to track various body parts one by one and brainstorm some real-world
applications you can create by tracking them individually or simultaneously.
Conclusion
In this segment, we first learned about graphical programming fundamentals. Then
we explored various graphical programming platforms available. Finally, in the end,
we made a fun AI-based Air Draw project using different platforms. Now, before
moving onto the next unit, you can explore possibilities with AI and make real-world
applications based on learning from this unit.
43
ANSWERS TO "BASE MODULE RECAP QUIZ"
Answer - 1
Answer - 2
44
Let's Learn Artificial Intelligence - Step-Up Module
Answer - 3
Answer - 4
CLUES ANSWER
ACROSS It is the process of finding out the Modelling
relationships between different parts of the
data and the patterns that can be recognized
in it.
45
DOWN It is a set of step-by-step instructions to solve a Algorithm
problem created along with rules that decide
which path to take when a decision is faced.
DOWN It is a number system that only uses two digits Binary system
0 and 1. Computers process information using
this system.
Answer - 5
Algorithms are step-by-step instructions to solve a problem, along with rules that
decide which path to take when a decision is to be made. When trying to solve
a problem, we may find more than one solutions. A different Algorithm, can be
built from each solution. Logical reasoning determines if algorithms will work by
predicting what happens when the algorithm’s steps, and the rules they consist
of, are followed. Algorithms can then be represented in the form of a flowchart.
46
Let's Learn Artificial Intelligence - Step-Up Module
SUMMARY
Some areas where AI is currently being used are Healthcare, Astronomy, Agriculture,
Education, Entertainment, Business, Art, Data Security, Social Media, Chatbots,
Welfare, Automobiles, Aviation, Deepfakes.
AI is a broad field that includes several theories, methods and technologies, as well
as the following major domains, such as A neural network, Deep learning, Cognitive
computing, Computer vision, Natural language processing (NLP).
Additionally, several technologies enable and support AI, such as, Graphical processing
units (GPU), Internet of Things (IoT), Advanced algorithms, Application Programming
Interfaces (API)
The core process of developing an AI using machine learning primarily remains similar
for various applications. It may involve minor variations based on the context, purpose
and approach. Let us look at the process steps:
1) Identify the problem
2) Acquire relevant data
3) Prepare the data
4) Analyze the data using a model
a. Choose the algorithms
b. Train the model
c. Evaluate the model
5) Choose a particular programming language
6) Run on a selected platform
47
• A Sprite is an object or a character that performs different actions in the projects.
It understands and obeys the instructions that you’ll give them in your projects.
It can move and can be moved
• The Backdrop is similar to Sprite, but with some limitations like it cannot move.
It has its own set of scripts, images, and sounds.
• Blocks are like jigsaw puzzle pieces that fit into each other. They are
predefined commands used to create programs by simply dragging and
dropping them below one another in the scripting area.
• A script is a program or a code in Scratch based graphical programming
lingo. It is a set of ‘blocks’ that are arranged below one another in a specific
order to perform a task or a series of tasks.
48
Let's Learn Artificial Intelligence - Step-Up Module
49
50
UNIT 2
ETHICS IN AI
Topic Covered
What does ‘Ethical AI’ mean? | Fairness | Robustness |
Privacy | Explainability | Governance
2.1 WHAT DOES “ETHICAL AI” MEAN?
Humans have something called “a moral compass”. It
is an area of our cognition (mental processes of gaining
knowledge and understanding) which tells right from Good Evil
wrong. When you see an injustice, your brain tells you
something is not right.
Progress Regress
The standards of your moral compass strongly depend
on your upbringing and environment.
This 'moral compass' and environment is also what
companies build their ethics on, i.e. to decide what is
right and what is wrong.
AI is a technology that could affect public values, human
rights or public decision-making and hence there is a
strong need for ethics here.
In the same way that parents raise a child, companies must teach and test their AI
systems to learn, communicate, make unbiased decisions and comply with norms
and regulations.
Artificial Intelligence is a very vast field, and today most of the AI code developed
across the world falls under the bucket of Artificial Narrow Intelligence (ANI). In this
Unit, we will discuss the ethical considerations with respect to ANI.
Ethical AI pillars are as below:
FAIRNESS ROBUSTNESS
EXPLANABILITY PRIVACY
52
Let's Learn Artificial Intelligence - Step-Up Module
2.2 FAIRNESS
AI Systems should be built such that they are fair and inclusive for all. Impartial behavior
of AI model without any discrimination should be our goal. Unfair models can create
improper outcomes,
For example, the AI model for face recognition identifies a person as a criminal
incorrectly - this will lead to a loss of that person’s reputation and false criminal charges
against the person.
Unfair models can injure people, damage reputations and become legal liabilities. The
consequences of biased AI systems could be an injustice at a large scale.
Ensuring Fairness in AI is hard because of below 3 points:
1. There is pre-existing bias in the data that is collected from the real-world.
Example: Gender bias - when you search for 'Doctor' on a search website, the
results for Doctor images might mostly come up as 'male'. Whereas for 'Nurse'
most results would be 'female'.
This shows inherent bias in the search results. Now, if you train your AI models
using this dataset, the AI model will be biased too and learn that to be a Doctor,
being male is an important criterion.
Doctor Nurse
Hence, we need to ensure that such bias is identified before the AI model is trained.
53
2. Ensuring that the AI system is fair across all the circumstances and scenarios
is a challenge. Even thorough training and testing of the AI systems cannot
guarantee this.
There will always be situations where only after the launch of the AI system,
unfair and unintentional blind spots get revealed.
Example: – An AI model is trained on 1000 different scenarios in which a car
can meet an accident with another car. It is very good at identifying which
car’s driver was at fault and caused the accident if one of these 1000 scenarios
come up. But after launch, a new scenario comes up that was not foreseen.
Hence, the AI model will need retraining and until then, it might be unfair.
3. When it comes to fairness, there is no single approach. What might be fair for
one person might not be fair for another. In such cases, the need would be
to identify the Fairness Criteria for building the AI system by considering the
historical, ethical, social, legal, political and maybe even more scenarios. See
picture below to get an understanding of what we mean.
As you can see and understand now, implementing fairness is very important
for Artificial Intelligence systems.
And below are some recommended practices that everyone can follow to get
a fair AI system in place.
54
Let's Learn Artificial Intelligence - Step-Up Module
4: Which of the following is one of the primary causes for unfairness in any AI model?
A) AI models can remember data about individuals and give subjective results
B) AI models can’t be explained easily
C) If the data itself is biased then any model trained on such data will also become
biased
D) None of the above
55
And below are some recommended practices that everyone can follow to get a fair
AI system in place.
Chalk out Take inputs from experts ranging from Industry (Insurance,
exact goals Healthcare, etc.) to Humanities (lawmakers, ethicists, etc.) to
for ensuring consider different thought processes.
fairness and
Set goals for an AI system to be fair for a list of anticipated scenarios.
inclusion
Goals will be monitored, and the list will be expanded over time
(based on feedback loop)
Analyze Take feedback from users after testing your AI model on real-
performance – world scenarios.
Real-world The users and scenarios should be from across different industries,
scenarios groups etc.
56
Let's Learn Artificial Intelligence - Step-Up Module
2.3 ROBUSTNESS
AI models are highly susceptible to all sorts of attacks, including many based on
adversarial AI methods. In these methods, adversaries exploit AI models for potential
gain, in most cases, by predicting the decisions machine learning models will make
and then manipulating subsequent sets of data to produce the attacker’s desired
outcomes—rather than the correct decisions.
The accuracy of AI models is directly correlated to their vulnerability to small variations
on the input dataset. Testing and benchmarking AI models against adversarial attacks
is key to establishing trust in AI systems.
For example, the original image below was classified by the AI model as Panda, which
is correct. Once an adversary adds an input in the dataset that is random noise, any
human being would still classify the image as a Panda. But the AI model, which works
at pixel levels for images, gets confused and classifies the image as a 'Gibbon'.
The question is – what can we do to ensure AI models do not break due to such
adversarial attacks?
+ =
A regular AI model development flow looks like the one below and does not include
any checks or fixes to ensure the Robustness of a model.
57
We can alter the AI model development pipeline to include attack scenarios when
training the AI model. This will teach the defense mechanisms to the AI models at
the time of training. So, when the model is deployed in the real world, it will be able to
handle the attacks much better. Like shown in the diagram below.
For example, before a model is trained for Robustness, it can be fooled into accepting
that the STOP sign on the left is a Green signal. This can create hazardous situations in
the real world, where we might have self-driving cars on the road, relying on AI models
to read the STOP signs. And will end up reading them as Green signals instead.
58
Let's Learn Artificial Intelligence - Step-Up Module
But once the AI model is trained with Robustness in mind, it will be able to identify the
attack and give a correct result.
1: When should we carry out the Robustness check in the AI model development
pipeline?
A) Before we build the AI Model
B) Before the data ingestion phase
C) After we build the AI Model
D) After data ingestion phase
4: Which of the below scenarios are examples of probable adversarial attacks on the
AI system? (Select 2)
A) AI Model recognizing the image of a Panda as a Gibbon
B) AI Model recognizing a Red Traffic signal as a Green Traffic Signal
C) Human recognizing the image of a Panda as a Gibbon
D) None of the above
59
Given below are some recommended practices that everyone can follow to achieve a
robust AI system.
Potential Make a list of threats (that could happen) to the AI system and
threats to the identify the problems if those threats do happen.
system
Understand the likelihood and severity of these problems.
60
Let's Learn Artificial Intelligence - Step-Up Module
2.4 EXPLAINABILITY
AI models are like a black box. There is little visibility on how AI systems make decisions
and the reasons behind the AI model predictions. Due to this lack of transparency,
businesses had started to hesitate to utilize this technology.
Explainability has led to the opening of this AI black box leading to transparency of
the decision-making process of the AI models. Explainability is vital to being able to
question, understand, and trust AI systems.
AI systems can now list information about features and the values of these features
that went into its prediction/decision-making process. Hence, enabling human beings
to look inside the black box and gain insights into the decision-making process of AI
models. The below diagram explains the concept very nicely.
61
Explainable AI systems are available, which can:
Explain – rationale; the reasoning, whenever needed
Characterize – strengths and weaknesses
Compare – with other AI systems
Convey – an understanding of how they will behave in the future
Make – decisions smarter by augmenting humans with machines
One of the techniques used for explainability is called LIME (Local Interpretable Model-
Agnostic Explanations). It attempts to understand the model by changing the input of
data samples and understanding how the predictions change.
It modifies a single data sample by tweaking the feature values and observes the
resulting impact on the output/prediction. See picture below.
62
Let's Learn Artificial Intelligence - Step-Up Module
63
And below are some recommended practices that everyone can follow to build an
explainable AI system.
Chalk out Engage with the Industry experts (banks, healthcare, etc.) of your
options to AI system and decide on the exact list of features that are needed
perform to be explained.
Explainability
Understand the limitations you may have, e.g. less access to data
if working with sensitive data etc.
Explainability Explainability is an important part of the user’s experience
is an important Discuss at regular intervals with the AI system users on their
part of needs, i.e. the list of features they need to be explained etc.
the user’s
experience
Design AI Use the simplest model with the smallest set of inputs necessary
Model to be to meet your performance goal of understanding the factors that
Explainable are affecting your AI model
Understand Analyze how your AI model behaves for different data, for different
the trained subsets of examples.
model
Look at how even minor differences in data impacts your AI
model.
64
Let's Learn Artificial Intelligence - Step-Up Module
2.5 PRIVACY
When AI systems are to be built using sensitive data, we need to consider the privacy
implications in using it.
Legal & regulatory requirements, social norms and individual expectations should be
obeyed.
If AI models remember or can reveal the sensitive data that they have used, rules
should be put in place to ensure that the data is safe.
The possibility that AI models reveal data can be decreased by applying various
techniques.
For example, personally identifiable information, or PII, is any data that could potentially
be used to identify a person. Examples include a person’s picture, full name, social
security number, driver’s license number, bank account number, passport number
and email address. Identity theft is a big consumer concern. And identity thieves can
piece together PII information from various sources and cause harm to the consumers.
There are various rules and regulations across the globe to protect data privacy. For
example, The General Data Protection Regulation (GDPR) is a regulation in European
Union (EU) law on data protection and privacy for all individuals within the EU and the
European Economic Area (EEA). It requires companies to protect the privacy of their
EU customers.
Hence the appropriate measures must be put in place to prevent privacy breaches.
Examples of anonymizing PII data in images – faces and car license plates.
65
Let’s go through an exercise to understand the topic better:
1: Select examples of sensitive data from the options below (Select 2):
A) Images collected of cats and dogs
B) Images collected of people
C) Images collected of trees and plants
D) Images collected of car number plates
66
Let's Learn Artificial Intelligence - Step-Up Module
And below are some recommended practices that everyone can follow to ensure Data
Privacy.
67
2.6 GOVERNANCE
Artificial Intelligence algorithms are expected to carry out decision making across
several sections of our society. Hence it is vital to ensure that the complete AI Operations
lifecycle is continuously monitored and vigilantly governed. The model should also be
enhanced on the basis of basis new situations encountered in the real world.
A typical AI Ops (or AI Operations) delivery lifecycle can be seen in the diagram shown
below.
Start
Along with the AI model delivery lifecycle, we need to ensure that the AI system is kept
accountable. A few of the below points can be put in place to ensure accountability:
1. Plan for doing audits, impact assessments or thorough governance via rules
and regulations.
2. For high-risk systems, Governance activities carried out by a human being could
also be proposed as a model for accountability.
3. Also, multiple research groups help identify AI systems that are unfair or
discriminatory and try to ensure that there is accountability and action taken
68
Let's Learn Artificial Intelligence - Step-Up Module
APPENDIX
69
ANSWERS TO QUIZS
Question Answer
1 C
2 D
3 A
4 C
5 B
Question Answer
1 C
2 B
3 A
4 A and B
5 A
Question Answer
1 C
2 A
3 B
4 A
5 A
70
Let's Learn Artificial Intelligence - Step-Up Module
Question Answer
1 B and D
2 D
3 C
4 D
5 A
SUMMARY
AI Systems should be built such that they are fair and inclusive for all. Impartial
behavior of AI model without any discrimination should be our goal. Unfair models
can create improper outcomes. Unfair models can injure people, damage reputations
and become legal liabilities. Recommended practices to ensure fairness are:
• Chalk out exact goals for ensuring fairness and inclusion
• Representative datasets should be used
• Analyze performance using Real-world scenarios
71
Recommended actions to ensure robustness
• Potential threats to the system
• Approach to overcome the threats
• Learn and stay updated on the latest security techniques
Explainability has led to the opening of this AI black box leading to transparency of
the decision-making process of the AI models. Explainability is vital to being able to
question, understand, and trust AI systems. Recommended actions for explainable ai
• Chalk out options to perform explainability
• Explainability is an important part of the user’s experience
• Design ai model to be explainable
• Use a metric that shows the performance of the end goal
• Understand the trained model
When AI systems are to be built using sensitive data, we need to consider the privacy
implications in using it. Legal & regulatory requirements, social norms and individual
expectations should be obeyed. There are various rules and regulations across the
globe to protect data privacy. Recommended actions to ensure data privacy
• Handle data responsibly
• On-device / different setting
• Processing of data
• Safeguard the privacy of ai models
Along with the AI model delivery lifecycle, we need to ensure that the AI system is kept
accountable.
72
Let's Learn Artificial Intelligence - Step-Up Module
73
74
Let's Learn Artificial Intelligence - Step-Up Module
UNIT 3
MACHINE LEARNING
Topic Covered
Introduction to Machine Learning | Gathering Data | Preparing the Data
Choosing the Model | Neural Networks and Deep Learning |
Introduction to Maths for ML | Training | Evaluating | Parameter Tuning |
Prediction | Machine Learning as a Service
75
3.1 INTRODUCTION TO MACHINE LEARNING
Artificial Intelligence has a wide variety of applications. As you engage with AI, you will
learn that while some projects are relatively simpler, others can be far more complex.
In the Air Draw exercise, we saw how the computer could mimic human action by
the following movement. This is a very simple application compared to say a robot
following you overcoming physical obstacles and finding their way in the real world.
The capabilities of machines to make decisions, learn and act like humans on their
own, is a result of machine learning and deep learning. We shall learn more about it
and its various facets in this Unit.
In this Unit, you will learn many new terms that form the core of the Artificial Intelligence
building process. It will be an excellent idea to maintain a record of the new terms that
you are learning and explore more about them on the Internet.
Let us begin now, by understanding what the difference between artificial intelligence
and machine learning is.
The relation between Artificial Intelligence (AI) and Machine Learning (ML)
As we know, AI involves machines that can perform tasks akin to human intelligence.
Typically this includes planning, recognizing objects and sounds, learning, predicting,
understanding language, and problem-solving. There are many techniques through
which it can do so. Machine Learning is the technique where algorithm or deep
learning is used to build AI rather than the use of pure hard-coded programming.
Machine learning is a subset of AI (see diagram).
76
Let's Learn Artificial Intelligence - Step-Up Module
Arthur Samuel (an American pioneer in the field of artificial intelligence) coined the
phrase Machine Learning in 1959, defining it as “the ability to learn without being
explicitly programmed.” Technically we can build an AI without using machine
learning, but this would involve writing millions of lines of code with complex rules
and decision-trees.
77
So, by using the method of ‘training’ an algorithm so that machines can learn, we
avoid hard-code software routines where specific instructions are given to accomplish
a particular task.
‘Training’ as per ML involves feeding vast amounts of data to the algorithm and
allowing the algorithm to adjust itself and improve.
Example:
Expert systems are another one, wherein computers are programmed with
rules that enable them to behave like human experts in a particular domain, for
example, an autopilot system flying a plane.
78
Let's Learn Artificial Intelligence - Step-Up Module
Gathering
Data
Preparing Prediction
that Data
The machine
learning process
involves the
following steps:
Choosing a Hyperparameter
Model Tuning
Training Evaluation
EXPLORE
As an introduction to the key steps, you can go through the following video:
https://www.youtube.com/watch?time_continue=103&v=3TD0p_f6bZU&feature=emb_
logo
79
3.2 GATHERING DATA
The machine learning process starts with data.
Data could be in various forms, such as numbers,
text, images, audios, videos, etc. Data gathering
can be an expensive and time-consuming aspect
of any machine learning project. Lack of care at
this stage can result in failure.
There is a phrase, ‘Garbage in, Garbage out’ (GIGO)
which means if the input is poor, the output will
also be poor. GIGO is as valid for machine learning
as with any other data-driven effort.
Hence it has to be done with planning, care and
in an organized manner.
80
Let's Learn Artificial Intelligence - Step-Up Module
(Source: https://www.cloudfactory.com/data-labeling-guide)
81
3.2.3 WAYS TO COLLECT DATA
Data collection methods could be divided into primary and secondary methods:
1 Direct observation
Data coverage
Data coverage is the distribution of data across target classes or values, as well as
across other variables that contribute to variation. Target classes refer to what you
are looking to detect or predict. For example, if we are building a model to detect
if a machine is performing normally or is displaying a particular kind of error, then
‘Normal’ and ‘Error’ are the target classes.
Data has to be collected keeping in mind the sources, variation in data and the different
variables.
The purpose of data coverage planning is to make sure that we collect sufficient data
to capture and overcome the variation embedded in what we are trying to measure.
It requires a statistically significant number of observations taken from a sufficient
number of combinations of different target classes and variables.
82
Let's Learn Artificial Intelligence - Step-Up Module
Humans generate a massive amount of data these days, in a wide variety of areas.
83
However, with respect to big data, good data is certainly better than more data. There
are limits to what a clever machine learning algorithm can do with unstructured or
poor-quality data. There is no substitute for collecting information correctly at the
outset.
Listed below are tools that enable users to source large volumes of raw data quickly.
• Data Scraping Tools: Web scraping describes the automated, programmatic
use of an application to extract data or perform actions that users would
usually perform manually, such as social media posts or images. The following
companies offer tools to extract data from the web:
Octoparse: A web scraping tool that lets users obtain public data without
coding.
Mozenda: A tool that allows people to extract unstructured web data
without scripts or developers.
84
Let's Learn Artificial Intelligence - Step-Up Module
• Data Labeling Tools: If you’re building a machine learning model, chances are
you’re going to need data labelling tools to put together datasets and ensure
high-quality data production quickly. It includes data tagging, classification,
moderation, transcription, or processing.
Given below are some open-source data labeling tools for various types of data.
EXPLORE
https://recogito.github.io/ https://labelbox.com/
annotorious/
http://labelme.csail.mit.edu/ https://sloth.readthedocs.io/en/
Release3.0/ latest/
EXPLORE
https://www.fon.hum.uva.nl/ https://github.com/felixbur/
praat/ Speechalyzer
https://aubio.org/
85
EXPLORE
https://rasa.com/docs/rasa/nlu/ https://github.com/dennybritz/
about/ bella
https://stanfordnlp.github.io/ https://www.tagtog.net/
CoreNLP/
• Open-Source Datasets: Another way to obtain raw data for machine learning
is to obtain pre-built, publicly available datasets on the Internet. There are
thousands of publicly available datasets spanning a wide range of industries
and use cases.
Here are a few of the Indian Government and open-source datasets that you can use.
There are many more that you can search for on the Internet.
EXPLORE
https://data.gov.in/
https://bhuvan-app3.nrsc.gov.in/
data/download/index.php
http://www.surveyofindia.gov.in/
https://dbie.rbi.org.in/DBIE/dbie.
rbi?site=home
86
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
Open Source:
https://www.kaggle.com/ https://www.imf.org/en/Data
datasets
https://data.worldbank.org/ https://trends.google.com/trends/?
q=google&ctab=0&geo=
all&date=all&sort=0
87
3.3 PREPARING THE DATA
Once the data has been collected, it is in a raw form. Raw data is collected from various
sources and is usually unsuitable for analysis by itself. For example:
There might be many duplicate entries.
The data may have typographical errors.
There may be missing data.
The data may be available in different formats.
With an uncleaned dataset, no matter what type of algorithm you try, you will never
get accurate results. That is why data scientists spend a considerable amount of time
on data cleaning; further, it has to be labeled. This is call pre-processing.
The steps and techniques for data pre-processing will vary for each dataset. But the
following steps could be used as a standard approach for any dataset type:
Identifying relevant data and removing irrelevant data
Fix Irregular cardinality and structural errors
Outliers
Missing data treatment
Data Transformation
Post the data pre-processing, comes the final data preparation phase of Data Splitting.
1. Duplicate entries
Data is usually collected from many sources and joined together to form one dataset.
This might lead to the duplicity of observations. This might not be a problem if the
observations are repeated a few times, particularly in the case of Big Data. However, if
observation(s) are repeated far too many times, it could lead to an erroneous behaviour.
Hence, it is better to remove duplicate observations to have a cleaner dataset.
88
Let's Learn Artificial Intelligence - Step-Up Module
2. Irrelevant observations:
The dataset would contain observations which might not be useful for your specific
task. For example, if you were analyzing the shopping behaviour of women only, you
would not require observations for men in your dataset. Similarly, your data might
have a whole column of data that is not useful in making predictions and could be
dropped.
89
Once the outliers are identified, then depending on the objectives, you must decide
whether the outliers must be included or excluded.
You should not drop any observations without careful consideration. You must
consider the possibility that such information can be valuable to understand the
unusual behaviour or anomalies in the data. Do not exclude unless you are sure that
the outlier may be due to measurement error or if the presence of outliers influences
the fit of a model.
After detecting the missing values, the following course of actions can be followed:
Ignore the missing values: Less than 10% missing data for a single case or
observation can generally be ignored, unless the missing data is a MAR or
MNAR. However, the number of complete cases, i.e. the observations with no
missing data, must be sufficient for the selected analysis technique if removing
the observations with missing data.
90
Let's Learn Artificial Intelligence - Step-Up Module
Drop the missing values: If the data is MCAR or MAR and the number of
missing values in a feature is very high, then that feature should be left out
of the analysis. If missing data for a particular feature or sample is more than
5%, then you probably should leave that feature or sample out. If the cases or
observations have missing values for target variables(s), it is advisable to delete
the dependent variable(s) to avoid any artificial increase in relationships with
independent variables.
Case Deletion: In this method, cases (the rows) which have missing values
for one or more features are deleted. If the cases having missing values are
small in number, it is better to drop them. Though this is an easy approach, it
might lead to a significant decrease in the sample size. Also, the data may not
always be missing completely at random. This may lead to biased estimation of
parameters.
Imputation: Imputation is the process of substituting the missing data by some
statistical methods. Imputation is useful in the sense that it preserves all cases
by replacing missing data with an estimated value based on other available
information. But imputation methods should be used carefully as most of them
introduce a large amount of bias and reduce variance in the dataset.
Scaling. Data may have numeric attributes that span different ranges, for
example, millimeters, meters, and kilometers. Scaling is about converting these
attributes so that they will have the same scale, such as between 0 and 1, or 1
and 10 for the smallest and biggest value for an attribute.
Decomposition. Sometimes finding patterns in data with features representing
complex concepts is more difficult. Decomposition technique can be applied
in this case. During decomposition, you can convert higher-level features into
lower-level ones. In other words, new features based on the existing ones are
being added. Decomposition is mostly used in time series analysis. For example,
In order to estimate the monthly demand for computers, a market research
analyst converts the data representing the quarterly demand.
91
Aggregation. Aggregation combines several features into one feature that
represents them all. For example, if you have collected basic information about
your customers, particularly their age in order to develop a demographic
segmentation strategy. You could need to distribute them into categories of
age, such as 11-20, 21-30, 31-40, etc. for this you would use aggregation to create
large-scale features based on small-scale ones. This technique will allow you to
reduce the size of a dataset without the loss of information.
• Training set. A data scientist uses a training set to train a model and define its
optimal parameters — parameters it has to learn from data.
Test set. A test set is needed for an evaluation of the trained model and its
capability for generalization. The latter means a model’s ability to identify
patterns in new unseen data after having been trained over the training data.
It’s crucial to use different subsets for training and testing to avoid model
overfitting, which is the incapacity for generalization we mentioned above.
Validation set. The purpose of a validation set is to tweak a model’s
hyperparameters — higher-level structural settings, that can’t be directly learned
from data. These settings can express, for instance, how complex a model is and
how fast it finds patterns in data.
92
Let's Learn Artificial Intelligence - Step-Up Module
The next step is selecting an appropriate model. A machine learning model is a set of
rules that recognize certain types of patterns. For example, you can train a Machine
learning algorithm with a set of data, that can reason over and learn from and become
the final model which can make predictions over data that it hasn't seen before.
Based on the type of tasks, we can classify machine learning models in the following
types:
Classification Regression
Clustering
Models Models
Deep
Dimensionality
Learning
Reduction
etc.
3.4.1 CLASSIFICATION
In the context of machine learning, classification is the task of predicting the type or
class of an object within a finite number of options. The output variable for classification
is always a categorical variable. For example, predicting an email is spam or not is a
standard binary classification task.
Classification Model could be Binary or Multi-class.
93
ML models for binary classification problems predict a binary outcome (one of two
possible classes). Examples of Binary Classification Problems
"Is this email spam or not spam?"
"Will the customer buy this product?"
"Is this comment written by a human or a robot?"
3.4.2 REGRESSION
In the machine, learning regression is a set of problems where the output variable can
take continuous values. For example, predicting the airline price can be considered as
a standard regression task.
Examples of Regression Problems
"What will the temperature be in Mumbai tomorrow?"
"How many copies of this book will sell?"
"What price will this car sell for?"
Clustering
In simple words, clustering is the task of grouping similar objects together. It helps to
identify similar objects automatically without manual intervention. We can not build
effective supervised machine learning models (models that need to be trained with
manually curated or labeled data) without homogeneous data. Clustering helps us
achieve this more smartly.
We will learn more about this in the Unit on Algorithms.
94
Let's Learn Artificial Intelligence - Step-Up Module
Dimensionality Reduction
Dimensionality is the number of predictor variables used to predict the independent
variable or target.often in the real world datasets the number of variables is too high.
Too many variables also bring the curse of overfitting to the models. In practice, among
these large numbers of variables, not all variables contribute equally towards the goal
and in a large number of cases, we can preserve variances with a lesser number of
variables.
Deep Learning
Deep learning is a model of machine learning which deals with neural networks. We
shall learn more about it in the next section
95
3.5 NEURAL NETWORKS AND DEEP LEARNING
Artificial Neural Networks (ANN) are inspired by the central nervous system that
we possess. The essential concept is that a network of artificial neurons built out of
interconnected threshold switches can learn to recognize patterns in the same way
that an animal brain and nervous system (including the retina) does.
ANN’s have been successfully applied to several problem domains:
To classify data by recognizing patterns.
To detect anomalies or novelties, when test data does not match the usual
patterns.
To process signals, for example, by filtering, separating or compressing.
To find a function that closely matches the training data (approximate a target
function) so that it can predict almost correctly.
Identify faces
Recognize speech
Translate texts
96
Let's Learn Artificial Intelligence - Step-Up Module
There are many ways of knitting the nodes of a neural network together, and each way
results in more or less complex behavior. Possibly the simplest of all topologies is the
feed-forward network. Signals flow in one direction only; there is never any loop in the
signal paths.
Basically, the learning occurs by the strengthening of the connection between two
neurons when they are active during training. This is mostly a matter of increasing
the weight values of the connections between the neurons. For this a rule called back
propagation of error, backprop, or BP is used.
97
Each has a propagation function that transforms the outputs of the connected
neurons, often with a weighted sum. The output of the propagation function goes
to an activation function. This activation function fires when input exceeds a certain
threshold value.
Perceptrons
In the 1940s and 1950s, a step activation function was used by artificial neurons, called
perceptrons. Perceptrons can be said to be the building blocks in a single layer of a
neural network. They are made up of four different parts:
A step activation function is a simple binary function that has only two possible results,
which means that it takes several real-valued inputs and gives a single binary output.
98
Let's Learn Artificial Intelligence - Step-Up Module
The function in the figure given above is called the Heaviside Step function. This
function returns 1 if the input is positive or zero, and 0 for any negative input. A neuron
whose activation function is a function like this is called a perceptron.
In the perceptron model, every input x has weight w associated with it. The weights
indicate the importance of the input in the decision-making process. A threshold
value decides the model output. If the weighted sum of the inputs is greater than the
threshold value, the output will be 1 else output will be 0. In other words, the model
will fire if the weighted sum is greater than the threshold.
A neural network, which is made up of perceptrons, can be perceived as a complex
logical statement (neural network) made up of very simple logical statements.
The logistic or sigmoid function: Sigmoid neurons are similar to perceptrons, but
they are slightly modified such that the output from the sigmoid neuron is much
smoother than the step functional output from a perceptron. A small change in the
input causes a small change in the output as opposed to the stepped output. There
are many functions with the characteristic of an “S” shaped curve known as sigmoid
functions.
For example:
99
The Hyperbolic Tangent: In neural networks, as an alternative to sigmoid function,
the hyperbolic tangent function could be used as an activation function. When you
backpropagate, the derivative of activation function would be involved in the calculation
for error effects on weights. The derivative of the hyperbolic tangent function has a
simple form just like a sigmoid function.
100
Let's Learn Artificial Intelligence - Step-Up Module
The layers are of alternating linear and nonlinear processing units and are trained
using large-scale algorithms and massive amounts of training data. A deep neural
network might have 10 to 20 hidden layers, whereas a typical neural network may
have only a few.
101
The more layers in the network, the more characteristics it can recognize. Unfortunately,
the more layers in the network, the longer it will take to calculate, and the harder it will
be to train.
Another kind of deep learning algorithm is Random Decision Forests (RDFs). Again,
they are constructed from many layers, but instead of neurons, the RDF is constructed
from decision trees and outputs a statistical average (mode or mean) of the predictions
of the individual trees. The randomized aspects of RDFs are the use of bootstrap
aggregation (bagging) for individual trees and taking random subsets of the features.
There are many ways to approach deep learning, but none are perfect, at least not yet.
There are only better and worse strategies for each application.
102
Let's Learn Artificial Intelligence - Step-Up Module
Based on the architecture of neural networks, some important deep learning models
are:
Multi-Layer perceptron
103
Recurrent Neural Networks
Boltzmann machine
104
Let's Learn Artificial Intelligence - Step-Up Module
Autoencoders etc.
EXPLORE
To learn more about deep learning and neural networks, you can go through
the following links:
https://www.freecodecamp.org/ https://pathmind.com/wiki/neural-
news/deep-learning-neural- network
networks-explained-in-plain-
english/
105
3.6 TRAINING
Training process entails 'feeding' the algorithm with training data. An algorithm will
process data and output a model that can find a target value (attribute) in new data
— an answer you want to get with predictive analysis. The purpose of model training
is to develop a model.
Two model training styles are most common — supervised and unsupervised learning.
The choice of each style depends on whether you want to forecast specific attributes
or group data objects by similarities.
Supervised learning - Supervised learning allows for processing data with target
attributes or labeled data. These attributes are mapped in historical data before
the training begins. With supervised learning, you can solve classification and
regression problems.
Unsupervised learning - During unsupervised learning, an algorithm analyzes
data that is unlabeled. The goal is to find hidden interconnections, similarities or
differences in the data. It is used for solving problems such as clustering,
association rule learning, and dimensionality reduction.
The process of training an ML model involves providing an ML algorithm (that is, the
learning algorithm) with training data to learn from. The term ML model refers to
the model artifact that is created by the training process. The training data must
contain the correct answer, which is known as a target or target attribute. The learning
algorithm finds patterns in the training data that map the input data attributes to
the target (the answer that you want to predict), and it outputs an ML model that
captures these patterns.
You can use the ML model to get predictions on new data for which you do not know
the target. To train an ML model, you need to specify the following:
Input training data source
Name of the data attribute that contains the target to be predicted
Training parameters to control the learning algorithm
Training Parameters
Typically, machine learning algorithms accept parameters that can be used to control
certain properties of the training process and the resulting ML model. In Amazon
Machine Learning, these are called training parameters. You can set these parameters
using the Amazon ML console, API, or Command-Line Interface (CLI). If you do not set
any parameters, Amazon ML will use default values that are known to work well for a
large range of machine learning tasks.
106
Let's Learn Artificial Intelligence - Step-Up Module
1 2 3 4 5
EXPLORE
https://docs.aws.amazon.com/machine-learning/latest/dg/training-parameters.html
107
3.7 EVALUATING
Once training is complete, it’s time to see if the model is any good, using Evaluation.
This is where that dataset that we set aside, for testing, comes into play. Evaluation
allows us to test our model against data that has never been used for training. This
metric allows us to see how the model might perform against data that it has not yet
seen. This is meant to be representative of how the model might perform in the real
world.
By using different metrics for performance evaluation, we should be in a position to
improve the overall predictive power of our model before we roll it out for production
on unseen data.
Without doing a proper evaluation of the ML model using different metrics, and
depending only on accuracy, can lead to a problem when the respective model is
deployed on unseen data and can result in poor predictions.
This happens because, in cases where the model does not learn but instead memorizes;
hence, it is not able to generalize well on unseen data.
To get started, let’s define these three important terms:
Learning: ML model learning is concerned with the accurate prediction of future data,
not necessarily the accurate prediction of training/available data.
108
Let's Learn Artificial Intelligence - Step-Up Module
The images below depict how solely relying on model accuracy during training, leads
to poor performance during validation.
Evaluation Metrics
Evaluation metrics are tied to machine learning tasks. There are different metrics for
the tasks of classification, regression, ranking, clustering, topic modeling, etc.
Model Accuracy
Model accuracy in terms of classification models can be defined as the ratio of correctly
classified samples to the total number of samples:
109
True Positive (TP) — A true positive is an outcome where the model correctly
predicts the positive class.
True Negative (TN) — A true negative is an outcome where the model correctly
predicts the negative class.
False Positive (FP) — A false positive is an outcome where the model incorrectly
predicts the positive class.
False Negative (FN) — A false negative is an outcome where the model incorrectly
predicts the negative class.
Though highly-accurate models are what we aim to achieve, accuracy alone may not
be sufficient to ensure the model’s performance on unseen data.
Precision = TP
(TP+FP)
High precision means that an algorithm returned substantially more relevant results
than irrelevant ones.
110
Let's Learn Artificial Intelligence - Step-Up Module
In this context, recall is defined as the number of true positives divided by the total
number of elements that belong to the positive class (i.e. the sum of true positives and
false negatives, which are items which were not labeled as belonging to the positive
class but should have been).
Recall = TP
(TP+FN)
High recall means that an algorithm returned most of the relevant results
To fully evaluate the effectiveness of a model, it’s necessary to examine both precision
and recall. Unfortunately, precision and recall are often in conflict. That is, improving
precision typically reduces recall and vice versa.
F1 Score
The F1 score is the harmonic mean of the precision and recall, where an F1 score
reaches its best value at 1 (perfect precision and recall) and worst at 0.
Why harmonic mean? Since the harmonic mean of a list of numbers skews strongly
toward the least elements of the list, it tends (compared to the arithmetic mean) to
mitigate the impact of large outliers and aggravate the impact of small ones:
F1 = 2 (Precision Recall)
(Precision + Recall)
ROC Curve
A receiver operating characteristic curve, or ROC curve, is a graphical plot that illustrates
the diagnostic ability of a binary classifier system as its discrimination threshold is
varied.
111
The ROC curve is created by plotting the True Positive Rate (TPR) against the False
Positive Rate (FPR) at various threshold settings. The true positive rate is also known
as sensitivity, recall, or probability of detection in machine learning. The false-positive
rate is also known as the fall-out or probability of false alarm.
112
Let's Learn Artificial Intelligence - Step-Up Module
4a shows the areas under two ROC curves, A and B. Classifier B has a greater area and
therefore better average performance. Fig. 4b shows the area under the curve of a
binary classifier A and a scoring classifier B.
It is possible for a high-AUC classifier to perform worse in a specific region of ROC space
than a low-AUC classifier. Fig. 4a shows an example of this: classifier B is generally
better than A except at FP rate > 0.6, where A has a slight advantage. But in practice,
AUC performs very well and is often used when a general measure of prediction is
desired.
AUC is desirable for the following two reasons:
AUC is scale-invariant. It measures how well predictions are ranked, rather than
their absolute values.
AUC is a classification-threshold-invariant. It measures the quality of the model’s
predictions irrespective of what classification threshold is chosen.
Multi-Class ROC
With more than two classes, the classifications problem become much more
complicated if the entire space is to be managed. With n classes, the confusion matrix
becomes an n · n matrix containing the n correct classifications (the major diagonal
entries) and n*n — n possible errors (the off-diagonal entries). Instead of managing
trade-offs between TP and FP, we have n benefits and n*n — n errors. With only three
classes, the surface becomes a 3*3- 3 = 6-dimensional polytope.
One method for handling n classes is to produce n different ROC graphs, one for each
class.
Specifically, if C is the set of all classes, ROC graph i plots the classification performance
using class ci as the positive class and all other classes as the negative class:
Pi = Ci
Ni = Union(Cj) for j≠i
Multi-Class AUC: Similarly, AUC can be calculated for multi-class problems by generating
each class reference ROC curve, in turn, measuring the area under the curve and then
summing the AUCs weighted by the reference class’s prevalence in the data.
EXPLORE
113
3.8 PARAMETER TUNING
Once you’ve done evaluation, you may want to further improve your training in some
way. You can do this by tuning your parameters. There may have been a few parameters
that were implicitly assumed during training. Now is a good time to go back and test
those assumptions and try other values.
One example is how many times we run through the training dataset during training.
What we mean by that is we can 'show' the model our full dataset multiple times,
rather than just once. This can sometimes lead to higher accuracies.
Another parameter is ‘learning rate’. This defines how far we shift the line during each
step, based on the information from the previous training step. These values all play a
role in how accurate our model can become and how long the training takes.
For more complex models, initial conditions can play a significant role in determining
the outcome of training. Differences can be seen depending on whether a model
starts off training with values initialized to zeroes versus some distribution of values,
which leads to the question of which distribution to use.
As you can see there are many considerations at this phase of training, and it’s
important that you define what makes a model “good enough”, otherwise you might
find yourself tweaking parameters for a very long time.
These parameters are typically referred to as ‘hyperparameters’. The adjustment, or
tuning, of these hyperparameters, remains a bit of an art and is more of an experimental
process that heavily depends on the specifics of your dataset, model, and training
process.
Ensemble Methods
Data scientists mostly create and train one or several dozen models to be able to
choose the optimal model among well-performing ones. Models usually show
different levels of accuracy as they make different errors on new data points. There
are ways to improve analytic results. Model ensemble techniques allow for achieving a
more precise forecast by using multiple top-performing models and combining their
results. The accuracy is usually calculated with mean and median outputs of all models
in the ensemble. Mean is a total of votes divided by their number. Median represents
a middle score for votes rearranged in order of size.
The common ensemble methods are stacking, bagging, and boosting.
Stacking - Also known as Stacked Generalization, this approach suggests
developing a meta-model or higher-level learner by combining multiple base
models. Stacking is usually used to combine models of different types, unlike
bagging and boosting. The goal of this technique is to reduce generalization
error.
114
Let's Learn Artificial Intelligence - Step-Up Module
Once you’re happy with your training and hyperparameters, guided by the evaluation
step, it’s time to finally use your model to do something useful!
115
3.9 PREDICTION
Machine learning is using data to answer questions. So Prediction, or inference, is the
step where we get to answer some questions. This is the point of all this work, where
the value of machine learning is realized.
However, to get the results, the model has to be deployed. To do so, you will have to
translate the final model from high-level programming languages (i.e. Python and R)
into low-level languages such as C/C++ and Java. The distinction between two types of
languages lies in the level of their abstraction in reference to hardware. A model that’s
written in low-level or a computer’s native language, therefore, better integrates with
the production environment.
After translating a model into an appropriate language, you can measure its
performance with testing. Testing can show how many customers engaged with
a model used for a personalized recommendation, for example, correlates with a
business goal.
Machine learning as a service (MaaS) is an automated or semi-automated cloud
platform with tools for data preprocessing, model training, testing, and deployment,
as well as forecasting. The top three MLaaS are Google Cloud AI, Amazon Machine
Learning, and Azure Machine Learning by Microsoft. ML services differ in a number
of provided ML-related tasks, which, in turn, depends on these services’ automation
level.
Deployment on MLaaS platforms is automated. For example, the results of predictions
can be bridged with internal or other cloud corporate infrastructures through APIs.
The predictions from the model can be real-time or in set intervals.
Batch prediction
This deployment option is appropriate when you don’t need your predictions
continuously. When you choose this type of deployment, you get one prediction for a
group of observations. A model is trained on a static dataset and outputs a prediction.
You can deploy a model on your server, on a cloud server if you need more computing
power or use MLaaS for it. Deployment is not necessary if a single forecast is needed
or you need to make sporadic forecasts. For example, you can solve a classification
problem to find out if a certain group of customers accept your offer or not.
Web service
Such machine learning workflow allows for getting forecasts almost in real-time. A
model, however, processes one record from a dataset at a time and makes predictions
on it. It’s possible to deploy a model using MLaaS platforms, in-house, or cloud servers.
116
Let's Learn Artificial Intelligence - Step-Up Module
117
3.10 MACHINE LEARNING AS A SERVICE
Machine learning as a service (MLaaS) covers the various cloud-based platforms that
provide most of the machine learning services such as data pre-processing, model
training, and model evaluation, with further prediction. Prediction results can be
bridged with your internal IT infrastructure through REST APIs.
The four leading cloud MLaaS services that allow for fast model training and
deployment are:
Amazon
Machine Azure
Google IBM
Machine
Learning Cloud AI Watson
Learning
services
These platforms are most useful for Machine Learning enthusiasts. We shall be working
with some of these in the activities and projects in subsequent units.
EXPLORE
https://aws.amazon.com/ https://cloud.google.com/
machine-learning/ products/ai/
https://azure.microsoft. https://www.ibm.com/watson
com/en-us/services/
machine-learning/
118
Let's Learn Artificial Intelligence - Step-Up Module
SUMMARY
Machine Learning is the technique where algorithm or deep learning is used to build
AI rather than the use of programming. It uses the method of ‘training’ an algorithm
so that machines can learn.
The machine learning process involves the following steps:
• Gathering data
• Preparing that data
• Choosing a model
• Training
• Evaluation
• Hyperparameter tuning
• Prediction.
Data Collection involves Data discovery, Data augmentation, Data generation and
Data Labeling
Big Data is the name given to massive collections of data generated by humans.
This availability of massive datasets is one of the key reasons that has had machine
learning possible. More data means more opportunity for a computer algorithm to
find associations and hence, higher accuracy of predictions.
There are tools that enable users to source large volumes of raw data quickly such
as, Data Scraping Tools, Synthetic Data Generators, Data Augmentation Tools, Data
Labelling Tools and Open-Source Datasets.
119
Once the data has been collected, it is in a raw form. That is why a large amount of
time is spent on data cleaning which involves:
• Identifying relevant data and removing irrelevant data
• Fix Irregular cardinality and structural errors
• Outliers
• Missing data treatment
• Data Transformation
Post the data pre-processing, comes the final data preparation phase of Data splitting.
The next step is selecting an appropriate model. A machine learning model is a set
of rules that recognizes certain types of patterns. Based on the type of tasks, we can
classify machine learning models in the following types:
• Classification Models
• Regression Models
• Clustering
• Dimensionality Reduction
• Deep Learning etc.
Artificial Neural Networks (ANN) are inspired by the central nervous system that we
possess and are applied to several problem domains:
• To classify data by recognizing patterns.
• To detect anomalies or novelties, when test data does not match the usual
patterns.
• To process signals, for example, by filtering, separating or compressing.
• To find a function that closely matches the training data (approximate a target
function) so that it can predict almost correctly.
Effectively, Neural networks can identify faces, recognize speech, read handwriting,
translate texts, play games (typically board games or card games), control autonomous
vehicles and robots and a few other things.
Typically, ANN’s have a layered structure. The input layer picks up the input signals.
Then the input layer passes them on to the next layer, the so-called ‘hidden’ layer.
There may be more than one hidden layer in a neural network. Last comes the output
layer that delivers the result.
120
Let's Learn Artificial Intelligence - Step-Up Module
121
UNIT 4
NATURAL LANGUAGE PROCESSING (NLP)
Topic Covered
Introduction to Natural Language Processing (NLP) |
Natural Language Understanding I Application of NLP | Use Cases |
How Does NLP work | Elements of NLP
4.1 INTRODUCTION TO NATURAL LANGUAGE
PROCESSING (NLP)
Natural Language Processing (NLP) is the part of machine learning that helps
computers interpret and process natural human language.
Ever wondered how devices like Siri, Google Assistant and Alexa understand and
interpret your voice? Have you also been irritated when they have not been able to
pick up specific terms or confuse them with others?
Well, this is the result of Natural Language Processing (NLP), the potential and
challenges of it.
NLP is a subfield of artificial intelligence. NLP uses AI and ML tools, techniques,
algorithms (computer science) and linguistics to help computers understand
unstructured ‘human language’ data and derive meaning from it. NLP bridges the
gap between the human language and the command line interface of a computer.
124
Let's Learn Artificial Intelligence - Step-Up Module
NLP involves computer science, artificial intelligence, linguistics and data mining. The
objective of NLP is to help machines read, decipher, understand, and make sense of the
human languages. It aims at substituting humans for doing various tasks. Common
real-world examples of such tasks include online chatbots, text summarizers, auto-
generated keyword tabs, and tools that analyze text to identify the sentiment of that
given text.
125
4.2 NATURAL LANGUAGE UNDERSTANDING
Automatic speech recognition is used to convert spoken words into computer text.
It is also used for biometric authentication (authenticating users via their voice)
and following human instructions to perform an action. Typically, automatic speech
recognition requires preconfigured or saved voices of the primary user(s). The human
needs to train the automatic speech recognition system by storing speech patterns
and their vocabulary into the system.
Nowadays, big data can sort, organize, and provide insights into data unthinkable to
humans in the past. Yet, in the pursuit of artificial intelligence, we realise that it takes
a lot more for computers to make sense of the data of language.
126
Let's Learn Artificial Intelligence - Step-Up Module
which aren’t inherently readable from the text or understanding the real purpose or
goal of a body of content.
This is where NLP and NLU are required and these applications can be quite complex.
With so many different languages in the world, communication is a complex
phenomenon varying between cultures and geographies. Add to it the diversity in
the languages, such as writing styles, syntax, and grammar rules, varying accents and
dialects; all make a significant difference in language comprehension.
127
4.3 APPLICATION OF NLP
Thus, NLP is still an ever-evolving and developing field that still involves a vast amount
of research, experimentation and innovation to cater to the various needs it serves.
Syntax – understanding the rules and arrangement of words and phrases followed to
create well-formed and meaningful sentences in a language.
Semantics – understanding the meaning of words, phrases, sentences and other text.
There isn’t a single way to ensure language acquisition, even for humans, a new
language is hard to learn. What makes it more complicated is that languages are
always being modified, shifting, adding and subtracting from an ever-widening
lexicon. It also incorporates new forms of usage such as e-mails, texts, social media
impacting language.
Tasks that NLP is used for include text summarization, sentiment analysis, topic
extraction, parts-of-speech tagging, stemming, text mining, machine translation,
named entity recognition, relationship extraction, and automated question answering,
language modeling and almost all other language-related tasks.
128
Let's Learn Artificial Intelligence - Step-Up Module
Given below are some real-world applications of NLP that you may be familiar with.
SPAM SENTIMENT
CHATBOTS
FILTERING ANALYSIS
Spam Filtering
SPAM filtering uses NLP extensively. A spam filter identifies
unwanted e-mails and sends them to a separate folder
instead of the inbox.
Simple spam filtering can be achieved using classification
models in machine learning. However, NLP provides better
capabilities to distinguish between useful e-mails and
real spam. NLP techniques such as n-gram modelling are
applied to e-mails to classify them as spam or ham (not
spam) with higher accuracy than traditional classification
models.
Chatbots
A chatbot with artificial intelligence (AI) is a conversational
piece of software used to answer questions without the
need of a human operator. It can simulate a conversation
with a user in natural language through voice and
text. It is usually customized for use on mobile devices,
web browsers, and chat platforms such as Facebook
Messenger, or Slack.
129
A chatbot is an advanced application of natural language processing, that takes us
closer to communicating with computers in a way like human-to-human conversations.
Without NLP, chatbots will not deliver any value to its users. The NLP models allow the
chatbot to understand your query or comment and reply to you. Businesses these days
are increasingly using chatbots across all domains, especially customer service, sales
and marketing, etc. It is being used to respond to customer queries, analyse feedback
and provide solutions to the customer through product or service recommendations.
Chatbots help businesses in saving time and money. Since most questions asked
by customers are frequently repeated, they can be handled by chatbots. This helps
customer service agents prioritize relevant customer queries, thereby ensuring overall
customer satisfaction.
Chatbots use a combination of Natural Language Processing, Natural Language
Understanding, and Natural Language Generation to achieve the desired
Conversational User Interface.
Sentiment Analysis
Sentiment analysis is used
when we have to identify the
sentiment of a text to interpret
and classify emotions to get
feedback or understand intent.
We may use this to gauge how
customers feel when they use a
service or to classify comments
made on a site.
With many people expressing
their feedback through large
scale surveys or social media
platforms, organizations rely
on software to process large
volumes of such information
to save time by automating
responses or collecting and analyzing data. NLP is used to understand human
emotions expressed via text to understand the customer’s feedback. These analyses
may also be used to adapt products and services to meet customer expectations.
130
Let's Learn Artificial Intelligence - Step-Up Module
Now let us look at some products that use NLP. There are numerous products that we
use daily without realizing the fact that NLP powers them; here is a short list.
Google
Alexa / Translate Auto-
Spell
Siri / Grammarly And complete Immersive
Check
Cortana Microsoft Reader
Translate
131
Grammarly
Grammarly is one of the popular writing/editing tools used around the world for all
types of writing. It provides options for different writing styles, audiences and other
capabilities to help edit your text. One can use auto-detect capabilities for spellings
and grammar, passive voice writings, styles of English, tone of the document, etc. to
get suggestions for changes according to the selected writing goals.
AI, ML and NLP are the primary enablers of Grammarly’s capabilities. Grammarly’s AI
system relies on a wide range of NLP algorithms that address each of the options that
one can see in its offering. NLP allows Grammarly to perform various tasks, including
writing improvement, sentiment analysis, readability scoring, suggestions to use
alternate words, phrases, and sentence structure, etc.
The machine learning system of Grammarly is continuously updated even as it is used
by the many users across the world and it will only get better with time as it has more
data to work with.
132
Let's Learn Artificial Intelligence - Step-Up Module
Spell Check
Spell Check is a much-used tool that corrects spelling or even some grammar errors.
The spell check feature is usually combined with an auto-suggestion feature used
in smartphones that can help save time and enables people to overcome language
and accurate typing limitations. It helps to eradicate the spelling errors from your
messages and improves communication. This feature uses NLP to identify errors and
auto-suggest the right word or spelling you are looking for, by analysing the text and
predicting the words/spellings you are looking for and is consistent with the writing.
133
Autocomplete
Autocomplete is another extended application of NLP that is used along with spell
check often. It is b used by almost every web and mobile application, including emails
and search engines like Internet Explorer and Google.
Autocomplete using previous searches performed by other customers, helps us
to find what we are looking for quickly. Thanks to the advancements in NLP word
combinations and text analysis, we can experience the benefits of these.
Immersive Reader
This is an application of NLP that is used to capture information from documents,
whiteboards, business cards, receipts, menus, signs, handwritten memos, or anything
else containing text and have it read by the software. This is used for teaching students
how to read, for helping visually challenged individuals and anyone else who wants
their documents auto-read.
Let us now see how NLP may be used in a few other applications:
Marketing
Natural language processing is being used in marketing efforts to boost efficiency.
From using chatbots to smart AI copywriters generating slogans, NLP models are
extending marketing capabilities and creating new forms of marketing as well.
134
Let's Learn Artificial Intelligence - Step-Up Module
Banking
NLP is helping AI software process documentation in Banking institutions faster. It is
also helping to mitigate risks, automate business processes, or check the quality of
customer services. For example, NLP enabled AI is being used by banks to extract key
data and clauses to help loan officers review commercial loan agreements.
Healthcare
NLP models are currently helping medical workers to process patient data more
efficiently. It is also helping to improve the quality of medical care and identify
patients who need special care. NLP is being used to map data elements present in
unstructured text to structured fields in an electronic health record (EHR) to improve
clinical data integrity. Health workers are also using speech recognition to dictate
clinical notes or other information that is then turned into text.
135
4.4 FUTURE OF NLP
The ultimate target of NLP AI is to develop a chatbot that would be able to carry out
meaningful, unsupervised interactions with humans and pass the Turing test. We are
likely to see more investments from businesses and academia in NLP and NLU for the
many products and services it enables.
There is much ground to be covered and people from different fields such as language,
linguistics, cognitive science, data science, computer science are combining efforts
and collaborating to produce successful NLP AI. We hope you will keep track of the
development of this exciting front too. Now let’s get into some activity to produce a
chatbot.
NLP derives meaning from human languages by analysis of the text using semantics
and syntax.
136
Let's Learn Artificial Intelligence - Step-Up Module
137
4.5 ELEMENTS IN NATURAL LANGUAGE
PROCESSING
Let us briefly explore the process and main elements of natural language processing:
If we run through the NLP basics, there are seven essential steps you need to undertake
to help your computer understand natural language:
Named
Dependency
Stop Words Entity
Parsing
Recognition
in NLP
(NER)
Coreference Sentence
Resolution Segmentation
138
Let's Learn Artificial Intelligence - Step-Up Module
Sentence Segmentation
The first element involves splitting sentences into separate objects. In this stage, a
smart AI algorithm evaluates the data sets and defines punctuation marks like periods
(full-stops) to identify where a sentence finishes. This stage is vital the NLP model here
has to meaningfully segments the sentence and then derive the meaning by analysis
of the whole paragraph.
Under ideal circumstances, this should be easy, but it is not. It is made complicated as
the real-world data comes without accurately used punctuation marks or has other
errors in the text. Therefore, data scientists need to apply sophisticated algorithms
and techniques to identify the correct segments.
Word Tokenization
After sentence segmentation, words are separated to determine various parts of
speech. While it is easy to do in some languages like English by identifying spaces
between the words or tokens, in other languages it may be complicated as words
may be strung together, etc. Punctuation marks in themselves are also considered
separate tokens as they carry particular meaning that can change the meaning of the
overall text.
To determine its part of speech, AI algorithms analyze each word and based on selected
criteria categorize these into adverbs, adjectives, nouns, verbs, etc. The machine uses
this to determine the role of each token in the sentence or text.
A pre-trained parts-of-speech classification model enables this function. Millions of
texts in the given language that are previously tagged and marked are used to train
this model. Algorithms use this essential data in large data sets to analyze and develop
statistical models that determine which part of speech do the words belongs to.
Text Lemmatization
Lemma refers to the common part or the basic form of related words used in different
grammatical forms. For example, the words ‘greater’ and ‘greatly’ will have ‘great’ as
the lemma. Many texts or sentences may contain the root word as well as its different
grammatical forms. NLP figures out the lemma from each token (word) and uses it to
identify the words, categorise them and find their meaning.
Stop Words
Stop words are words that do not add to the meaning of the text. These are filler words,
or casually inserted words that add noise to the text and only complicate the text. NLP
includes processes to identify and skip these when trying to understanding the text.
139
Dependency Parsing in NLP
Dependency parsing is discovering the relations between all the words in a text. A
‘Parse Tree’ is created by AI algorithms that identifies the root word in the sentence
and bridge the gap between other tokens. A parent word for each token may also be
defined to gain more insight and understand the core concept.
Coreference Resolution
Coreference resolution refers to the task of finding all expressions in the text that
refer to the same entity. Every language has many entities, such as pronouns and
other parts of speech, that may relate to another word in the sentence and expand its
meaning. Coreference resolution is used to cluster all mentions in the text, which refer
to a real-life concept or entity.
For example, He said, “Mary and I are going to see my other apartment so that she can
choose, which living quarters may be more suited to her needs”.
Here Mary, Her and She, refer to one cluster and I and My belong to the second cluster.
NLP Algorithms using coreferences commonly look for the nearest preceding mention
that is compatible with the referring expression. Neural networks can also be trained to
account for word embeddings and distance between mentions as features, to identify
these clusters to find the meaning of the whole text.
EXPLORE
https://huggingface.co/coref/
140
Let's Learn Artificial Intelligence - Step-Up Module
ACTIVITY
Go through the following links and try replicating the instructions provided using
Scratch.
EXPLORE
https://www.youtube.com/watch?v=KSr5bLXaoig
EXPLORE
2. Chatbot in Scratch
https://www.youtube.com/watch?time_continue=53&v=gZAdYyjxRcg&feature=emb_
logo
141
SUMMARY
Natural Language Processing (NLP) is the part of machine learning that helps
computers interpret and process natural human language. It uses AI and ML tools,
techniques, algorithms (computer science) and linguistics to help computers
understand unstructured 'human language' data and derive meaning from it.
Tasks that NLP is used for include text summarization, sentiment analysis, topic
extraction, parts-of-speech tagging, stemming, text mining, machine translation,
named entity recognition, relationship extraction, and automated question answering,
language modeling and almost all other language-related tasks.
Some familiar real-world applications of NLP are Spam Filtering, Chatbots, Sentiment
Analysis.
Some products that we use daily that ae powered by NLP are Alexa / Siri / Cortana,
Grammarly, Google Translate or Microsoft Translate, Spell Check, Autocomplete,
Immersive Reader, etc.
142
Let's Learn Artificial Intelligence - Step-Up Module
143
144
Let's Learn Artificial Intelligence - Step-Up Module
UNIT 5
COMPUTER VISION
Topic Covered
Introduction to computer vision | Computer Vision Applications |
Challenges of Computer Vision | Optical Character Recognition |
OpenCV | Project: Face Recognition using Scratch |
Project: Attendance System using Face Recognition
145
5.1 INTRODUCTION TO COMPUTER VISION
In the base module, we introduced you to the exciting field of Computer Vision (CV).
CV is the field of computer science that aims to replicate parts of the human vision
system, to enable computers to identify and process objects in images and videos in
the same way that humans do. We must not confuse Computer Vision with Image
processing though.
EXPLORE
Thanks to advances and innovation in artificial intelligence, deep learning and neural
networks, computer vision has taken great leaps in recent times. CV has been able to
better humans in some tasks related to detecting and labeling objects in images and
videos.
But despite our aim to mimic the human brain and its functions, we do not know how
exactly do our brains work? Therefore, it is difficult to know how we can approximate
that with our algorithms. The reality is that brain computation is still not completely
understood, so although AI and Neural Nets including those in Computer Vision are
supposed to “mimic the way the brain works,” nobody can say if that is true. We are
not sure how the brain and eyes process images, therefore it’s difficult to say how well
the algorithms used in CV and AI processes approximate our own internal mental
processes.
But despite that over the past decade, systems have reached an accuracy of 99 per-
cent from 50 percent. This makes them even more accurate than humans at reacting
quickly to visual inputs.
146
Let's Learn Artificial Intelligence - Step-Up Module
Then these images are subjected to various software techniques, or algorithms, that
allows the computer to identify patterns in all the elements of these images that relate
to those labels. It is an example of supervised learning.
Deep learning is a somewhat fundamentally different approach. Deep learning
relies on neural networks that can extract common patterns between examples of
labeled data provided to them and transform it into a mathematical equation that
will help in identifying and classifying future images or pieces of data. It is an example
of unsupervised learning. The difference between the two approaches is the kind of
algorithms used.
For instance, to create a facial recognition application with deep learning, one is
required to develop or choose a pre-defined algorithm and train it with images of
people’s faces, we want it to detect. Once it is trained with sufficient examples, the
neural network used will be able to detect these faces without any direct programming
or instructions that provide information on features or measurements of these faces.
Deep learning is very effective when the deep learning algorithm works on a large
amount of labeled training data. Further tuning the parameters such as the type and
number of layers of neural networks and training epochs (a measure of the number of
times all of the training vectors are used once to update the weights). Deep learning
is considered easier and faster to develop and deploy, compared to other types of
machine learning.
Due to its ease and effectiveness, current computer vision applications mostly use
deep learning; these include applications such as self-driving cars, tuberculosis or
cancer detection and facial recognition. Deep learning and deep neural networks have
been enabled in large measure due to the availability and more recent developments
in hardware and cloud computing.
Another driving factor behind computer vision’s growth is the sheer amount of data
we generate and capture, that is then used to train and make computer vision better.
Along with the massive amounts of visual graphic data (for example the billions of
images shared online every day), the vast amount of computing power required to
analyze the data is now available at low costs. All these developments, along with
extensive and widespread use, new algorithms and developments in hardware have
resulted in massive improvements in accuracy rates for object identification.
The advantage offered by ML and Deep learning is that developers no longer needed
to hard-code every single rule into their vision applications manually. Now they
program ‘features’ and smaller applications that could detect specific patterns in
images. These combined with statistical learning algorithm such as linear regression,
logistic regression, decision trees or support vector machines (SVM) help them to
detect patterns, classify images and identify objects in them.
147
Machine learning has provided an alternative to traditional software development that
required dozens of engineers, programmers and domain experts to work together
and code every scenario and detail into the software. Also, such programs were often
unable to handle situations where new situations arose. Machine learning has helped
to overcome many such problems. For instance, in the past to predict a disease such
as a lung or breast cancer, machine learning these days can do it without the need for
dozens of engineers and breast cancer experts. It can be done with relative ease by
training the computers using Computer Vision and loads of data.
EXPLORE
https://www.youtube.com/watch?v=ALQ_RNSRE40
Not only is the problem solved by Machine Learning, but it can also happen in light-
ning-fast speed today. This is because of ultra-fast chips and related hardware, along
with a fast, reliable internet and cloud networks. One vital factor in the development
of these solutions is that big companies doing AI research like Amazon, Google, Mic-
rosoft, Facebook, Intel and IBM are sharing their work, that allows others to use that
to build on their previous work rather than start from the beginning. As a result, the
AI industry is making rapid progress, and processes that took weeks or months some
time ago can be done in minutes or even in real-time today. For these real-time appli-
cations of computer vision, this process is carried out continuously and in microsec-
onds. This is why computer today can be termed to be what scientists call ‘situation-
ally aware’. Here is an example of how Computer Vision is being used in real-time in
cancer prediction.
EXPLORE
https://www.youtube.com/watch?v=9Mz84cwVmS0
148
Let's Learn Artificial Intelligence - Step-Up Module
Self-Driving Cars
Self-driving cars make sense of their surroundings by using computer vision. Many
different cameras around the car capture video from different angles and feed it to a
computer vision software. These images are processed in real-time to interpret traffic
signs, detect other cars, find the extremities of roads, locate obstacles, detect pedes-
trians, etc. This information enables the self-driving car to steer its way on the road,
following traffic rules, avoiding obstacles, and drive its passengers safely to their des-
tination.
149
CV In Facial Recognition
Facial recognition applications are another Computer Vision enabled technology
that helps computers to identify people using images of people’s faces. This is done
through Computer Vision algorithms that detect facial features in images and compare
them with stored databases of face profiles. Smartphone and other consumer
devices authenticate the identities of their owners or authorized users relying on
facial recognition technologies. Social media apps detect and tag users using facial
recognition, similar to how law enforcement agencies rely on facial recognition to
identify criminals in video feeds.
EXPLORE
150
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
https://www.clickz.com/how-ar-works/241718/
CV In Healthcare
Computer Vision has also been an essential part of advances in health-tech as we
have seen in the earlier cancer detection model. Computer vision algorithms can help
automate many tasks, just like detecting various types of cancer and other diseases
such as tuberculosis; another example is detecting symptoms in X-rays and MRI scans.
Here is an exciting video for seeing how CV is being used through smartphone devices
in healthcare.
EXPLORE
https://www.youtube.com/watch?time_continue=16&v=Dt-rC5nR-FE&feature=emb_
logo
151
5.3 CHALLENGES OF COMPUTER VISION
152
Let's Learn Artificial Intelligence - Step-Up Module
Image Classification – which broad category does this image belong to?
Involves assigning a label to the overall image.
Object Detection – This involves locating different objects in the image and
classifying them, using different bounding boxes and labels, for instance.
Object Landmark Detection – This identifies critical points for the object in the
image rather than use a bounding box.
153
Object Verification – This involves finding out if a specific object is in the image.
Object Segmentation – This involves a more closer defining of the object
marking every pixel that belongs to the object.
Image Segmentation - This involves the computer segmenting different
sections of the image.
Image Style Transfer – This involves the task of learning the style from one or
more images and applying that style to a new image.
Image Colorization – This involves converting a grayscale image to a full-color
image.
Image Reconstruction – This involves filling in missing or corrupt parts of an
image.
Image Super-Resolution – This involves generating a new version of an image
with a higher resolution and detail than the original image.
Image Synthesis – This involves generating targeted modifications of existing
images or entirely new images.
Video Motion Analysis – This involves using computer vision to estimate the
velocity of objects in a video, or the camera itself.
EXPLORE
https://machinelearningmastery.com/applications-of-deep-learning-for-computer-
vision/
EXPLORE
https://www.youtube.com/ https://www.youtube.com/
watch?v=BMFAbYAxpII watch?v=08myARQYXXY
https://www.youtube.com/
watch?v=st_U59Yv3a8
154
Let's Learn Artificial Intelligence - Step-Up Module
155
5.4.2 HOW DO MACHINES SEE?
Machines analyze images based on various features, including colors, edges, etc. The
techniques and approaches to analyze images are integrated within algorithms that
help to detect these features and classify or identify them based on trained data. Some
of the processes involved are:
Images are broken down into colors by using hexadecimal numbers to identify each
color. A similar group of colors identified and then used to segment the image, for
example, distinguish the foreground from background. Color gradient technique
is used to find edges of different objects. The computer then uses algorithms to
then search for specific features, also called as corners. These are lines that meet or
intersect at an angle and cover a specific part of the image with a single color shade.
These are considered the building blocks that enable the search for more detailed
information within the image. Another vital aspect of identifying images or parts
within it correctly requires determining textures in the image. The variance in textures
between different objects helps to categorize an object correctly. Using these steps,
the machine then makes a guess and matches the image with those stored in the
database. The machine then based on algorithms and provided instructions verifies if
the image and the objects within were correctly classified, then repeats the process as
per requirement till a sufficiently good and reliable accuracy level is achieved.
Despite the recent progress, which has been impressive, we’re still not even close
to solving computer vision. However, there are already multiple institutions and
enterprises that have found ways to apply CV systems, powered by CNNs, to real-world
problems.
156
Let's Learn Artificial Intelligence - Step-Up Module
5.5 OPENCV
OpenCV (Open Source Computer Vision Library) is an open-source Computer Vision
and Machine Learning software library. It mainly focuses on image processing, video
capture, and analysis, including features like face detection and object detection.
OpenCV was built to provide a common infrastructure for computer vision applications
and to accelerate the use of machine perception in commercial products. Being a
BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the
code.
Let’s play
What do you need before we start?
Python
An IDE
OpenCV basics
Import library
import cv2
157
Read an image
We can use cv2.imread function to do this. First, make a variable and use the function
to read the image using its path as the first parameter.
#For a colored image
img = cv2.imread(“Man.jpg”, 1)#For a grayscale image
img = cv2.imread(“Man.jpg”, 0)
In here, the ‘1’ and ‘0’ represent the type of the image. If we put ‘1’, then it will be read
in RGB (Colored), or if we put ‘0’, then it will be read as grayscale or black and white
image. OpenCV will read the read as a NumPy array or in English a matrix of numbers.
So if the image is colored, it will be a 3d matrix, or if it is a grayscale one, it will be a 2d
matrix.
If you print the img variable you will be able to see the image is now converted to a
matrix! And if you print the shape of the image, you can see the Numpy array with the
number of rows and columns.
print(img.shape)
Resize an image
If you want to resize the image you read you can simply use cv2.resize function.
resized = cv2.resize(img, (650,650))
The first parameter is the object we read and the second one is the size we want it to
be resized. You can also use the original image properties for the parameters as well.
Here is how you can do it.
#New image shape = Old image shape/2
resized = cv2.resize(img,(int(img.shape[1]/2),int(img.shape[0]/2)))
So here you go with the super basic functionalities of computer vision using OpenCV.
Let’s meet again to continue with the session later! Godspeed.
158
Let's Learn Artificial Intelligence - Step-Up Module
Can you identify the age and gender of the people in this image?
159
Yes, you can! You can identify that there are 3 males and 1 female in the photo. You can
also perhaps guess their age.
Age - 3 | Male
Age - 6 | Male
Age - 39 | Male
Age - 33 | Female
But can a machine recognize if there are any faces in the image and guess their
gender, age, and emotion?
Well, in the past few years, face recognition has become one of the most promising
applications of Computer Vision. Face detection can be considered as a substantial
part of face recognition operations.
160
Let's Learn Artificial Intelligence - Step-Up Module
The method of face detection in pictures is complicated because, well, human faces
are widely different from one another! They can have different poses, expressions,
position, and orientation, skin color, have glasses or facial hair or not, etc. Then there
are also differences in camera settings, lighting conditions, and image resolution.
Face detection is the action of locating human faces in an image and optionally
returning different kinds of face-related data.
There are a few methods that you can use to detect a face. But here we will use feature-
based detection. Feature-based detection uses what is known as face landmarks to
detect faces.
Face landmarks are a set of easy-to-find points on a face, such as the pupils or the tip
of the nose.
The feature-based method detects faces by extracting the face landmarks of the face.
161
Pros:
1. Features are invariant to pose and orientation change.
2. This method has a higher chance of face detection.
Cons:
1. Difficult to locate facial features due to noise in the images.
2. Difficult to detect features in complex backgrounds.
Once you have detected the face, you can classify the face for the age, gender, and
emotion using machine learning algorithms and techniques.
Now it is time for you to do a face recognition project in Scratch. But do you think you
can do one on your own? Give it a try!
In this project, we will make a script that detects the face using the camera and reports
the expression of all the faces detected on the stage. Let’s begin!
COMPATIBLE PLATFORMS
FLOW CHART
The following flow chart shows the overview of the program required to implement
Face Expression Recognition in the compatible platforms. You can find various
graphical blocks in the compatible platforms to make the face expression recognizer.
Now go ahead and try the project.
162
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
Use the following video to get some guidance for the project.
https://www.youtube.com/watch?v=6w9kvc_pN4o
163
HOW DOES IT WORK?
164
Let's Learn Artificial Intelligence - Step-Up Module
FACE RECOGNITION
Face recognition systems allow programs to identify a person by analyzing the face
landmarks of her/his face in a digital image.
1. When a face is analyzed, its geometrical properties, size, and position are
obtained.
2. A structure is generated that forms a pattern that is later stored in a database.
3. Once stored, the user’s facial record is compared with the biometric features
of each face that is scanned and allows adding new templates to make the
recognition more effective.
4. If the image matches a search, the system will alert us that a match has been
found.
165
PROJECT: ATTENDANCE SYSTEM USING FACE
RECOGNITION
LEARNING OUTCOMES
1. Understand face recognition cycle
2. Training the AI model using image data sets
3. Data logging
Requirements: Laptop with a camera. There is no need for any additional hardware
for this project.
COMPATIBLE PLATFORMS
1. Stretch 3
2. PictoBlox
Because of the COVID-19 pandemic which started in early 2020, a lot of things are
becoming contactless – from lift buttons to doors to sanitizer dispensers. So, why
should the fingerprint-based attendance systems, widely used in offices and schools,
be left behind? Thus, to make sure that we do not touch, we’ll develop a contact-free,
face recognition-based attendance system. Our system will first take a sample image
of the person whose attendance needs to be tracked. The next time when the system
scans the person’s face, it will match the image with the sample image stored and will
note down the time stamp for that person.
FLOW CHART
The following flow chart shows the overview of the program required to implement
the Attendance system using Face Recognition in the compatible platforms. You can
find various graphical blocks in the compatible platforms to make the face expression
recognizer. Go ahead!
166
Let's Learn Artificial Intelligence - Step-Up Module
Face recognition
167
SUMMARY
Computer Vison (CV) is the field of computer science aims to replicate parts of the
human vision system, to enable computers to identify and process objects in images
and videos. Core to CV is pattern recognition.
To enable pattern recognition, the first step is to train a computer to understand visual
data by feeding it lots of images. These images should be labelled. Then these images
are subjected to various software techniques, or algorithms, that allows the computer
to identify patterns in all the elements of these images that relate to those labels. It is
an example of supervised learning.
Deep learning can also be employed for this, which relies on neural networks that
extract common patterns between examples of labelled data and transform it into a
mathematical equation that will help in identifying and classifying future images. It is
an example of unsupervised learning.
Some applications of computer vision in the real world are self-driving cars, facial
recognition by law enforcement agencies, Augmented Reality & Mixed Reality projects,
disease detection, etc.
Many computer vision applications involve Detect, Recognize, Classify and Identify
(DCRI).
OCR goes through a series of steps. First, a scanner converts the paper document
image to binary data. The scanned image’s light and dark areas are analyzed by the
machine, separating the writing from the background. The light areas are segmented
and classified as background while the dark ones are classified as handwriting. This step
is called Image Acquisition. The image color or grayscale is converted to binary. During
segmentation, feature extraction, and classification, OCR finds the letters, numbers
and symbols inside the dark areas, using a variety of techniques. Pattern recognition
helps to identify text across various fonts and formats to compare characters from the
scan. Then the characters are converted to ASCII (American Standard for Information
Exchange), or similar codes.
168
Let's Learn Artificial Intelligence - Step-Up Module
OCR is widely used in post offices to sort letters, in banks to deposit checks electronically,
to convert documents into text for the visually impaired, in translation apps, in online
text databases like Google Books, with security cameras that recognize license plates
and in the examination system to mark large volumes of answer sheets.
Face detection is the action of locating human faces in an image and optionally
returning different kinds of face-related data.
Face recognition systems allow programs to identify a person by analyzing the face
landmarks of her/his face in a digital image.
169
170
Let's Learn Artificial Intelligence - Step-Up Module
UNIT 6
DATABASES AND SQL
Topic Covered
Databases | SQL
171
6.1 DATABASES
Databases offer a convenient and powerful way to store and organise information.
6.1.1 SPREADSHEET
A Spreadsheet is a computer program that looks like a large sheet of paper with boxes.
It has a grid of cells that form rows and columns.
Users can write words or numbers in the cells. They can make it in the form of head-
ings with their values given against them. These files then can be saved. Usually, the
rows in a table are called records and the columns in a table are called fields or attri-
butes.
172
Let's Learn Artificial Intelligence - Step-Up Module
The main reason most people use spreadsheets is for performing various types of
automatic calculations such as:
Count items, identify maximum or minimum.
Perform various arithmetic calculations such as addition, subtraction,
multiplication, division, etc.
Calculate averages, percentages, square roots, sine, cosine, tangents, logs, etc.
Apply standard formulas that can be used like that of interest rate, price value,
yield, etc.
One can even write a formula and apply on the data. This means the computer does
all the math, and people only need to give the correct information.
Using spreadsheets, one can make calculations on a large set of numbers in seconds,
that would take a very long time if done manually.
Another strength of spreadsheets is that they can produce diagrams, like graphs and
pie-charts, based on the data the user enters. Sometimes the numbers make more
sense when the computer turns them into a picture.
Open Google
Microsoft Apple
Office.org Spread-
Excel Numbers
Calc sheet
173
ACTIVITY 1 (Part 1)
EXPLORE
https://data.gov.in/
https://bhuvan-app3.nrsc.gov.
in/data/download/index.php
http://www.surveyofindia.gov.in/
https://dbie.rbi.org.in/DBIE/
dbie.rbi?site=home
174
Let's Learn Artificial Intelligence - Step-Up Module
Data (numbers or characters) sorted into descending order is when they are
arranged from the largest to the smallest number, e.g. 37, 23, 19, 12 and 7 or Z, X,
M, H, E, C, and A.
The whole table has now adjusted for the sorted column. Note: when the data in one
column is related to the data in the remaining columns of the table, you want to select
Expand the selection. This will ensure the data in that row carries over with sorted
column data.
ACTIVITY 1 (Part 2)
If the data provided is not sorted. Apply a sort to at least two columns. If it is
already sorted then change the sort, i.e. make it ascending instead of descending
or vice-versa.
You will notice that all the data in the table rearranges when you sort on one
heading.
If you select the whole column and the sort it, then only the data in that column
gets sorted, but now it will have changed it’s row headings and row-wise alignment
with rest of the data.
Make a note of information that you can extract from the data using the sorting
function.
175
6.1.3 FILTERING DATA
The filter feature applies a dropdown menu to each column heading, allowing you
to select specific choices to narrow a table. Using the above example, let’s say you
wanted to filter your table by gender.
ACTIVITY 1 (Part 3)
Now select all the column headings of your database and activate the filtering
option for them.
Now select a certain type of data from any of the columns by using the filter.
You can apply filters for greater than a value, less than a value, a value containing
something or value not containing something as well. Find out how.
Now write down three new findings that you were able to make from your
database, using filters.
Excel formula can be calculated in any empty cell, but preferably at the end of the
data in the relevant column or row. Excel formulas all start with an = sign written in
the empty cell, followed by the function name (In this case ‘sum’) and a section in
parentheses to provide ranges or arguments. Start out by typing =sum(
176
Let's Learn Artificial Intelligence - Step-Up Module
After the start parentheses enter the range of cells that contain the data you want to
add together. You can this in the following ways:
• Type your range in by writing the starting cell(Column and row name) followed
by a colon, followed by the ending cell (column and row name). E.g. D2:D20
• Select the respective cells using your mouse or input device. In the following
case, we will select B3:F3.
Next you can close the parenthesis, although newer versions of Excel will auto close
it for you.
By applying the same method, you can calculate the average, minimum value,
maximum value, and count:
Sum =SUM(B3:F3)
Average =AVERAGE(B3:F3)
Maximum Value =MAX(B3:F3)
Minimum Value =MIN(B3:F3)
Count of entries =COUNT(B3:F3)
ACTIVITY 1 (Part 4)
In your database, apply the Count, Sum, Average, Minimum and Maximum
formulae for at least three different ranges.
Extract three new insights that you got by using these formulae on your database.
177
6.1.5 RELATIONAL DATABASES
There are other more complex kinds of databases, but here we will focus on relational
databases.
Here’s an easy way to understand the benefits of dividing the data into multiple tables:
Imagine that you are responsible for keeping track of all the books being checked
out of a library. You could use a single table (a flat database) to track all the critical
information:
First Name Last Name Address Phone Book Title Due Date
Pari Kumari ABC, D1, 4111111 The Guide 30-08-20
EF STREET
This table meets the basic need to keep track of who has checked out which book, but
does have some serious flaws in terms of efficiency, space required and maintenance
time. For example, as voracious reader Pari checks out more books over time, you will
have to re-enter all of her contact information for every book.
First Name Last Name Address Phone Book Title Due Date
Pari Kumari ABC, D1, 4132549 The Guide 30-08-20
EF STREET
178
Let's Learn Artificial Intelligence - Step-Up Module
To re-enter Pari’s contact information wastes time and increases the opportunity for
error. Moreover, when an update is necessary (e.g. Pari’s phone number changes), each
of Pari’s records must be located and corrected. If one of Pari’s records has a different
address from the rest, is it a correction, a record overlooked during the last update, or
a data-entry mistake?
These problems can be decreased by normalizing our data – in other words, dividing
the information into multiple tables with the goal of having ‘a place for everything, and
everything in its place.’ Each piece of information should appear just once, simplifying
data maintenance and decreasing the storage space required.
STUDENTS TABLE
179
CHECKOUT TABLE
Student Id Book Title Due Date
0001 The Guide 30-08-20
Now that the data are arranged efficiently, we need a way to show which records in
the STUDENTS table correspond to which records in the CHECKOUT table – in other
words, who checked out which book. Instead of repeating everything we know about
a patron whenever he checks out a book, we will instead give each library patron an
ID, and repeat only the ID whenever we want to associate that person with a record in
a different table.
Now the PATRONS and CHECKOUT tables can be related (how relationships are
formally declared in various database software is beyond the scope of this paper).
Let’s learn some new terms to talk about our related tables.
The primary key:
It is a field whose values are unique in this table, and so can be used as identifiers for
the records.
In table STUDENTS, the Student ID field is the primary key and so its values must
remain unique. For example, the value “0002” can appear only in one record - Biju’s
- and Biju can have only one Student ID - “0002.”
Is the Student ID field in table CHECKOUT the primary key?
We can see that it contains duplicate values, so the answer is No. If Student ID were
the primary key for CHECKOUT, each person would only be permitted to check out
one book, and afterwards would be forbidden to check out any more books, ever.
180
Let's Learn Artificial Intelligence - Step-Up Module
So if Student ID is not the primary key for table CHECKOUT, which field is?
We can’t make Book Title the primary key, or we’d have a similar problem – each
book could only be checked out once, and afterwards, no one would be permitted
to check it out ever again. We can’t make ‘Due Date’ the primary key, or else only
one book could be due each day. Since none of the existing fields works as a primary
key, we will add a new field to hold an identifier for each record. We could name
this field Checkout ID.
CHECKOUT TABLE
Checkout Id Student Id Book Title Due Date
0001 0001 The Guide 30-08-20
For Example:
Is it possible to have a record of a student who has never checked out any book? Yes.
If the student is not yet started taking books.
181
Is it possible to check out a book to a student, who doesn’t have his or her records of
address and phone number captured? No, the library will not issue to a student who
is not registered.
This implies that the STUDENT Table is the Parent Table and the CHECKOUT TABLE is
the Child table.
If somehow the child table contains a record that does not have a corresponding
record in the parent table, that record is called an orphan. Orphaned records are a
problem that generally requires attention from the database administrator.
You have just learnt some basic concepts of databases, using the Spreadsheets.
However, Spreadsheets can hold only a limited amount of data. AI requires the use of
very large amounts of data that could carry thousands or millions of records.
182
Let's Learn Artificial Intelligence - Step-Up Module
It is a tedious task to address all the challenges mentioned above. The process can be
very time-consuming and prevent database administrators from focusing their atten-
tion to more strategic tasks.
Hence, specialized database development and management tools such as like SQL
Server Management Studio (SQL Server), Oracle SQL Developer (Oracle), MySQL Work-
bench (MySQL), MS Access, Oracle, Sybase, Informix, Postgres and SQL Server are used
to perform various types of operations in a given database. These operations can be
adding, deleting, updating, inserting and displaying information as stored in a data-
base.
183
6.2 SQL
SQL was developed at IBM by the joint effort of Donald D. Chamberlin and Raymond
F. Boyce during the early 1970s. Initially, it was called a SEQUEL(Structured English
QUEry Language). SQL language was invented for creating, deleting, fetching and
modifying rows for the data present in a tabulated format. SQL is an ANSI (American
National Standards Institute) standard language although there are many more ver-
sions of this language such as:
Some common relational database management systems that use SQL are Oracle,
Sybase, Microsoft SQL Server, Access, Ingres, etc. Although most database systems
use SQL, most of them also have their own additional proprietary extensions that are
usually only used on their system.
SQL is used to communicate with a database. According to ANSI (American National
Standards Institute), it is the standard language for relational database management
systems.
SQL statements or queries are used to perform tasks such as:
• Create, modify and delete database objects such as tables of views
• Update data on a database
• Retrieve data from a database
• Grant permissions and connect tables, etc.
184
Let's Learn Artificial Intelligence - Step-Up Module
MySQL MS SQL
The SQL syntax for both are more or less the same.
EXPLORE
185
You will be asked to register and sign-in. You also have the option to skip this by click-
ing on “No Thanks, just start my download”
186
Let's Learn Artificial Intelligence - Step-Up Module
This will download the installer. After the download process is complete, you can ex-
ecute the the MySQL installer file from its location. You can select the full developer
package from the options it provides.
At this point, the system tries to resolve possible inconsistencies. It might inform you
that additional packages need to be installed for the process to continue (e.g., Micro-
soft Visual C++ 2019 Redistributable Package). You can also run into Path installation
inconsistencies if you have previous MySQL installations on your Windows Server.
Luckily the MySQL Installer auto-resolves issues and installs the latest binary compat-
ible version of missing software. You are now ready to start the installation process in
earnest. Click Execute to begin the installation process.
Once the status of the installation status is labeled as Complete, you are ready to con-
figure the MySQL database.
187
You have successfully configured the MySQL server and need to confirm for the MySQL
Installer to apply the configuration.
188
Let's Learn Artificial Intelligence - Step-Up Module
• Microsoft has provided two specialized free editions to work on MS SQL server.
• Select the Developer Edition.
• Click on the “Download Now” tab to start the downloading process.
Click Here
189
• Click on the “Basic” option. This option has the default configuration for learning
MS SQL.
• Next, you will see “Microsoft Server License Terms” on the screen. Read it and click
“Accept”.
• Server install location - By default, the file will be saved in the main drive, but you
can change the installation location by clicking on ‘Browse’
• Once you have selected the installation location, click on the ‘Install’ button.
• Select the Connect now option and a separate command line window will open to
test the connection of the installed software. The system will run by default ‘select
@@Version’ statement to confirm that we can connect to new MSSQLSERVER
instance successfully.
• Select Install SSMS. This option takes us to Microsoft SSMS download link. To run the
SQL commands, the SQL Server should be connected with SQL Server Management
Studio (SSMS).
EXPLORE
• Go to the website
https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-
ssms?view=sql-server-ver15
190
Let's Learn Artificial Intelligence - Step-Up Module
• Save the setup.exe file in the desired location. Run the “.exe” file and click on “Install”
button as shown in.
• Once you have clicked on installation, the packages will start loading.
ACTIVITY 2 (Part 1)
Follow the steps of any one of the installation methods mentioned above and
install the SQL Server of your choice into your computer.
Let us now read about the various types of operations that take place for manipulating
data in the database using SQL commands.
191
6.2.4 BASIC SQL COMMANDS
Here are some basic commands used in SQL and their functions:
192
Let's Learn Artificial Intelligence - Step-Up Module
6.2.5 DATABASE
Creating A Database
For creating a new database, you can use the CREATE DATABASE command. The
syntax is given below:
Syntax:
Example:
After you have entered this statement, a database of the name “MLProject” will be
created.
Deleting A Database
For deleting an existing database in SQL (such as School in the given example), one
can use the DROP command as given below:
Syntax:
Example:
This SQL command will ensure that the database with the name MLProject will be
deleted.
193
6.2.6 TABLE
As the name suggests, a database consists of data which is organised in the form
of tables. Table, as we all know, consists of rows and columns. The data present in
the tables can be called as data element or values. These values are organised in the
form of horizontal rows and vertical columns. The point where the rows and columns
intersect is called a cell. The table can consist of any number of rows but should have
a specified number of columns.
Row
Cell
Name Marks Pass_Fail Subject
As can be seen in the given example, this table consists of three rows and four columns.
The columns are the vertical components under the subject name as “Name”, “Marks”,
“Pass_Fail” and “Subject”. The horizontal component is the rows that contain the
names such as “Appu, Kiara and Pari”. Each entry in the table is present in a space
called a cell. This table consists of twelve cells. All the entries present in the table (such
as Name, Marks, Pass_Fail and Subject) are the values or simply data elements.
194
Let's Learn Artificial Intelligence - Step-Up Module
Example:
Suppose we wish to create a table under the name “Month”. As an example, we have
taken three columns as the datatypes for the table representing months. These can be:
num – to represent the number of the month being referred. This will be of the type
integer.
name – to represent the name of the month. This will be a character.
days – to represent the number of days in a given month. This will also be an integer
type.
Hence, the command would become:
In the given statement, the datatypes are int and varchar, which represent integer
and character data, respectively. The data entries are represented by num, name and
days, which will be the headings for the database.
By using the keyword CREATE TABLE, we are telling the database that we are going
to create a new table. Then we need to enter the table name, which has to be unique.
Note that SQL is case insensitive, but the data stored inside the table will be case
sensitive. The columns for the table will be entered inside the open and close brackets.
Each column has to be specified with a certain data type.
ACTIVITY 2 (Part 2)
Now that we have chosen the headings for the database, there is a need to enter the
values or data elements. For entering the information in the table, we use the INSERT
INTO keyword. The syntax to use this statement is as follows:
Syntax:
195
Example:
Let us use this syntax in the table that we formed under the heading “months”.
If you want to add more entries in the table, simply enter the details of the data
element you wish to put in the database. The changes will automatically get reflected
in the existing table.
You can write an INSERT statement to add more months in the given table.
The table data will look as follows:
10 October 31
11 November 30
12 December 31
196
Let's Learn Artificial Intelligence - Step-Up Module
ACTIVITY 2 (Part 3)
Follow the steps mentioned above, enter the rows as given in the table with the
data mentioned in the following table:
Using SQL, it is possible to delete the data present in a particular field rather than
deleting the entire table. You must be wondering what keyword is used to perform
such an operation. This is called truncating a query in SQL. This operation can be
performed by using the TRUNCATE keyword. The syntax depicting the use of the
keyword is given below:
Syntax:
Example:
If we wish to delete an entry under the table name “months”, we should use the
following statement:
Once this statement is executed, the data inside the table “months” will be deleted,
but the table will still remain.
197
6.2.9 DELETING ROWS IN SQL
When we want to delete a particular row in a given table, we use DELETE keyword. Let
us understand the usage of this keyword for deleting a particular record or entry.
Syntax:
Example:
If we wish to delete an entry under the table name “months”, we should use the
following statement:
After the execution, the given code, the entries where the days of the are 31 will be
deleted. Also, the table “months” will be modified as follows:
2 February 28
4 April 30
6 June 30
9 September 30
11 November 30
ACTIVITY 2 (Part 4)
From the table Class_Test_August, delete the entries that are less than 12.
198
Let's Learn Artificial Intelligence - Step-Up Module
6.2.10 QUERIES
Syntax:
Example:
If you want to select the data present in the columns “name” and “days” in the table
name “months”, you should enter the following statements:
The above example will display all the entries, i.e. months and days as present under
the table name “months”. In case we wish to display all the entries present in a given
table, we use * (Star) operator.
SELECT *
FROM months;
If we wish to display certain fields without any duplication, then we would use the
DISTINCT keyword along with SELECT.
The above entry will display all the data entries under the column “name” without any
duplication. Every data entry will be unique.
199
Using ‘WHERE’ COMMAND for DATA SELECTION in SQL
There can be a situation where we need to select certain records from a given table.
The WHERE keyword acts as a filtering mechanism for the data present in a certain
table. For extracting the data, there is a need to follow certain conditions. Only if those
conditions are met, the data will be extracted.
Syntax:
Example:
name days
April 30
June 30
September 30
November 30
Once these statements are executed, all the names of the months that have 30 days
will be displayed. This is because we had selected the entries under the category
“Name” and “Age” and applied a condition that the age of the people should be 18
years. As per the example, the names Shashi, Tarun and Akhil will be displayed along
with their age.
200
Let's Learn Artificial Intelligence - Step-Up Module
AND Operator:
This operator displays the record if all the conditions that have been entered and
separated by AND are true.
Syntax:
Example:
SELECT *
FROM months
WHERE days = ‘30’ AND name = ‘April’;
OR Operator:
This operator displays a record if even one of the conditions that have been entered
are true. The conditions will be separated by an OR keyword.
Syntax:
Example:
SELECT *
FROM People
WHERE days = ‘28’ OR num = ‘10’;
201
num name days
2 February 28
10 October 31
NOT Operator:
NOT operator displays a record if the condition/conditions mentioned in the code are
not true. Let us understand the function of a NOT operator through an example.
Syntax:
Example:
SELECT *
FROM People
WHERE NOT days = ‘31’;
202
Let's Learn Artificial Intelligence - Step-Up Module
An aggregate function is the one wherein the values of multiple rows are grouped as
input based on a specific criterion which returns a single value. Some of the commonly
used aggregate functions in SQL are COUNT, SUM, AVG, MIN, MAX. Let us discuss each
of these aggregate functions for understanding their usage in practical applications.
COUNT ( ) Function:
COUNT function displays the number of rows that fulfil the specified criteria.
Syntax:
Example:
COUNT(days)
12
The given code asks the compiler to count all the records under the column “days”
and display the number of records present as the output.
AVG ( ) Function:
The AVG function displays the average of the data entries under a particular numeric
column.
Syntax:
SELECT AVG(column_name)
FROM table_name
203
Example:
SELECT AVG(days)
FROM months
The given code will display the average of the entries present under the column “days”.
Hence, the output would be:
AVG(Age)
30.41
SUM ( ) Function:
The SUM function will display the sum of all the data entries present under a particular
numeric column.
Syntax:
SELECT SUM(column_name)
FROM table_name
Example:
SELECT SUM(days)
FROM People
The code will add up the age of all the seven records present in the table. Therefore,
the output will be:
SUM(Age)
365
MIN ( ) Function:
MIN function returns the smallest value as present in a numeric column.
204
Let's Learn Artificial Intelligence - Step-Up Module
Syntax:
SELECT MIN(column_name)
FROM table_name
Example:
SELECT MIN(num)
FROM months
The code will display the minimum age among all the records as present in the column
“num”. In this case, the output will be:
MIN(num)
1
Note: If we want to modify the heading in the output, we use the “AS” function. For
example, if we want to change the heading “MIN(days)” to “Smallest_Month”, we will
execute the following code:
Smallest_Month
16
MAX ( ) Function:
MAX function will return the largest value as present in a particular numeric column.
Syntax:
SELECT MAX(column_name)
FROM table_name
205
Example:
SELECT MAX(num)
FROM months
MAX(num)
12
Sometimes, there is a need to modify the data present in certain rows. This operation
is performed using the UPDATE keyword. The steps involved in using the UPDATE
keyword are:
1. Choose the table where the record needs to be modified
2. Set new values for the desired column(s)
3. Select the rows using WHERE keyword where the modification needs to be
done
206
Let's Learn Artificial Intelligence - Step-Up Module
BETWEEN Condition:
The SQL BETWEEN condition allows the user to test if an expression is within a range
of values (inclusive). These values can be in the form of text, date, or numbers. Also, it
can be used in statements containing SELECT, INSERT, UPDATE, or DELETE keywords.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
342 Appu 76 54 65
271 Pari 84 90 88
562 Kiara 56 82 74
875 Sohini 92 94 90
198 Biju 89 60 81
974 Samar 78 54 63
Consider the table given. Suppose if we want to display the name of the students
who scored marks between 80-95 in Mathematics, then we would use the BETWEEN
keyword and the code will be written as:
SELECT SName
FROM Result
WHERE Maths
BETWEEN 80 AND 95;
207
The output after the code is executed would be:
SName
Pari
Kiara
Sohini
Similarly, if we want to display the ID and names of the students who scored between
80-95 in Physics, then we would write the following code:
ID SName
271 Pari
562 Kiara
875 Sohini
IN Condition:
The IN operator is useful in testing if the expression matches any value in the existing
list of values. By using IN operator, there is no need of using multiple OR condition in
SELECT, INSERT, UPDATE or DELETE.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (list_of_values);
208
Let's Learn Artificial Intelligence - Step-Up Module
Example:
Refer to the table “Result” given below. Write an SQL code to display the ID and the
name of students who have scored 75 marks in Mathematics.
342 Appu 75 54 65
271 Pari 80 90 88
562 Kiara 75 82 74
875 Sohini 75 90 90
198 Biju 90 60 81
974 Samar 84 90 63
Output:
ID SName
342 Appu
562 Kiara
875 Shi
Similarly, if we want to display the ID and the name of students who have scored 90
marks in Physics, we can write the following SQL code:
209
Output:
ID SName
271 Harsh
875 Shalini
974 Mehak
Similarly, if we want to display the ID and the name of students who have scored 90
marks in Physics, we can write the following SQL code:
ACTIVITY 2 (Part 5)
3 Kevin 12 120 7
3. Add more entries for Pari, Sohini, Samar, Kiara, Appu and Biju:
4. Display the records who have played a specified number of matches and
taken a specified number of wickets.
5. Display the names of the players who have scored maximum runs, played
maximum matches and taken maximum wickets. Also, write a code to display
the players who have scored minimum runs, played minimum matches and
taken minimum wickets.
6. Display the average of the total runs scored.
7. Replace the entry in row number 5 (ID 5) with the given data:
5 Goldie 14 198 10
8. Display the records who have scored runs between 110-150.
9. Display the records who have played more than 10 matches.
210
Let's Learn Artificial Intelligence - Step-Up Module
SUMMARY
Databases offer a convenient and powerful way to store and organise information.
A Spreadsheet is a computer program that looks like a large sheet of paper with boxes.
It has a grid of cells that form rows and columns. Usually, the rows in a table are called
records and the columns in a table are called fields or attributes.
Data sorting is the process of arranging the data into a meaningful order to enable
the viewer to analyse the information more effectively. It can be done in ascending
and descending order. The computer sorts the elements of a list into the desired order
using a Sorting Algorithm.
The filter feature applies a dropdown menu to each column heading, allowing you to
select specific choices to narrow a table.
Excel formulas all start with an = sign written in the empty cell, followed by the function
name (In this case ‘sum’) and a section in parentheses to provide ranges or arguments.
A database with only one table is called a flat database. A database that contains two
or more related tables is called a relational database. Some important concepts of
relational databases are:
• The primary key is a field whose values are unique in this table, and so can be
used as identifiers for the records.
• When two tables have an unequal relationship, we call the independent table
the parent and the dependent table the child.
• Cardinality describes how many records in one table can be related to records in
another table. Two tables might have a cardinality of 1-1, 1- oo, 1-3, oo – oo
Specialized database development and management tools are used to perform various
types of operations in a given database. These operations can be adding, deleting,
updating, inserting and displaying information as stored in a database.
Structured Query Language (SQL) is a language used to create databases objects such
as tables or views as well as to view or change data in the databases. It is an important
database development and management tool.
SQL statements or queries are used to perform tasks such as:
• Create, modify and delete database objects such as tables of views
• Update data on a database
211
• Retrieve data from a database
• Grant permissions and connect tables, etc.
212
Let's Learn Artificial Intelligence - Step-Up Module
213
214
UNIT 7
PYTHON
Topic Covered
Introduction To Python | Integrated Development Environment (IDE) |
Core Programming Concepts | Essential Data Science Libraries |
Troublesh Ooting Defects In Python Code | Practice For Writing Code
7.1 INTRODUCTION TO PYTHON
As we have learnt in the base module, Python is one of the various languages used to
communicate with computers.
Python is an interpreted, high-level, general-purpose programming language. Created
by Guido van Rossum and first released in 1991. It is mainly used for Data Science since
Python provides easy and simple tools for data mining and data analysis.
Because of it's precise and efficient syntax, Python is fast becoming one of the most
favoured languages used for Artificial Intelligence and Machine Learning. Python also
has various packages called 'libraries' for data analysis and machine learning, which
drastically reduce the time it takes to produce results.
A python program is a sequence of Python statements (code ) written to perform
some computational task. Like the word document has an extension of .doc or .docx,
the python scripts are stored in files and these files are named with the extension .py.
For example, hello.py.
While the world of Python is quite vast, to start one does not have to master everything
and learn all the syntax. You can start with the following:
With this approach, you can build mastery over time, as well as enjoy the process.
216
Let's Learn Artificial Intelligence - Step-Up Module
Visual
Jupyter Spyder PyCharm PyDev
Studio
Out of these, we shall help you work with Jupyter, but you can explore the others as well.
217
7.2.2 Downloading Jupyter Notebook
Step 1: The open-source version can be downloaded from the following site:
https://www.anaconda.com/products/individual
Anaconda Individual Edition is the industry standard for data scientists
developing, testing and training on a single machine.
Step 2: After installing it. Go through the brief introductory webinar to understand
the kind of packages that Anaconda offers and how to install them.
218
Let's Learn Artificial Intelligence - Step-Up Module
Step 3: Now open the application ‘Anaconda Navigator’ from your computer.
You will see all the packages included in Anaconda on the home screen, one of which
will be Jupyter Notebook.
219
Step 4: Launch Jupyter Notebook.
Step 5: After completing the instalment procedure, open the Jupyter Notebook
application. It will open in the browser. The preferred browsers are Chrome,
Safari and firefox. It may also open in Opera and Edge; however, if it doesn't,
one should use Chrome, Safari and Firefox.
You will see a similar-looking screen to the one given below:
Step 6: To write and run code, click on the 'New' button and then select Python 3.
These files can be saved and named. All the files created by you in Jupyter will
have the extension '.ipynb'.
You can create a Folder too, by selecting Folder, where you can save all your program
files. The folders can be renamed by selecting it and then clicking the rename button
on the top.
220
Let's Learn Artificial Intelligence - Step-Up Module
Step 7: After selecting New Python 3, the following screen will open, wherein you
can write the code and click on 'Run' to view the output. If there is any error,
it will show the error so that you can rectify it.
221
Installing Jupyter Notebook using Amazon Sagemaker
An Amazon SageMaker notebook instance is a fully managed ML compute instance
running the Jupyter Notebook App. Use Jupyter notebooks in your notebook instance
to prepare and process data, write code to train models, deploy models to Amazon
SageMaker hosting, and test or validate your models.
A best practice when using an Amazon SageMaker notebook is to use the notebook
instance to orchestrate other AWS services.
Below steps ask for necessary information required to create SageMaker notebook
instance. For other optional fields that are not covered in below steps, SageMaker will
choose the default values.
EXPLORE
https://console.aws.amazon.com/sagemaker/
Step 3: On the Create notebook instance page, provide the following information:
Step 4: For Notebook instance name, type a name for your notebook instance.
a. For Notebook instance type, choose an instance type for your notebook
instance.
b. For IAM role, ask your faculty/instructor for the role to choose from an
existing IAM role in your account that has the necessary permissions to
access Amazon SageMaker resources.
c. For Root access, to enable root access for all notebook instance users, choose
Enable. To disable root access for users, choose Disable. If you enable root
access, all notebook instance users have administrator privileges and can
access and edit all files on it.
d. Choose Create notebook instance.
In a few minutes, Amazon SageMaker launches an ML compute instance (a notebook
instance) and attaches an ML storage volume to it. The notebook instance has a
preconfigured Jupyter notebook server and a set of Anaconda libraries.
222
Let's Learn Artificial Intelligence - Step-Up Module
When the status of the notebook instance is InService, in the console, the notebook
instance is ready to use.
• Choose Open Jupyter next to the notebook name to open the classic Jupyter
dashboard.
• You can choose Open JupyterLab to open the JupyterLab dashboard.
C:\user\myprojects\btcat>jupyter notebook
Multiple notebooks can be created and used. Here is a screenshot of jupyter with 4
BTCAT notebook (. ipynb) files
223
7.3 CORE PROGRAMMING CONCEPTS
You must have learnt the fundamental concepts of Python language from the Base
Module, which are:
How to How to
Variables Deleting a
print a declare and
and variable in
simple use a variable
functions Python
string in Python
1 2 3 4 5 6 7
Standard
How to How to
data types
print blank concatenate
(Numbers,
lines variables in
String,
Python
List, Tuple,
Dictionary)
Let us go deeper into a few of these concepts and learn some more.
1. Variables
Variables are names to store data and use in computations. In Python, variables
can be a sequence of characters, including special character underscore ('_ ').
224
Let's Learn Artificial Intelligence - Step-Up Module
Variables are containers for storing data values. Unlike other programming languages,
Python has no command for declaring a variable. A variable is created the moment
you first assign a value to it. Variables can be integers, decimals (Floats), strings (We
will see what they are in a moment) and we can also perform calculations and much
more inside variables.
Example 1:
Enter
To run this program in jupyter, we press the shift key and enter key together on
Shift
the keyboard or click on run.
Example 2:
Consider calculating the area of a rectangle in Python: Let the sides of the rectangle
are given by length =10m, width=20.5 m:
Width = 20.5m
Length = 10.m
225
We store the value of length and width in variables, say a and b respectively:
In programming, we choose names that improve the readability of the program to the
outside person. Hence we define naming conventions.
Example 3:
The equivalent program to compute the area of a rectangle can be written as:
From these three programs, we understand that variables are used to store the values
for future calculations and print() is a function, in which we can write the meaningful
statement in 'single quotes followed by a comma (,) and the result variable. In the above
example, the result variable name is 'rectangle_area'. Though both these two programs
perform the same computation –computing area of the rectangle, the second version
above is more readable. However, the above programs have a limitation.
How do we compute the area of a rectangle of 'any length' and 'any width'?
226
Let's Learn Artificial Intelligence - Step-Up Module
2. Functions (Methods)
One way to answer the above question is by giving a name to the piece of the
program so that the values can be passed to that and that piece of the program
uses the values to perform the computation and return the resulting value. For
example, say computing area of the rectangle, in the above example, we can
write a function in Python that takes length and width as arguments so that
whenever we pass the values of length and width of a rectangle as arguments,
the function computes the area for given arguments (or parameters) and the
function returns the result.
Example1:
In Python, a function is defined using a special word (we call key word: def), followed
by the name of the function and the place holders (arguments) for the values when we
call this function. This can be seen from cells [4] and [5] above. In cell [5], we declared
two variables x,y and stored length and width values. Here we call the function using
the statement: result =compute_area(x,y)where the values 10, 20.5 get copied to length
and width respectively, and the function returns the value 10*20.5 by executing the
statement return length*width in the function compute_area.
Functions are a convenient way to divide your code into useful blocks, allowing you
to order your code and make it more readable, reuse it and save some time. Also,
functions are a key way to define interfaces so programmers can share their code.
227
Example 2:
Some functions may have some arguments which are variables passed into the
functions.
Example 3:
Note: The arguments have to be placed while calling the function otherwise a
"MissingPositionalArgument" error would come up
228
Let's Learn Artificial Intelligence - Step-Up Module
!
Remember that the type and order of parameters that we pass must be in the
same order as that of function defined.
Example 4:
A function in Python can return more than one value. So it is important that we collect
these returned values in the order in which the function returns them. In the function
above, get_SA_and_Volume(radius) contains a sequence of statements (i.e., four
statements) which are indented with some column margin (called indentation). Such
a group of statements with indentation is called a block and the statements within
the block can be called as compound statements. Python allows us to create such
blocks with 4 space indentation. As you see, the jupyter notebooks by default we get
this indentation instead of we typing these blank space.
3. Indentation
Indentation refers to the spaces at the beginning of a code line. Where in other
programming languages the indentation in code is for readability only, the
indentation in Python is very important. Python uses indentation to indicate a
block of code.
229
Example 1:
4. Data Types
We select a suitable data type based on the computation that we are performing.
For example, to perform arithmetic operations such as the above examples
where we used floating-point (real numbers for the performing area of the
rectangle. Similarly, for processing character-based sequences (strings), it will
be unrealistic to think operations such as multiplication of strings and division of
strings. However, there are specific computations defined for each of these data
types. For example, string reversal, finding a character in a string at a particular
location, finding the number of characters in the string (length) etc. are valid
operations on the strings. Below is a high level view of various data types we
commonly use in Python.
230
Let's Learn Artificial Intelligence - Step-Up Module
Data Types
Complex Complex
Integer Float Integer Float
Number Number
Integers:
Integers are whole numbers – for example, 5, 10, ….
Floating-point numbers:
Unlike integers floating point numbers will have two components, namely integer
part and fractional part. For example, 24.8973.
Strings:
Strings are a sequence of characters enclosed between either single quotes or double-
quotes.
Example:
231
Boolean:
Boolean in Python takes two values True, False. Notice the first character written
in capital Letter. The expressions which are part of checking conditions in branch
statement and iteration statements are evaluated to True or False. The statement
types you will see in the subsequent section.
In Python, the type of the variable used in computation is based on the value we assign
on the right-hand side of the assignment statement. The following example illustrates
this where we use the built-in function: type() to check the type of the variable –write
the value within these parentheses. For example, a string value can be assigned with
single quotes '-‘ or double quotes “-“.
5. Statements
232
Let's Learn Artificial Intelligence - Step-Up Module
For example, printing the name of a student 10 times involves usage of iterative
statement.
Sequential Statements:
A flow chart for computing volume of a cone (V), given the radius r, height h is given
by V=1/3 πr2 h:
PI=22/7
cone_height=14.0
radius=8.0
cone_volume=(1/3)*PI*radius*radius*cone_height
Conditional Statements:
Conditional statements help us define the change in behaviour of the program –
for example, execution of a few set of statements under one condition. Hence the
condition checking is performed through Boolean and logical expressions using
suitable operators appropriately and the results of these expressions are either True
or False.
233
Boolean Result Logical (and, or, not) Result
Operation
x != y x is not equal to y ? (x%3==0) and x is divisible by 3 and x
(x%2==0) is divisible by 2?
x == y x equals y ? x >3 and x <=10 x lies between 4 and 10?
234
Let's Learn Artificial Intelligence - Step-Up Module
You can also have if statement without else block. Also, Python provides chained
conditionals -through if – elif – else programming construct as explained below:
Observe that the above construct is helpful, particularly in the case where operands x
and y are common in each branch of test evaluation.
Iteration Statements:
Iteration statements are helpful when a
piece of program code to be executed
for a specific number of times, say as a
repetitive task.
This is where the computations
performed by programs are error-free
compared to human beings because
we as humans might make mistakes,
especially the performing the same
task again and again due to overlooking
certain aspects.
As shown in the flow chart, the body
of the look gets executed until the test
condition fails. Once the test condition
fails, we call it a termination condition
for the loop, and the statements after
the loop get executed as usual. Python provides while statement as two iteration
constructs namely while loop and for a loop as given below: One of the vital aspect for
iteration statements I update of the loop variable. The following example illustrates
the points discussed here.
235
Initialization of iteration counter
! The termination condition (i.e., when to stop iterations) is provided as part of the
body of while either through the count update till desired no of iterations or
when a particular result is obtained during these iterations.
! The loop without termination condition gets executed indefinitely and hence it
is called infinite while. Hence the programmer needs to keep in mind regarding
this termination condition while developing iterative constructs.
break statement can be used for early termination of the loop as shown in the
below:
Also, multi-line comments can be written by enclosing the text within the
three single quotes (‘ ‘ ‘) in the beginning and at the end.
Python
Consider a string: My_str=’Python’; Let us initialize
index variable, say, index=0. My_str[0] contains
character ‘P’, My_str[3] contains character ‘h’
and so on. We increment index and retrieve
the characters from the string. The following 0 1 2 3 4 5
example illustrates this.
236
Let's Learn Artificial Intelligence - Step-Up Module
The following function takes an input string (a sequence of characters) that are ended
with ! and returns the string till $. In this example, the input string variable is input_
string,
while loop can be used when the termination condition is not known prior to
the execution. In the case of the previous example, the loop reads character by
character until it encounters the character ‘!’. This indicates, prior to the
execution of the loop, we may not always know the number of characters in
the input string. Hence while the loop is called the indefinite loop.
Sometimes we want to iterate through a collection of items such as a list of
numbers, words, etc., where the size of the collection is finite. In such cases, we
use for loop, as shown below:
We know in mathematics, a prime number is the one which has 1 and itself as factors.
We have may such prime integers in the set of natural numbers, N. For example, 3, 7,
11, 29 … these are prime numbers as they don’t have any other factors except 1 and the
number itself. Note that 2 is only even prime number.
Consider a program that checks whether a given number n is a prime or not. One
way of checking it is to divide the number with all the numbers from 1 to N and check
whether there are any factors between 1 and the number, n itself. So we generate a
sequence of integers in the range of 2 to n-1 and divide n with each number from this
sequence. We initially assume that the given number is prime. During the division on
the first encounter of the factor number from the sequence, we flag that the number
is not prime. Let us see how it works:
237
Let n = 6; The sequence: 2, 3, 4, 5 and 2 is a factor of 6 and the function should return
that 6 is not prime OR is_prime(6) is False. Similarly, the function is_prime(5) should
return true.
Consider the program snippet for this problem below:
There are other efficient ways of implementing this function. For example, instead of
checking for factors from 1 to n, even 1 to √n which you can try on your own and see.
The statement for i in range(2,n) is an iterative statement and all the statements
within its scope (body of the loop) are executed n-1 times
Python provides in-built function range(2,n) that returns list of values [2, 3, ...,n-1]
The statement if n%i == 0 is a branching statement. The operator % returns
reminder when n is divided by i
Lists
Lists can contain any type of variable, and they can contain as many variables as
you wish. Lists can also be iterated over in a very simple manner.
238
Let's Learn Artificial Intelligence - Step-Up Module
Example 1
Note: The index of python starts from 0, and so
0=1
1=2
2=3
Example 2
My_list is initialized with 4 elements of int, int, string, and float type values respectively.
for loop provides ease of use and readability working with List data structure.
239
my_list 12 13 ‘hello’ 4.5
index positions 0 1 2 3
Elements of the list can be accessed using index position List methods and usage:
Index positions help in accessing elements of the list. In Python index starts
from 0.
In the above example, my_list[0] returns the first element in my_list, i.e., 12,
my_list[1] returns the second element in my_list, ie., 13 etc.
The last index position of the list will be one less than the number of elements
in the list. In the above example, there are 4 elements and the last index is 3
and hence my_list[3]is the last element with value 4.5.
Tuples
Example 1
240
Let's Learn Artificial Intelligence - Step-Up Module
As you notice, there are updated methods available in the list data structure (like
append, which helps you adding a new object to the existing list). Unlike lists, tuples
in Python are immutable, indicating that as the tuple is defined, we will not be able
to change the contents of it through addition or deletion of its elements. The tuple is
useful in the situations where you would like to provide the data to the outside world
and at the same time, you prevent modification of the data. The common example
of such a situation is to read employee records from a file and get the relevant details
without actually modifying any of these records. Following Python example illustrates
storing and retrieving student records which are stored in a list. The retrieval is based
on the grade ‘A+’.
Example 2
241
Step3: Retrieve the student records who scored ‘A+’ grade:
Exercise: Re-implement this using set data structure creating a set object S =set( )
Which of the above to data structures is appropriate in the given context and why?
Dictionary
The data structures you have learned so far, i.e., strings, lists and tuples use
integers as indices and you retrieve the value at a particular index position by
[<index_position>]. You will notice an error If you use any other data type. Also
frequently you require to store the data based on a key-value which can be a
string such as the name of a person. In such cases, dictionary data structure
comes handy. Dictionaries allow you to use any other immutable type as an
index, such as string, tuple. Each entry in the dictionary contains an index and
a value separated by a colon (:). We can add a new object to dictionary using [ ]
operator.
Example 1: The following code example illustrates the usage of the string as an index.
In this example, we create a dictionary depicting the number of fruits in a basket:
242
Let's Learn Artificial Intelligence - Step-Up Module
Alternatively, you can also initialize all fruits in one statement in a dictionary:
Python Syntax
Python Comments
Python Casting
Python Booleans
Python Operators
Python Sets
Python Dictionaries
243
Python If...Else
Python Functions
Python Lambda
Python Arrays
Python Scope
Python Modules
Python Iterator
Python Dates
EXPLORE
You can learn these from any one of the following links:
https://www.w3schools.com/python/default.asp
https://www.programiz.com/python-programming
https://www.techbeamers.com/python-tutorial-step-by-step/#tutorial-list
You can practice writing lines of code using the concepts learnt in Jupyter Notebook/
Python 3.
244
Let's Learn Artificial Intelligence - Step-Up Module
A collection of core modules can be called libraries. A Library can also be called a
module. Some of the most important modules of Python for Data Science include:
Scikit
Pandas Numpy
Learn
To learn these:
Read the library's training materials from the website provided after each.
We recommend diving much deeper into a library later, as one may forget most of
what has been learned by the time it’s time for using it in the projects. Instead, right
now one can aim to discover what each library is capable of.
245
7.4.1 Pandas
Pandas is a software library created for the Python programming language. It is aimed
at data manipulation and analysis. It primarily offers data structures and operations
for manipulating numerical tables and time series. Given below are examples of some
pandas functions;
You can learn the following using Pandas by visiting the website provided.
Creating a DataFrame
Indexing DataFrames
Combining Datasets
Computational Tools
Visualization
246
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
https://pandas.pydata.org/ https://www.w3resource.com/
pandas-docs/stable/user_guide/ pandas/index.php
index.html#user-guide
7.4.2 Numpy
Numpy is a library for the Python programming language, adding support for large,
multi-dimensional arrays and matrices. It has an extensive collection of high-level
mathematical functions to operate on these arrays. Let’s look at some Numpy functions
Python NumPy
NumPy Intro
247
NumPy Copy vs View
NumPy Random
NumPy ufunc
EXPLORE
https://numpy.org/doc/stable/ https://www.w3schools.com/
user/ python/default.asp
7.4.3 Scikit-Learn
Scikit-learn is a free software or machine learning library for the Python programming
language. It features various classification, regression and clustering algorithms
EXPLORE
https://scikit-learn.org/0.18/_ https://scikit-learn.org/stable/
downloads/scikit-learn-docs.pdf index.html
248
Let's Learn Artificial Intelligence - Step-Up Module
1. Dash
Dash is a Python framework for building web applications. It is built on top of Flask,
Plotly. It is used to build dashboards based on Python.
2. Plotly
Plotly provides online graphing, analytics, and statistics tools for individuals and
collaboration.
3. Matplotlib
Matplotlib is a plotting library, used with the Python programming language and its
numerical mathematics extension NumPy.
249
7.4.6 Heroku and deploying applications
Heroku
1). Type the command “Heroku login” to login to your Heroku account
4). Install gunicorn with pip(I already have it, so the output says ‘requirement already
satisfied’)
5). Next, since we can’t run ‘pip install …’ commands on the server, we will have to
make a requirements file which consists of all the modules we require (Changes
can be made by going to the file explorer and add the modules missing into the
file)
6). Use the normal git commands to add and commit the code
7). Specify the app name to the application you have created
8). Before we start pushing the code into ‘flaskexampleproject’, let’s make sure that
we will be pushing the code to the right place. Do so by running this command
v
250
Let's Learn Artificial Intelligence - Step-Up Module
In the jupyter notebook, print() and other functions to inspect the objects can be used.
For instance, head() of dataframe, shape() of dataframe, etc
Here is a simple example to inspect variable and code statements
251
And the below screen shows the results
252
Let's Learn Artificial Intelligence - Step-Up Module
For Beginners:
Beginners can use the following websites to get very simple exercises and their
solution. Try to develop the solution to the exercises on your own using Python 3. After
you are done, you can compare it with the solution provided. Always remember that
there are more than ways to get the desired output.
EXPLORE
https://w3resource.com/
https://www.w3schools.com/
python-exercises/heap-queue-
algorithm/index.php python/python_exercises.asp
http://www.practicepython.org/
In later Units, you will be given projects that you can do where you will have to use
Python extensively. Hence, it is a good idea that you practice with as many exercises
as you can.
ACTIVITY
Try this simple activity for creating a Chatbot using Python
EXPLORE
253
SUMMARY
254
Let's Learn Artificial Intelligence - Step-Up Module
• Dictionary: The data structures you have learned so far, i.e., strings, lists and
tuples use integers as indices and you retrieve the value at a particular index
position by [<index_position>].
A collection of core modules can be called libraries. It can also be called as a module.
Some of the most important modules of Python for Data Science include:
• Pandas is a software library created for the Python programming language,
aimed at data manipulation and analysis.
• Numpy: Numpy is a library for the Python programming language, adding
support for large, multi-dimensional arrays and matrices.
• Scikit-Learn: Scikit-learn is a free software or machine learning library that
features various classification, regression and clustering algorithms
Data visualization is the graphic representation of data. Python allows the same using
Dash, Plotly and Matplotlib.
Heroku is a container-based cloud Platform as a Service. Developers use Heroku to
deploy, manage, and scale modern apps.
255
256
Let's Learn Artificial Intelligence - Step-Up Module
UNIT 8
ALGORITHMS
Topic Covered
Introduction to Machine Learning Algorithms |
Algorithms Used in Supervised Learning |
Algorithms Used in Unsupervised Learning |
Algorithms Used in Reinforcement Learning
257
8.1 INTRODUCTION TO MACHINE LEARNING
ALGORITHMS
Algorithms are at the heart of Artificial Intelligence. It is the model that ultimately
does the intelligent work.
In the Base module, you had dabbled a bit with Algorithms, so you would remember
that Algorithms are step-by-step instructions to solve a problem, created along with
rules that decide which path to take when a decision is faced. Numerous Algorithms
are used in various fields of Data Science. In this Unit, you will learn about the Algo-
rithms very commonly used in Artificial Intelligence and Machine Learning.
In Machine Learning, the Algorithm is a program that can alter its own parameters
based on given responses on the past predictions of the data set. Based on the type
of problems to be solved, we can divide Machine Learning Algorithms into three main
categories:
258
Let's Learn Artificial Intelligence - Step-Up Module
ML Algorithm
Types Types
Classification
Supervised
Learning
Regression
Association
Machine Rule
Learning
Problems
Unsupervised
Clustering
Learning
Reinforcement Dimensionality
Learning Reduction
259
8.2 ALGORITHMS USED IN SUPERVISED
LEARNING
In supervised learning problems, the machines are provided with a labeled training
dataset, which means that the values to be predicted are known for the algorithm
from the very beginning.
The process is called ‘supervised’ learning since it is ‘supervised’ by the human-labeled
output variable, i.e. the input variables (x) and an output variable (y) are known.
The objective of the problem is to find a suitable mapping function f from x to y. Then,
when new inputs come in, we can apply the function f to predict the corresponding
output.
y = f(X) or Output = f(Input)
The machine learns and improves the function f through iterative optimization, where
an initial guess is used to generate a sequence of improving approximate solutions
for a class of problems. This process involves minimizing the difference between the
estimated and the actual output.
We can divide supervised learning into regression and classification based on whether
the output variable/target is Numerical or Categorical.
Numerical data are represented by numbers such as body weight, the number
of dogs.
Choice of the algorithms for Categorical or Numerical data, within Supervised Learning,
is as follows:
Categorical
Data Classification
Supervised
Learning
Numerical
Regression
Data
260
Let's Learn Artificial Intelligence - Step-Up Module
8.2.1 CLASSIFICATION
261
Thus, classification comes into play at any place where we need to predict an outcome,
from a set number of fixed, predefined outcomes.
Below are some examples of classification applications:
Classify customers of banks into different credit profiles based on existing
customers’ status.
O Input variables include income, age, gender, location, expenses, etc.
o Output variable is, whether a customer defaults on the payment to banks.
262
Let's Learn Artificial Intelligence - Step-Up Module
• Quadratic classifiers
• Kernel estimation
o k-nearest neighbor
• Decision trees
o Random forests
• Neural networks
EXPLORE
https://analyticsindiamag. https://dzone.com/articles/
com/7-types-classification- introduction-to-classification-
algorithms/ algorithms
263
ACTIVITY
Try to train and evaluate a regression model by using 2 popular Machine Learning
Platforms, AWS SageMaker and Microsoft Azure.
Prerequisites:
Scikit-learn
Microsoft Azure subscription
Visual Studio Codespace based on the MicrosoftDocs/ml-basics GitHub
repository.
EXPLORE
Prerequisites:
Jupyter Notebook
AWS SageMaker Studio Subscription
EXPLORE
(Image Classification) :
https://www.getstartedonsagemaker.com/workshop-studio-training/
trainingbuilt-in/imgclassification/
264
Let's Learn Artificial Intelligence - Step-Up Module
8.2.2 REGRESSION
265
Time series forecasting.
o Input variables are the values of the time series (the historical data).
o Output variable: the value of the time series at a later time.
266
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
https://www.youtube.com/ https://www.youtube.com/
watch?v=nk2CQITm_eo watch?v=yIYKR4sgzI8
https://www.justintodata.com/ https://www.justintodata.com/logistic-
linear-regression-machine- regression-for-machine-learning-
learning-python-tutorial/ tutorial/
ACTIVITY 2
Try to train and evaluate a regression model by using two popular Machine
Learning Platforms, AWS SageMaker and Microsoft Azure.
Prerequisites:
Scikit-learn
Microsoft Azure subscription
Visual Studio Codespace based on the MicrosoftDocs/ml-basics GitHub
repository.
EXPLORE
267
Using AWS SageMaker
Prerequisites:
Jupyter Notebook
AWS SageMaker Studio Subscription
EXPLORE
268
Let's Learn Artificial Intelligence - Step-Up Module
The term ‘unsupervised’ refers to the fact that the algorithm is not guided like
Supervised Learning Algorithm.
Machine Learning Algorithms that use unsupervised learning bring order to the
dataset and make sense of data. They are used to group unstructured data according
to their similarities and distinct patterns in the dataset.
When we only have input variables (X) but no corresponding output variable, the
training dataset is unlabeled. The machine can still extract structures within the
dataset, such as grouping or clustering of data points.
This becomes an unsupervised learning problem. You can also think of it as ‘Data
mining’ without any existing knowledge/labels.
The Unsupervised Machine Learning Algorithm is used to:
Explore the structure of the information and detect distinct patterns
Extract valuable insights
Implement this into its operation to increase the efficiency of the decision-
making process
Exploration of
data Clustering
Extracting Dimensionality
valuable reduction
information
269
8.3.1 CLUSTERING
‘Clustering’ is the term used to describe the exploration of data, where the similar
pieces of information are grouped. There are several steps to this process:
Defining the credentials that form the requirement for each cluster. The
credentials are then matched with the processed data and thus the clusters are
formed.
Breaking down the dataset into the specific groups (known as clusters) based
on their common features.
Clustering techniques are simple yet effective. They require some intense work yet
can often give us some valuable insight into the data.
270
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
To learn more about these, you can go through the following links:
https://www.analytixlabs.co.in/blog/types-of-clustering-algorithms/
https://www.youtube.com/watch?v=7xHsRkOdVwo
https://www.youtube.com/watch?v=4b5d3muPQmA
ACTIVITY 3
Try to train and evaluate a clustering model by using two popular Machine Learning
Platforms, AWS SageMaker and Microsoft Azure.
Prerequisites:
Scikit-learn
Microsoft Azure subscription
Visual Studio Codespace based on the MicrosoftDocs/ml-basics GitHub
repository
271
EXPLORE
Prerequisites:
Jupyter Notebook
AWS SageMaker Studio Subscription
EXPLORE
272
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
https://www.tutorialandexample.com/association-rule-learning-algorithm/
https://www.aionlinecourse.com/tutorial/machine-learning/association-rule-learning
273
Feature extraction reduces the data in a high dimensional space to a lower
dimension space, i.e. a space with lesser no. of dimensions.
For example, reducing 3 dimensions/variables to 2 dimensions
EXPLORE
https://courses.analyticsvidhya.com/courses/dimensionality-reduction-for-machine-
learning
https://www.geeksforgeeks.org/dimensionality-reduction/
274
Let's Learn Artificial Intelligence - Step-Up Module
Exploration is the process of the algorithm pushing its learning boundaries, assuming
more risk, to optimize towards a long-run learning goal. In an example of Tic-Tac-Toe,
this could take the form of running simulations that assume more risk, or purposefully
place pieces unconventionally to learn the outcome of a given move. Armed with a
greater possibility of manoeuvres, the algorithm becomes a much more fierce oppo-
nent to match against.
Exploitation is the process of the algorithm leveraging information it already knows
to perform well in an environment with short term optimization goals in mind. Back
to the Tic-Tac-Toe example, the algorithm would perform the same moves it knew to
produce a nominal probability of winning. However, in this learning mode, the ML al-
gorithm will not develop beyond elementary sophistication.
It is usually a hybrid of exploration and exploitation styles that produces the optimal
algorithm. In the same way that a human must branch out of comfort zones to in-
crease their breadth of learning, but at the same time cultivate their given resources
to increase their depth of learning.
In the hybrid exploration/exploitation technique:
an action takes place
the consequences are observed
the next action considers the results of the first action.
Further, reward signals occur upon performing specific tasks serving as a naviga-
tion tool for the reinforcement algorithms. They give it an understanding of right and
wrong course of action.
275
Two main types of reward signals are:
Positive reward signal encourages continuing performance a particular se
quence of action
Negative reward signal penalizes for performing certain activities and urges to
correct the algorithm to stop getting penalties.
Reward
Action
Agent Environment
276
Let's Learn Artificial Intelligence - Step-Up Module
EXPLORE
To learn more about this, you can go through the following links:
https://www.guru99.com/reinforcement-learning-tutorial.html#4
https://www.geeksforgeeks.org/ml-monte-carlo-tree-search-mcts/
https://www.youtube.com/watch?v=-fVu_bj8Os8
ACTIVITY 4
Using the learnings from this Unit as well as the previous units on Machine Learning,
NLP, Computer Vision and Python, try to do a project by using the MLaaS platform,
Amazom Sagemaker.
EXPLORE
https://www.getstartedonsagemaker.com/resources/sampleproblems/
277
SUMMARY
Algorithms are step-by-step instructions to solve a problem, created along with rules
that decide which path to take when a decision is faced.
In machine learning, the algorithm is a program that can alter its own parameters
based on given responses on the past predictions of the data set. ML algorithms can
be divided into three types:
• Supervised Learning: Classification and Regression algorithms.
• Unsupervised Learning – Association Rule, Clustering and Dimensional Reduction
Algorithms
• Reinforcement Learning
We can divide supervised learning into regression and classification based on whether
the output variable/target is Numerical (Classification) or Categorical (Regression).
Classification algorithms are used when the target is a categorical variable. Classification
is the act of dividing the variable that we are trying to predict into output categories
or classes and then predicting a class for a given input. Classification can broadly be
Binary, Multi-Class or Multi-Label.
When the output variable is numerical, we have a regression problem. It is about
estimating the relationship between the target (dependent variable) based on at
least one input variable (independent variable/feature). It is often used to predict or
forecast based on experience. The most common regression models used in Machine
Learning are of two types, 1) Linear Regression (Simple linear regression model and
Multiple linear regression model); 2) Logistic Regression.
Unsupervised learning applies three major techniques – 1) Exploration of data
(Clustering); 2) Finding relationships (Association Rule Learning); Extracting valuable
information (Dimensionality reduction).
Clustering is about separating a set of data objects into clusters. The observations in
the same group are more like each other than the observations in different groups.
It is used to recognize patterns among clusters in different fields such as biology,
marketing, etc.
Association rules are used to analyze transactions to find relationships among different
variables. It is mostly used in market basket analysis.
278
Let's Learn Artificial Intelligence - Step-Up Module
279
ACKNOWLEDGEMENT
We thank all contributors and facilitators who have supported the development of this
Module. We thank the NASSCOM team for facilitating the Industry-Academia workgroup in
this endeavor.
Companies Represented
280
Workgroup Members
01. Amit Nevatia Educate Program Lead, India/South Asia Amazon Internet Services
Pvt Ltd
02. Arokiasamy Stephen Raj Head of Data Science and AI Robert Bosch Engineering
& Business Solutions Pvt.
Ltd.
04. Ashish Mital Solutions Architect, India/South Asia Amazon Internet Services
Pvt Ltd
17. Kanishka Agiwal Head, AI/ML & Analytics, India/South Asia Amazon Internet Services
Pvt Ltd
281
21. Omjiwan Gupta Director Education Microsoft Corporation India
Pvt Ltd
29. Sachin Punyani Technical Business Development – AI/ML, Amazon Internet Services
India South Asia Pvt Ltd
32. Syama Sundar Cheruvu SME, Predictive Analytics and NLP, Accenture
AI Practice
33. Vinnie Jauhari Director Education Industry Expert Microsoft Corporation India
Pvt Ltd.
282
Let's Learn Artificial Intelligence - Step-Up Module
283
284
Let's Learn Artificial Intelligence - Step-Up Module
285
HAPPY
TINKERING