Python Machine Learning - (BooksHash)
Python Machine Learning - (BooksHash)
Andrew Park
Download the Audio Book Version of This Book for FREE
If you love listening to audio books on-the-go, I have great news for you.
You can download the audio book version of this book for FREE just by
signing up for a FREE 30-day audible trial! See below for more details!
For Audible US
For Audible UK
For Audible FR
For Audible DE
Table of Contents
INTRODUCTION
WHAT IS MACHINE LEARNING?
APPLICATIONS OF MACHINE LEARNING
ADVANTAGES AND DISADVANTAGES OF MACHINE LEARNING
MACHINE LEARNING – CONCEPTS & TERMS
OBJECTIVES OF MACHINE LEARNING
CATEGORIES OF MACHINE LEARNING SYSTEMS
STEPS IN BUILDING A MACHINE LEARNING SYSTEM
LINEAR REGRESSION WITH PYTHON
LINEAR REGRESSION WITH ONE VARIABLE
LISTS IN PYTHON
NESTED LISTS
SUMMARY OF LIST METHODS IN PYTHON
INBUILT PYTHON FUNCTIONS TO MANIPULATE PYTHON LISTS
MODULES IN PYTHON
MODULES CONCEPT AND UTILITY WITHIN PYTHON
MACHINE LEARNING TRAINING MODEL
SIMPLE MACHINE LEARNING TRAINING MODEL IN PYTHON
SIMPLE MACHINE LEARNING PYTHON MODEL USING LINEAR REGRESSION
CONDITIONAL OR DECISION STATEMENTS
CONDITIONAL TESTS IN PYTHON
CREATING MULTIPLE CONDITIONS
IF STATEMENTS
ESSENTIAL LIBRARIES FOR MACHINE LEARNING IN PYTHON
SCIKIT – LEARN
TENSORFLOW
THEANO
PANDAS
MATPLOTLIB
SEABORN
NUMPY
SCIPY
KERAS
PYTORCH
SCRAPY
STATSMODELS
WHAT IS THE TENSORFLOW LIBRARY
INSTALLING TENSORFLOW
ACTIVATE TENSORENVIRON
ARTIFICIAL NEURAL NETWORKS
DEFINITION OF ARTIFICIAL NEURAL NETWORK
WHAT ARE THE TYPES OF ARTIFICIAL NEURAL NETWORKS?
HOW TO TRAIN AN ARTIFICIAL NEURAL NETWORK?
ARTIFICIAL NEURAL NETWORK: PROS AND CONS OF USE
CONCLUSION
Introduction
For all that we know about Machine Learning, the truth is that we are
nowhere close to realizing the true potential of these studies. Machine
Learning is currently one of the hottest topics in computer science. If you are
a data analyst, this is a field you should focus all your energy on because the
prospects are incredible. You are looking at a future where interaction with
machines will form the base of our being.
In this installation, our purpose was to address Python Machine Learning
from the perspective of an expert. The assumption is that you have gone
through the earlier books in the series that introduced you to Machine
Learning, Python, libraries, and other important features that form the
foundation of your knowledge in Machine Learning. With this in mind, we
barely touched on the introductory concepts, unless necessary.
Even at an expert level, it is always important to remind yourself of the
important issues that we must look at in Machine Learning. Algorithms are
the backbone of almost everything that you will do in Machine Learning.
Because of this reason, we introduced a brief section where you can remind
yourself of the important algorithms and other elements that help you
progress your knowledge of Machine Learning.
Machine Learning is as much about programming as it is about probability
and statistics. There are many statistical approaches that we will use in
Machine Learning to help us arrive at optimal solutions from time to time. It
is therefore important that you remind yourself about some of the necessary
probability theories and how they affect outcomes in each scenario.
In our studies of Machine Learning from the beginner books through an
intermediary level to this point, one concept that stands out is that Machine
Learning involves uncertainty. This is one of the differences between
Machine Learning and programming. In programming, you write code that
must be executed as it is written. The code derives a predetermined output
based on the instructions given. However, in Machine Learning, this is not a
luxury we enjoy.
Once you build the model, you train and test it and eventually deploy the
model. Since these models are built to interact with humans, you can expect
variances in the type of interaction that you experience at every level. Some
input parameters might be correct, while others might not. When you build
your model, you must consider these factors, or your model will cease to
perform as expected.
The math element of Machine Learning is another area of study that we have
to look at. We didn’t touch on this so much in the earlier books in the series
because it is an advanced level study. Many mathematical computations are
involved in Machine Learning for the models to deliver the output we need.
To support this cause, we must learn how to perform specific operations on
data based on unique instructions.
As you work with different sets of data, there is always the possibility that
you will come across massive datasets. This is normal because as our
Machine Learning models interact with different users, they keep learning
and build their knowledge. The challenge of using massive datasets is that
you must learn how to break down the data into small units that your system
can handle and process without any challenges. In this case, you are trying to
avoid overworking your learning model.
Most basic computers will crash when they have to handle massive data.
However, this should not be a problem when you learn how to fragment your
datasets and perform computational operations on them.
At the beginning of this book, we mentioned that we will introduce hands-on
approaches to using Machine Learning in daily applications. In light of this
assertion, we looked at some practical methods of using Machine Learning,
such as building a spam filter and analyzing a movie database.
We have taken a careful step-by-step approach to ensure that you can learn
along the way, and more importantly, tried to explain each process to help
you understand the operations you perform and why.
Eventually, when you build a Machine Learning model, the aim is to
integrate it into some of the applications that people use daily. With this in
mind, you must learn how to build a simple solution that addresses this
challenge. We used simple explanations to help you understand this, and
hopefully, as you keep working on different Machine Learning models, you
can learn by building more complex models as your needs permit.
There are many concepts in Machine Learning that you will learn or come
across over time. You must reckon the fact that this is a never-ending
learning process as long as your model interacts with the data. Over time, you
will encounter greater datasets than those you are used to working with. In
such a scenario, learning how to handle them will help you achieve your
results faster, and without struggling.
What Is Machine Learning?
We live in a world where technology has become an inalienable part of our
daily lives. In fact, with all the rapid changes in technology these days,
machines enabled with artificial intelligence are now responsible for different
tasks like prediction, recognition, diagnosis, and so on.
Data is added or fed to the machines and these machines “learn” from these
data. These data are referred to as training data because they are used to train
the machines.
Once the machines have the data, they start to analyze any patterns present
within the data and then perform actions based on these patterns. Machines
use various learning mechanisms for analyzing the data according to the
actions that they need to perform. These mechanisms can be broadly
classified into two categories- supervised learning and unsupervised
learning.
You might wonder why there aren’t any machines designed solely to perform
those tasks that they are needed to carry out. There are different reasons why
Machine Learning is important. As already mentioned, all research conducted
about Machine Learning comes in handy since it helps us understand a
couple of aspects of human learning. Also, Machine Learning is
quintessential because it helps increase the accuracy, effectiveness, and
efficiency of machines.
Here is a real-life example that will help you understand this concept better.
Let us assume that there are two random users A and B who love listening to
music and we have access to their history of songs. If you were a music
company, then you can use Machine Learning to understand the kind of
songs each of these users prefers and thereby you can come up with different
ways in which you can sell your products to them.
For instance, you have access to noting down the different attributes of songs
like their tempo, frequency, or the gender of the voice, and then use all these
attributes and plot a graph. Once you plot a graph, over time, it will become
evident that A tends to prefer to listen to songs that have a fast tempo and are
sung by male artists, whereas B likes to listen to slow songs sung by female
artists, or any other similar insight. Once you obtain these data, you can
transfer them to your marketing and advertising teams to make better product
decisions.
At present, we have free access to all the historical data that have been
collected since the advent of technology. Not only we have access to these
data, but we can now store and process such large quantities of them.
Technology has certainly evolved, and it has come a long way when you look
at the way we can now handle such operations. The technology is so refined
these days that it provides access to more data to mine from.
Here are a couple of other reasons why Machine Learning is important.
Even with all the progress that engineers keep making, there will always be
some tasks that are incapable of being defined explicitly.
Some tasks must be explained to the machines with the help of examples.
The idea is to train the machine with the input of data and then teach it to
process it to produce an output. In this manner, the machine will be aware of
how it needs to deal with similar inputs of data in the future and process them
accordingly to generate the appropriate outputs.
The fields of Machine Learning and data mining are intertwined. Data mining
refers to the process of going through tons of data to find any bowlers
correlations or relationships that exist within. This is another benefit of
Machine Learning in the sense that it helps the machines find any vital
information.
There are numerous occasions where humans can’t design machines without
having an accurate estimation of the conditions within which such machines
will function.
The external conditions tend to have a major effect on the performance of the
machine. In such situations, Machine Learning helps to get the machine
acclimatized to its environment to ensure optimum performance. It also helps
the machine to easily adapt to any changes in the environment without
affecting its performance.
There is another problem if someone has to hardcode an extremely elaborate
process into the machine, and then it is likely that the programmer will miss a
couple of details. If there is any manual error, then it becomes quite tedious
to encode all the details all over again. In such instances, it is better to allow
the machine to learn the process instead.
The world of technology is in a constant flux of change and changes take
place in the languages used as well. It isn’t practical to keep redesigning the
systems all over again to accommodate all the possible changes. Instead,
Machine Learning helps the machine to automatically get acclimatized to all
the changes.
Applications of Machine Learning
Machine Learning is drastically changing the way businesses are operated
these days. It helps operate a large scale of data that’s available and enables
the users to draw helpful predictions based on the given information.
Certain manual tasks cannot be completed within a short time frame when
large amounts of data are involved. Machine Learning is the answer to such
problems. In the present times, we are overwrought with data and information
and there is no physical way in which anyone can process all this
information. Therefore, there is a dire need for an automated process and
Machine Learning helps attain this objective.
When the processes of analysis and discovery are fully automated, it becomes
simpler to attain useful information. This helps make all the future processes
fully automated. The words Big Data, Business Analytics, and Data Science
require Machine Learning. Predictive analytics and business intelligence are
no longer restricted to just the elite businesses and are now accessible to
small businesses and companies too. This allows small businesses to be a part
of the process of collection and effective utilization of information. Let us
look at a couple of technical applications of Machine Learning and see how
these apply to problems in the real world.
Virtual Personal Assistants
Popular examples of virtual assistants available today are Alexa, Siri, and
Google Now. As is obvious from the name, they help the user find the
necessary information via voice commands. You simply need to activate it
and then ask the question you want like, “What is my schedule for the day?”
“What are the flights available between London and Germany?” or any other
question that you want.
To answer your question, your personal assistant will look for information,
recall the question you asked and then give you an answer. It can also be used
to set reminders for certain tasks. Machine Learning is an important part of
the process since it enables the system to gather and refine the information
you need based on any of your previous involvements with it.
Density Estimation
Machine Learning allows the system to use the data that’s available to it to
suggest similar products. For instance, if you were to pick up a copy of Pride
and Prejudice from a bookstore and then run it through a machine, then
Machine Learning will help it determine the density of the words and come
up with other books that are similar to Pride and Prejudice.
Latent Variables
When you are working with latent variables, the machine will use clustering
to determine whether any of the variables present in it are related to one
another or not. This comes in handy when you aren’t certain of the reason
that caused the change in variables and aren’t aware of the relationship
between the variables. When a large quantity of data is involved, it is easier
to look for latent variables because it helps with a better understanding of the
data thus obtained.
Reduction of Dimensionality
Usually, the data that is obtained tends to have some variables and
dimensions. If there are more than three dimensions involved, then the human
mind can’t visualize that data. In such situations, Machine Learning helps to
reduce these data into manageable proportions so that the user can easily
understand the relationship between any variables.
Models of Machine Learning train the machines to learn from all the
available data and offer different services like prediction or classification that
in turn have multiple real-life applications like self-driving cars, the ability of
smartphones to recognize the user’s face or how Google Home or Alexa can
recognize your accent and voice and how the accuracy of the machines
improves if they have been learning for longer.
Advantages and Disadvantages of Machine Learning
Disadvantages
In Machine Learning, we always train the model and then validate that model
on a small data set. We then use that model to predict the output for some
unseen or new data. You will find it difficult to identify if there was a bias in
the model that you have created. If you cannot identify the bias, your
inferences will be incorrect.
Some social scientists will begin to rely only on Machine Learning. It is
important to remember that improvements should be made to some
unsupervised Machine Learning tasks.
Some of the advantages
Human beings cannot process large volumes of data, let alone analyze that
data. There is a lot of real-time data that is being produced, and if there is no
automatic system to understand and analyze that data, we cannot reach any
conclusion.
Machine Learning is getting better. With the advent of deep learning systems,
the costs of data engineering and pre-processing of data are reducing.
Machine Learning – Concepts & Terms
Machine Learning is done by feeding the machine with relevant training data
sets. Ordinary systems, that is, systems without any artificial intelligence, can
always provide an output based on the input that is provided to the system. A
system with artificial intelligence, however, can learn, predict, and improve
the results it provides through training.
Let us look at a simple example of how children learn to identify objects, or
in other words, how a child will associate a word with an object. Let us
assume that there is a bowl of apples and oranges on the table. You, as an
adult or parent, will introduce the round and red object as an apple, and the
other object as an orange. In this example, the words apple and orange are
labels, and the shapes and colors are attributes. You can also train a machine
using a set of labels and attributes. The machine will learn to identify the
object based on the attributes that are provided to it as input.
The models that are based on labeled training data sets are termed as
supervised Machine Learning models. When children go to school, their
teachers and professors give them some feedback about their progress. In the
same way, a supervised Machine Learning model allows the engineer to
provide some feedback to the machine.
Let us take an example of an input [red, round]. Here, both the child and the
machine will understand that any object which is round and red is an apple.
Let us now place a cricket ball in front of either the machine or the child. You
can feed the machine with the response negative 1 or 0 depending on whether
the prediction is wrong or right. You can always add more attributes if
necessary. This is the only way that a machine will learn. It is also for this
reason that if you use a large-high-quality data set and spend more time
training the machine, the machine will give you better and more accurate
results.
Before we proceed further, you must understand the difference between the
concepts of Machine Learning, artificial intelligence, and deep learning. Most
people use these concepts interchangeably, but it is important to know that
they are not the same.
Machine Learning, Artificial Intelligence and Deep Learning:
The diagram below will give you an idea of how these terms relate.
An illustration to understand the relationship between Machine Learning,
Artificial Intelligence, and Deep Learning.
Supervised Learning
In this model, the engineers feed the machine with labeled data. In other
words, the engineer will determine what the output of the system or specific
data sets should be. This type of algorithm is also called a predictive
algorithm.
For example, consider the following table:
Currency (label) Weight (Feature)
1 USD 10 gm
1 EUR 5 gm
1 INR 3 gm
1 RU 7 gm
In the above table, each currency is given an attribute of weight. Here, the
currency is the label, and the weight is the attribute or feature.
The supervised Machine Learning system with first we fed with this training
data set, and when it comes across any input of 3 grams, it will predict that
the coin is a 1 INR coin. The same can be said for a 10-gram coin.
Classification and regression algorithms are a type of supervised Machine
Learning algorithms. Regression algorithms are used to predict match scores
or house prices, while classification algorithms identify which category the
data should belong to.
We will discuss some of these algorithms in detail in the later parts of the
book, where you will also learn how to build or implement these algorithms
using Python.
Unsupervised Learning
In this type of model, the system is more sophisticated in the sense that it will
learn to identify patterns in unlabeled data and produce an output. This is a
kind of algorithm that is used to draw any meaningful inference from large
data sets. This model is also called the descriptive model since it uses data
and summarizes that data to generate a description of the data sets. This
model is often used in data mining applications that involve large volumes of
unstructured input data.
For instance, if a system is Python input of name, runs and wickets, the
system will visualize that data on a graph and identify the clusters. There will
be two clusters generated - one cluster is for the batsman while the other is
for the bowlers. When any new input is fed, the person will certainly fall into
one of these clusters, which will help the machine predict whether the player
is a batsman or a bowler.
Name Runs Wickets
Rachel 100 3
John 10 50
Paul 60 10
Sam 250 6
Alex 90 60
Sample data set for a match. Based on this, the cluster model can group the
players into batsmen or bowlers.
Some common algorithms which fall under unsupervised Machine Learning
include density estimation, clustering, data reduction and compressing.
The clustering algorithm summarizes the data and presents it differently. This
is a technique used in data mining applications. Density estimation is used
when the objective is to visualize any large data set and create a meaningful
summary. This will bring us the concept of data reduction and
dimensionality. These concepts explain that the analysis or output should
always deliver the summary of the data set without the loss of any valuable
information. In simple words, these concepts say that the complexity of data
can be reduced if the derived output is useful.
Reinforced learning
This type of learning is similar to how human beings learn, in the sense that
the system will learn to behave in a specific environment, and take actions
based on that environment. For example, human beings do not touch fire
because they know it will hurt and they have been told that will hurt.
Sometimes, out of curiosity, we may put a finger into the fire, and learn that
it will burn. This means that we will be careful with fire in the future.
The table below will summarize and give an overview of the differences
between supervised and unsupervised Machine Learning. This will also list
the popular algorithms that are used in each of these models.
Supervised Learning Unsupervised Learning
Works with labeled data Works with unlabeled data
Takes Direct feedback No feedback loop
Predicts output based on input Finds the hidden
data. Therefore also called structure/pattern from input data.
“Predictive Algorithm” Sometimes called as
“Descriptive Model”
Some common classes of Some common classes of
supervised algorithms include: unsupervised algorithms include:
Logistic Regression Clustering, Compressing, density
Linear Regression (Numeric estimation & data reduction
prediction) K-means Clustering (Clustering)
Polynomial Regression Association Rules (Pattern
Regression trees (Numeric Detection)
prediction) Singular Value Decomposition
Gradient Descent Fuzzy Means
Random Forest Partial Least Squares
Decision Trees (classification) Hierarchical Clustering
K-Nearest Algorithm Principal Component Analysis
(classification)
Naive Bayes
Support Vector Machines
We will look at each of these algorithms briefly and learn how to implement
them in Python. Let us now look at some examples of where Machine
Learning is applied. It is always a good idea to identify which type of
Machine Learning model you must use with examples. The following points
are explained in the next section:
Facebook face recognition algorithm
Netflix or YouTube recommending programs based on past
viewership history
Analyzing large volumes of bank transactions to guess if they are
valid or fraudulent transactions.
Uber’s surge pricing algorithm
Before installing this library, users need to have SciPy and NumPy installed.
If they already have a data set, they need to split it into training data, testing
data, and validation data. However, in this example, they are creating their
own training set, which will contain both the input and desired output values
of the data set they want to use to train their model. To load an external
dataset, they can use the Panda library, which will allow them to easily load
and manipulate datasets.
Their input data will consist of random integer values, which will generate a
random integer N; for instance, a <= N <= b. As such, they will create a
function that will determine the output. Recall a function uses some input
value to return some output value. Having created their training set, they will
split each row into an input training set and its related output training set,
resulting in two lists of all inputs and their corresponding outputs.
Benefits of splitting datasets include:
1. Gaining the ability to train and test the model on different types of
data than the data used for training
2. Testing the model's accuracy, which is better than testing the
accuracy of out-of-sample training
3. Ability to evaluate predictions using response values for the test
datasets
They will then use the linear regression method from Python's sci-kit-learn
library to create and train their model, which will try to imitate the function
they created for the ML training dataset. At this point, they will need to
determine whether their model can imitate the programmed function and
generate the correct answer or accurate prediction.
Here, the ML model analyzes the training data and uses it to calculate the
coefficients or weights to assign to the inputs to return the right outputs. By
providing it with the right test data, the model will arrive at the correct
answer.
Conditional or Decision Statements
In programming, we normally set certain conditions and decide which
particular action to perform depending on the conditions. To do this, Python
uses the “if statement” to check the program current state before responding
suitably to that state. However, in this chapter, you will be exposed to various
ways to write conditional statements. Furthermore, you will learn basic “if
statements,” create complex if statements and write loops to handle items in a
list. There is so much more loaded in this chapter for you to learn. Without
further ado, let us begin with a simple example.
The program below shows how you can use “if statement” to respond to a
particular situation correctly. For instance, we have a list of colors and want
to generate an output of different colors. Furthermore, the first letter should
be in the title case of the lower case.
colors =[“Green”, “Blue”, “Red”, “Yellow”]
for color in colors:
print(color.title())
The output will be as follows:
Green
Blue
Red
Yellow
Consider another example where we want to print a list of cars. We have to
print them in the title case since it is a proper name.
Additionally, the value “Kia” must be in uppercase.
cars = [“Toyota,” “Kia,” “Audi,” “Infinity”]
for car1 in cars:
if car1 == “kia”:
print(car1.upper())
else:
print(car1.title())
The loop first verifies if whether the current value of the car is “Kia.” If that
is true, it then prints the value in uppercase. However, if it is not kia, it prints
it in title case. The output will look like this:
Toyota
KIA
Audi
Infinity
The example above combines different concepts, which at the end of this
chapter, you will learn. However, let us begin with the various conditional
tests.
Conditional Tests in Python
In the center of any if statement lies an expression, which must be evaluated
to be either true or false. This is what is normally known as a conditional test
because Python uses both values to determine if a particular code should be
executed. If the particular statement is true, Python executes the code that
follows it. However, if it is false, it ignores the code after it.
Checking Equality
At times, we may test for the equality of a particular condition. In this
situation, we test if the value of the variable is equal to the other variable we
decide. For instance:
>>>color = “green”
>>> color == “green”
True
In this example, we first assign the variable color with the value “green by
using the single equal sign. This is not something new, as we have been using
it throughout this book. However, the second line checks if the value of color
is green, which has a double equal sign. It will return true if the value on the
left side and that on the right side are both true. If it doesn’t match, then the
result will be false. When the value of the color is anything besides green,
then this condition equates to false. The example below will clarify that.
>>>color = “green”
>>> color == “blue”
False
Note: When you test for equality, you should know that it is case sensitive.
For instance, two values that have different capitalizations won’t be regarded
as equal. For instance,
>>>color = “Green”
>>> color == “green”
False
If the case is important, then this is advantageous. However, if the case of the
variable isn’t important, and you want to check the values, then you can
convert the value of the variable to lowercase before checking for equality.
>>>color = “Green”
>>> color.lower() == “green”
True
This code will return True irrespective of how to format the value “Green” is
because the conditional tests aren’t case sensitive. Please note that the lower()
function we used in the program does not change the value originally stored
in color.
In the same way, we can check for equality; we can also check for inequality
in a program code. In checking for inequality, we verify if two values are not
equal and then return it as true. To check for inequality, Python has its unique
symbol, which is a combination of the exclamation sign with an equal sign
(!=). Most programming language uses these signs to represent inequality.
The example below shows the use of if statement to test for inequality:
color = “green”
if color != “blue”
print(“The color doesn’t match”)
In the second line, the interpreter matches the value of color to that of “blue.”
If the values match, then Python return false; however, if it is true, Python
returns true before executing the statement following it “The color doesn’t
match”
The color doesn’t match
Numerical Comparison in Python
We can also test numerical values in Python, but it is very straightforward.
For instance, the code below determines if a person’s age is 25 years old:
>>>myage = 25
>>>myage == 25
True
Additionally, we can also test if two numbers are unequal. Consider the code
below.
number = 34
if number != 54:
print(“The number does not match. Please retry!”)
The first line declares number as a variable and stores the number “34” in it.
The conditional statement begins in line two and passes through the line
because the number 34 is not equal to 54. Since the code is indented, the code
is then executed to produce
The number does not match. Please retry!
Besides this, you can perform various mathematical comparison inside your
conditional expressions including greater than, greater than or equal to , less
than, and less than or equal to.
>>> number = 22
>>> number <25
True
>>> number <= 25
True
>>> number > 25
False
>>> number >= 25
False
Every mathematical comparison you want can be included as part of an “if
statement” that allows you to detect the particular condition in question.
Creating Multiple Conditions
When writing code, some situations may warrant you to verify multiple
conditions simultaneously. For instance, you require conditions to be false to
take action. At times, you may want only one condition to be satisfied. In this
situation, you can use the keyword “or” and “and.” Let first use the “and”
keyword to check multiple conditions in Python programming.
Using “AND”
If you want to verify that two expressions are both true at the same time, the
keyword “and” serves that purpose. The expression is evaluated to be true
when both conditions test to return true. However, if one of the condition
falls, then the expression returns false.
For instance, you want to ascertain if two students in a class have over 45
score marks.
>>> score_1 = 46
>>> score_2 = 30
>>> score_1 >=45 and score_2 >= 45
False
>>> score_2 = 47
>>> score_1 >= 45 and score_2 >= 45
True
The program looks complicated but lets me explain it step-by-step. In the first
two lines, we define two scores, score_1, and score_2. However, in line 3, we
perform a check to ascertain if both scores are equal to or above 45. The
condition on the right-hand side is false, but that of the left-hand side is true.
Then in the line after the false statement, I changed the value of score_2 from
30 to 47. In this instant, the value of score_2 is now greater than 46;
therefore, both conditions will evaluate to true.
To make the code more readable, we can use parentheses in each test.
However, it is not compulsory to do such but makes it simpler. Let us use
parentheses to demonstrate the difference between the previous code and the
one below.
(score_1 >= 45) and (score_2 >=45)
Using “OR”
The “OR” keyword allows you to check multiple conditions as the “AND”
keyword. However, the difference here is that the “OR” keyword is used
when you want to ascertain that one expression is true for multiple
conditions. In this situation, if one of the expression is false, the condition
returns true. It returns false when both conditions are false.
Let us consider our previous example using the “OR” keyword. For instance,
you want to ascertain if two students in a class have over 45 score mark.
>>> score_1 = 46
>>> score_2 = 30
>>> score_1 >=45 or score_2 >= 45
True
>>> score_1 = 30
>>> score_1 >= 45 or score_2 >= 45
False
We began by declaring two variables score_1 and score_2 and assign values
to them. In the third line, we test the OR condition using the two variables.
The test in that line satisfies the condition because one of the expressions is
true. Then, it changed the value of the variable score to 30; however, it fails
both conditions and therefore evaluates false.
Besides using the “And” and “OR” conditional statements to check multiple
conditions, we can also test the availability of a value in a particular list. For
instance, you want to verify if a requested username is already in existence
from a list of usernames before the completion of online registration on a
website.
To do this, we can use the “in” keyword in such a situation. For instance, let
us use a list of animals in the zoo and check if it is already on the list.
>>>animals = [“zebra”, “lion”, “crocodile”, “monkey”]
>>> “monkey” in animals
True
>>> “rat” in animals
False
In the second and fourth lines, we use the “in” keyword to test if the request
word in a double quote exists in our list of animals. The first test ascertains
that “monkey” exists in our list, whereas the second test returns false because
the rat is not in the animal's list. This method is significant because we can
generate lists of important values and check the existence of the values in the
list.
There are situations where you want to check if a value isn’t in a list. In such
a case, instead of using the “in” keyword to return false, we can use the “not”
keyword. For instance, let us consider a list of Manchester United players
before allowing them to be part of their next match. In order words, we want
to scan the real players and ensure that the club does not field an illegible
player.
united_player = [“Rashford,” “Young,” “Pogba,” “Mata,” “De Gea”]
player = “Messi”
if player not in united_player:
print(f “{player.title()}, you are not qualified to play for Manchester
United.”)
The line “if player, not in united_player:” reads quite clearly. Peradventure,
the value of the player isn’t in the list united_player, Python returns the
expression to be True and then executed the line indented under it. The player
“Messi” isn’t part of the list united_player; therefore, he will receive a
message about his qualification status. The output will be as follow:
Messi, you are not qualified to play for Manchester United.
Boolean Expressions in Python
If you have learned any programming language, you might have come across
the term “Boolean Expression” because they are very important. A Boolean
expression is another term to describe the conditional test. When evaluated,
the outcome can only be either True or False. However, they are essential if
your goal is to keep track of specific conditions like if a user can change
content or light is switched on or not. For instance,
change_content = False
light_on = False
light_off = True
Boolean values provide the best means of tracking the particular condition of
a program.
Exercises
Conditional Testing – Write various conditional expressions. Furthermore,
print a statement to describe each condition and what the likely output of
each test will be. for instance, your code can be like this:
car = “Toyota”
print(“Is car == ‘Toyota’? My prediction is True.”(
print (car == “Toyota”)
print(“\nIs car == ‘KIA’? My prediction is False.”)
print(car== “KIA”)
1. Test the following condition to evaluate either True or False using
any things of your choice to form a list.
2. Test for both inequality and equality using strings and numbers
3. Test for the condition using the “or” and “and” keywords
4. Test if an item exists in the above list
5. Test if an item doesn’t exist in the list
If Statements
Since you now know conditional tests, it will be easier for you to under if
statements. There are various types of if statements to use in Python,
depending on your choice. In this section, you will learn the different if
statements possible and the best situation to apply them, respectively.
Simple If Statements
In any programming language, the “if statement” is the simplest to come
across. It only requires a test or condition with a single action following it,
respectively. The syntax for this statement is as follows:
if condition:
perform action
The first line can contain any conditional statement with the second following
the action to take. Ensure to indent the second line for clarity purposes. If the
conditional statement is true, then the code under the condition is executed.
However, if it is false, the code is ignored.
For instance, we have set a standard that the minimum score for a person to
qualify for a football match is 20. We want to test if such a person is qualified
to participate.
person = 21
if person >= 20
print(“You are qualified for the football match against Valencia.”)
In the first line, we define the person’s age to 21 to qualify. Then the second
line evaluates if the person is greater than or equal to 20. Python then
executes the statement below because it fulfills the condition that the person
is above 20.
You are qualified for the football match against Valencia.
Indentation is very significant when using the “if statement” like we did in
the “for loop” situations. All indented lines are executed once the condition is
satisfied after the if statement. However, if the statement returns false, then
the whole code under it is ignored, and the program halted.
We can also include more code inside the if statements to display what we
want. Let us add another line to display that the match is between Chelsea
and Valencia at the Standford Bridge.
person =21
if person >= 20
print(“You are qualified for the football match against Valencia.”)
print(“The match is between Arsenal and Valencia.”)
Print(“The Venue is at the Emirate Stadium in England.”)
The conditional statement passes through the condition and prints the
indented actions once the condition is satisfied.
The output will be as follow:
You are qualified for the football match against Valencia.
The match is between Arsenal and Valencia.
The Venue is at the Emirate Stadium in England.
Assuming the age is less than 20, and then there won’t be any output for this
program. Let us try another example before going into another conditional
statement.
name = “Abraham Lincoln”
if name = “Abraham Lincoln”
print(“Abraham Lincoln was a great United State President.”)
print(“He is an icon that many presidents try to emulate in the world.”)
The output will be:
Abraham Lincoln was a great United State President.
He is an icon that many presidents try to emulate in the world.
If-else Statements
At times, you may want to take certain actions if a particular condition isn’t
met. For example, you may decide what will happen if a person isn’t
qualified to play a match. Python provides the if-else statements to make this
possible. The syntax is as follows:
if conditional test
perform statement_1
else
perform statement_2
Let us use our football match qualification to illustrate how to use the if-else
statement.
person =18
if person >= 20:
print(“You are qualified for the football match against Valencia.”)
print(“The match is between Arsenal and Valencia.”)
print(“The Venue is at the Emirate Stadium in England.”)
else:
print(“Unfortunately, you are not qualified to participate in the match.”)
print(“Sorry, you have to wait until you are qualified.”)
The conditional test (if person>=20) is first evaluated to ascertain that the
person is above 20 before it passes to the first indented line of code. If it is
true, then it prints the statements beneath the condition. However, in our
example, the conditional test will evaluate to false then passes control to the
else section. Finally, it prints the statement below it since it fulfills that part
of the condition.
Unfortunately, you are not qualified to participate in the match.
Sorry, you have to wait until you are qualified.
This program works because of the two possible scenarios to evaluate – a
person must be qualified to play or not play. In this situation, the if-else
statement works perfectly when you want Python to execute one action in
two possible situations.
Let us try another.
station_numbers = 10
if station_numbers >=12:
print(“We need additional 3 stations in this company.”)
else:
print(“We need additional 5 stations to meet the demands of our
audience.”)
The output will be:
We need an additional 5 stations to meet the demands of our audience.
The if-elif-else Chain
At times, you may want to test three different conditions based on certain
criteria. In such a situation, Python allows us to use the if-elif-else
conditional statement to execute such a task. We have many real-life
situations, require more than two possibilities. For instance, think of a cinema
hall with different charge rates for different sets of people.
- Children under 5 years are free
- Children between 5 years and 17 years are $30
- People older than 18 years is $50
As you can see, there are three possible situations because the following set
of people can attend the cinema to watch the movie of their choice. In this
situation, how can you ascertain a person’s rate? Well, the following code
will illustrate that point and print out specific price rates for each category of
people.
person_age = 13
if person_age < 5:
print(“Your ticket cost is $0.”)
elif person_age < 17:
print(“Your ticket cost is $30.”)
else:
print(“Your ticket cost is $50)
The first line declares a variable “person_age” with value 13. Then we
perform the first conditional statement to test if the person is below the age of
5. If it fulfills the condition, it prints the appropriate message, and the
program halts. However, if it returns false, it passes to the elif line, which
tests if the person_age is less than 17. At this post, the person’s minimum age
must be 5 years and not above 17. If the person is above 17, then Python
skips the instruction and goes to the next condition.
In the example, we fix the person_age to 13. Therefore, the first test will
evaluate false and won’t execute the block of line. It then tests the elif
condition, which in this case is true, and will print the message. The output
will be:
Your ticket cost is $30.”)
Nevertheless, if the age is above 17, then it will pass through the first two
tests because it will evaluate to false. Then the next command will be the else
condition, which will print the statement below.
We can rewrite this program in such a way that we won’t have to include the
message “Your ticket cost is…” all we need is to put the prince inside the if-
elif-else chain with a simple print() method to execute after the evaluation of
the chain. Look at the line of code below:
person_age = 13
if person_age < 5:
cost = 0
elif person_age < 17:
cost =30
else:
cost = 50
print(f “Your ticket cost is ${cost}.”)
In the third, fifth, and seventh lines, we defined the cost based on the person’s
age. The cost price is already set within the if-elif-else statement. However,
the last line uses the cost of each age to form the final cost of the ticket.
This new code will produce the same result as the previous example.
However, the latter is more concise and straightforward. Instead of using
three different print statements, our reverse code only uses a single print
statement to print the cost of the ticket.
Multiple elif Blocks
You can also have more than one elif block in your program. For instance, if
the manager of the cinema decides to implement special discounts for
workers, this will require additional, conditional tests to the program to
ascertain whether the person in question is qualified for such a discount.
Assuming those above 55 years will pay 70% of the initial cost of each ticket.
Then the program code will be as follows:
person_age = 13
if person_age < 5:
cost = 0
elif person_age < 17:
cost =30
elif person_age < 55
cost = 50
else:
cost = 35
print(f “Your ticket cost is ${cost}.”)
The cost is identical to our previous example; however, the only including is
the “elif person_age < 55” and is respective else condition. This second elif
block checks if the person’s age is less than 55 before assigning them the cost
of the ticket for $50. However, the statement after the else needs to be
changed. In this situation, it is applicable if the person’s age is above 55
years, which is this situation fulfills the condition we want.
The “else” statement isn’t compulsory because you can omit it and use the
elif statement instead. At times, it is better to use additional elif statements to
capture specific interests. Let us see how to implement it without using the
else statement.
person_age = 13
if person_age < 5:
cost = 0
elif person_age < 17:
cost =30
elif person_age < 55:
cost = 50
elif person_age >= 55:
cost = 35
print(f “Your ticket cost is ${cost}.”)
The additional elif statement helps to assign the ticket cost of “$30” to those
above 30 years. This format is a bit clearer when compared with the else
block.
Performing Multiple Conditions
Using the if-elif-else statement comes handy when especially when you want
to pass only one test. Once the interpreter discovers that this test is passed, it
skips other tests and halts the program. With this feature, you test a specific
condition in a line of code.
Nevertheless, some situations may warrant you to check all the conditions
available. In such a scenario, you can use multiple if statements without
adding the elif or else blocks. This method becomes relevant when more than
one of the condition returns true. For instance, let us consider the previous
example of players in Manchester United to illustrate this. In this, we want to
include the players in an upcoming match against their rivals Manchester
City.
united_players = [“Rashford,” “Young,” “Pogba,” “Mata,” “De Gea”]
if “Young” in united_players:
print(“Adding Young to the team list.”)
if “De Gea” in united_players:
print(“Adding Dea Gea to the team list.”)
if “Messi” in united_players:
print(“Adding Messi to the team list.”)
print( “Team list completed for the match against Manchester City!”)
In the first line, we defined united_players as a variable with values
Rashford, Young, Pogba, Mata, and De Gea. The second line uses the “if
statement” to check if the person requested for Young. The same applies to
the lines with the “if statement” and the condition is run regardless of the
outcome of the previous tests. For this program above, the output will be:
Adding Young to the team list.
Adding Dea Gea to the team list.
Team list completed for the match against Manchester City!
If we decide to use the if-elif-else block, the code won’t function properly
because once a particular test returns true, the program will stop.
Let us try it and see.
united_players = [“Rashford,” “Young,” “Pogba,” “Mata,” “De Gea”]
if “Young” in united_players:
print(“Adding Young to the team list.”)
elif “De Gea” in united_players:
print(“Adding Dea Gea to the team list.”)
elif “Messi” in united_players:
print(“Adding Messi to the team list.”)
print( “\ Team list completed for the match against Manchester City!”)
In this code, Python will evaluate the first condition, and once it is true, the
program stops. The output for this program will be:
Adding Young to the team list.
Team list completed for the match against Manchester City!
Exercise
Consider the list of colors we have in the world. Create a variable name-color
and assign the following colors to it – blue, red, black, orange, white, yellow,
indigo, green.
Use an “if statement” to check if the color is blue. If the color is blue, then
print a message indicating a score of 5 points.
Write a problem using the if-else chain to print if a particular selected is
green.
Write another program using the if-elif-else chain to determine the scores of
students in a class. Set a variable “score” to store the student’s score.
If the student’s score is below 40, indicate an output a message that such
student has failed.
If the student’s score is above 41 but less than 55, print a message that the
student has passed.
Essential Libraries for Machine Learning
in Python
Many developers nowadays prefer the usage of Python in their data analysis.
Python is not only applied in data analysis but also statistical techniques.
Scientists, especially the ones dealing with data, also prefer using Python in
data integration. That's the integration of Webb apps and other environment
productions.
The features of Python have helped scientists to use it in Machine Learning.
Examples of these qualities include consistent syntax, being flexible and even
having a shorter time in development. It also can develop sophisticated
models and has engines that could help in predictions.
As a result, Python boasts of having a series or a set of very extensive
libraries. Remember, libraries refer to a series of routines and sorts of
functions with different languages. Therefore, a robust library can lead to
tackling more complex tasks. However, this is possible without writing
several code lines again. It is good to note that Machine Learning relies
majorly on mathematics. That's mathematical optimization, elements of
probability and also statistical data. Therefore, Python comes in with a rich
knowledge of performing complex tasks without much involvement.
The following are examples of essential libraries being used in our present.
Scikit – Learn
Scikit learn is one of the best and a trendy library in Machine Learning. It has
the ability to supporting learning algorithms, especially unsupervised and
supervised ones.
Examples of Scikit learn include the following.
k-means
decision trees
linear and logistic regression
clustering
This kind of library has major components from NumPy and SciPy. Scikit
learn has the power to add algorithms sets that are useful in Machine
Learning and also tasks related to data mining. That's, it helps in
classification, clustering, and even regression analysis. There are also other
tasks that this library can efficiently deliver. A good example includes
ensemble methods, feature selection, and more so, data transformation. It is
good to understand that the pioneers or experts can easily apply this if at all,
they can be able to implement the complex and sophisticated parts of the
algorithms.
TensorFlow
It is a form of algorithm which involves deep learning. They are not always
necessary, but one good thing about them is their ability to give out correct
results when done right. It will also enable you to run your data in a CPU or
GPU. That's, you can write data in the Python program, compile it, then run it
on your central processing unit. Therefore, this gives you an easy time in
performing your analysis. Again, there is no need for having these pieces of
information written at C++ or instead of other levels such as CUDA.
TensorFlow uses nodes, especially the multi-layered ones. The nodes perform
several tasks within the system, which include employing networks such as
artificial neutral, training, and even set up a high volume of datasets. Several
search engines such as Google depend on this type of library. One main
application of this is the identification of objects. Again, it helps in different
Apps that deal with the voice recognition.
Theano
Theano too forms a significant part of Python library. Its vital tasks here are
to help with anything related to numerical computation. We can also relate it
to NumPy. It plays other roles such as;
Definition of mathematical expressions
Assists in the optimization of mathematical calculation
Promotes the evaluation of expressions related to numerical
analysis.
Diagrammatic explanation
Series Dimensional
A7
B8
C9
D3
E6
F 9
Matplotlib
This is another sophisticated and helpful data analysis technique that helps in
data visualization. Its main objective is to advise the industry where it stands
using the various inputs. You will realize that your production goals are
meaningless when you fail to share them with different stakeholders. To
perform this, Matplotlib comes in handy with the types of computation
analysis required. Therefore, it is the only Python library that every scientist,
especially the ones dealing with data prefers. This type of library has good
looks when it comes to graphics and images. More so, many prefer using it in
creating various graphs for data analysis. However, the technological world
has completely changed with new advanced libraries flooding the industry.
It is also flexible, and due to this, you are capable of making several graphs
that you may need. It only requires a few commands to perform this.
In this Python library, you can create various diverse graphs, charts of all
kinds, several histograms, and even scatterplots. You can also make non-
Cartesian charts too using the same principle.
Diagrammatic explanation
The above graph highlights the overall production of a company within three
years. It specifically demonstrates the usage of Matplotlib in data analysis.
By looking at the diagram, you will realize that the production was high as
compared to the other two years. Again, the company tends to perform in the
production of fruits since it was leading in both years 1 and 2 with a tie in
year 3. From the figure, you realize that your work of presentation,
representation and even analysis has been made easier as a result of using this
library. This Python library will eventually enable you to come up with good
graphics images, accurate data and much more. With the help of this Python
library, you will be able to note down the year your production was high,
thus, being in a position to maintain the high productivity season.
It is good to note that this library can export graphics and can change these
graphics into PDF, GIF, and so on. In summary, the following tasks can be
undertaken with much ease. They include:
Formation of line plots
Scattering of plots
Creations of beautiful bar charts and building up of histograms
Application of various pie charts within the industry
Stemming the schemes for data analysis and computations
Being able to follow up contours plots
Usage of spectrograms
Quiver plots creation
Seaborn
Seaborn is also among the popular libraries within the Python category. Its
main objective here is to help in visualization. It is important to note that this
library borrows its foundation from Matplotlib. Due to its higher level, it is
capable of various plots generation such as the production of heat maps,
processing of violin plots and also helping in generation of time series plots.
Diagrammatic illustration
The above line graph clearly shows the performance of different machines
the company is using. Following the diagram above, you can eventually
deduce and make a conclusion on which machines the company can keep
using to get the maximum yield. On most occasions, this evaluation method
by the help of the Seaborn library will enable you to predict the exact abilities
of your different inputs. Again, this information can help for future reference
in the case of purchasing more machines. Seaborn library also has the power
to detect the performance of other variable inputs within the company. For
example, the number of workers within the company can be easily identified
with their corresponding working rate.
NumPy
This is a very widely used Python library. Its features enable it to perform
multidimensional array processing. Also, it helps in the matrix processing.
However, these are only possible with the help of an extensive collection of
mathematical functions. It is important to note that this Python library is
highly useful in solving the most significant computations within the
scientific sector. Again, NumPy is also applicable in areas such as linear
algebra, derivation of random number abilities used within industries and
more so Fourier transformation. NumPy is also used by other high-end
Python libraries such as TensorFlow for Tensors manipulation. In short,
NumPy is mainly for calculations and data storage. You can also export or
load data to Python since it has those features that enable it to perform these
functions. It is also good to note that this Python library is also known as
numerical Python.
SciPy
This is among the most popular library used in our industries today. It boasts
of comprising of different modules that are applicable in the optimization
sector of data analysis. It also plays a significant role in integration, linear
algebra, and other forms of mathematical statistics.
In many cases, it plays a vital role in image manipulation. Manipulation of
the image is a process that is widely applicable in day to day activities. Cases
of Photoshops and much more are examples of SciPy. Again, many
organizations prefer SciPy in their image manipulation, especially the
pictures used for presentation. For instance, wildlife society can come up
with the description of a cat and then manipulate it using different colors to
suit their project. Below is an example that can help you understand this more
straightforwardly. The picture has been manipulated:
The original input image was a cat that the wildlife society took. After
manipulation and resizing the image according to our preferences, we get a
tinted image of a cat.
Keras
This is also part and parcel of Python library, especially within Machine
Learning. It belongs to the group of networks with high level neural. It is
significant to note that Keras has the capability of working over other
libraries, especially TensorFlow and even Theano. Also, it can operate
nonstop without mechanical failure. In addition to this, it seems to work
better on both the GPU and CPU. For most beginners in Python
programming, Keras offers a secure pathway towards their ultimate
understanding. They will be in a position to design the network and even to
build it. Its ability to prototype faster and more quickly makes it the best
Python library among the learners.
PyTorch
This is another accessible, but open-source kind of Python library. As a result
of its name, it boasts of having extensive choices when it comes to tools. It is
also applicable in areas where we have computer vision. Computer vision and
visual display, play an essential role in several types of research. Again, it
aids in the processing of Natural Language. More so, PyTorch can undertake
some technical tasks that are for developers. That's enormous calculations
and data analysis using computations. It can also help in graph creation which
mainly used for computational purposes. Since it is an open-source Python
library, it can work or perform tasks on other libraries such as Tensors. In
combination with Tensors GPU, its acceleration will increase.
Scrapy
Scrapy is another library used for creating crawling programs. That's spider
bots and much more. The spider bots frequently help in data retrieval
purposes and also applicable in the formation of URLs used on the web.
From the beginning, it was to assist in data scrapping. However, this has
undergone several evolutions and led to the expansions of its general
purpose. Therefore, the main task of the scrappy library in our present-day is
to act as crawlers for general use. The library led to the promotion of general
usage, application of universal codes, and so on.
Statsmodels
Statsmodels is a library with the aim of data exploration using several
methods of statistical computations and data assertions. It has many features
such as result statistics and even characteristic features. It can undertake this
role with the help of the various models such as linear regression, multiple
estimators, and analysis involving time series, and even using more linear
models. Also, other models, such as discrete choice are applicable here.
What is the TensorFlow Library
The next thing that we need to spend some time looking at is the TensorFlow
Library. This is another option that comes from Python, and it can help you
to get some Machine Learning done. This one takes on a few different
options of what you can do when it comes to Machine Learning, so it is
definitely worth your time to learn how to use this option along with the
algorithms that we talked about with the Scikit-Learn library.
TensorFlow is another framework that you can work within Python Machine
Learning, and it is going to offer the programmer a few different features and
tools to get your project done compared to the others. You will find that the
framework that comes with TensorFlow is going to come from Google, and it
is helpful when you are trying to work on some models that are deep learning
related. TensorFlow is going to rely on graphs of data flow for numerical
computation. And it can make sure that some of the different things that you
can do with Machine Learning are easier than ever before.
TensorFlow is going to help us out in many different ways. First, it can help
us with acquiring the data, training the models of Machine Learning that we
are trying to use, helps to make predictions, and can even modify a few of the
future results that we have to make them work more efficiently. Since each of
these steps is going to be important when it comes to doing some Machine
Learning, we can see how TensorFlow can come into our project and ensure
we reach that completion that we want even better.
First, let’s take a look at what TensorFlow is all about and some of the
background that comes with this Python library. The Brain team from Google
was the first to develop TensorFlow to use on large scale options of Machine
Learning. It was developed in order to bring together different algorithms for
both deep learning and Machine Learning, and it is going to make them more
useful through what is known as a common metaphor. TensorFlow works
along with the Python language that we talked about before. In addition to
this, it is going to provide the users with a front-end API that is easy to use
when working on a variety of building applications.
It makes it a bit further, though. Even though you can work with TensorFlow
and it matches up with the Python coding language while you do the coding
and the algorithms, it is going to be able to change these up. All of the
applications that you use with the help of TensorFlow are going to be
executed using the C++ language instead, giving them an even higher level of
performance than before.
TensorFlow can be used for a lot of different actions that you would need to
do to make a Machine Learning project a success. Some of the things that
you can do with this library, in particular, will include running, training, and
building up the deep neural networks, doing some image recognition,
working with recurrent neural networks, digit classification, natural language
processing, and even word embedding. And this is just a few of the things
that are available for a programmer to do when they work with TensorFlow
with Machine Learning.
Installing TensorFlow
With this in mind, we need to take some time to learn how to install
TensorFlow on a computer before we can use this library. Just like we did
with Scikit-Learn, we need to go through and set up the environment and
everything else so that this library is going to work. You will enjoy that with
this kind of library; it is already going to be set up with a few APIs for
programming (we will take a look at these in more depth later on), including
Rust, Go, C++ and Java to name a few. We are going to spend our time here
looking at the way that the TensorFlow library is going to work on the
Windows system, but the steps that you have to use to add this library to your
other operating systems are going to be pretty much the same.
Now, when you are ready to set up and download the TensorFlow library on
your Windows computer, you will be able to go through two choices on how
to download this particular library. You can choose either to work with the
Anaconda program to get it done, or a pip is going to work well, too. The
native pip is helpful because it takes all of the parts that go with the
TensorFlow library and will make sure that it is installed on your system.
And you get the bonus of the system doing this for you without needing to
have a virtual environment set up to get it done.
However, this one may seem like the best choice, but it can come with some
problems along the way. Installing the TensorFlow library using a pip can be
a bit faster and doesn’t require that virtual environment, but it can come with
some interference to the other things that you are doing with Python.
Depending on what you plan to do with Python, this can be a problem so
consider that before starting.
The good thing to remember here is that if you do choose to work with a pip
and it doesn’t seem like it is going to interfere with what you are doing too
much, you will be able to get the whole TensorFlow library to run with just
one single command. And once you are done with this command, the whole
library, and all of the parts that you need with it, are going to be set up and
ready to use on the computer with just one command. And the pip even
makes it easier for you to choose the directory that you would like to use to
store the TensorFlow library for easier use.
In addition to using the pip to help download and install the TensorFlow
library, it is also possible for you to use the Anaconda program. This one is
going to take a few more commands to get started, but it does prevent any
interference from happening with the Python program, and it allows you to
create a virtual environment that you can work with and test out without a ton
of interference or other issues with what is on your computer.
Though there are a few benefits to using the Anaconda program instead of a
pip, it is often recommended that you install this program right along with a
pip, rather than working with just the conda install. With this in mind, we will
still show you some of the steps that it takes to just use the conda install on its
own so you can do this if you choose.
One more thing that we need to consider here before moving on is that you
need to double-check which version of Python is working. Your version
needs to be at Python 3.5 or higher for this to work for you. Python 3 uses the
pip 3 program, and it is the best and most compatible when it comes to
working with a TensorFlow install. Working with an older version is not
going to work as well with this library and can cause some issues when you
try to do some of your Machine Learning code.
You can work with either the CPU or the GPU version of this library based
on what you are the most comfortable with. The first code below is the CPU
version and the second code below is going to be the GPU version.
pip 3 install – upgrade tensorflow
pip 3 install – upgrade tensorflow-gpu
Both of these commands are going to be helpful because they are going to
ensure that the TensorFlow library is going to be installed on your Windows
system. But another option that you can use is with the Anaconda package
itself. The methods above were still working with the pip installs, but we
talked about how there are a few drawbacks when it comes to this one.
Pip is the program that is already installed automatically when you install
Python onto your system as well. But you may find out quickly that
Anaconda is not. This means that if you want to ensure that you can get
TensorFlow to install with this, then you need to first install the Anaconda
program. To do this, just go to the website for Anaconda and then follow the
instructions that come up to help you get it done.
Once you have had the time to install the Anaconda program, then you will
notice that within the files there is going to be a package that is known as
conda. This is a good package to explore a bit at this time because it is going
to be the part that helps you manage the installation packages, and it is
helpful when it is time to manage the virtual environment. To help you get
the access that you need with this package, you can just start up Anaconda
and it will be there.
When Anaconda is open, you can go to the main screen on Windows, click
the Start button, and then choose All programs from here. You need to go
through and expand things out to look inside of Anaconda at the files that are
there. You can then click on the prompt that is there for Anaconda and then
get that to launch on your screen. If you wish to, it is possible to see the
details of this package by opening the command line and writing in “conda
info.” This allows you to see some more of the details that you need about the
package and the package manager.
The virtual environment that we talk about with the Anaconda program is
going to be pretty simple to use, and it is pretty much just an isolated copy of
Python. It will come with all of the capabilities that you need to maintain all
of the files that you use, along with the directories and the paths that go with
it too. This is going to be helpful because it allows you to do all of your
coding inside the Python program, and allows you to add in some different
libraries that are associated with Python if you choose.
These virtual environments may take a bit of time to adjust to and get used to,
but they are good for working on Machine Learning because they allow you
to isolate a project, and can help you to do some coding, without all of the
potential problems that come with dependencies and version requirements.
Everything you do in the virtual environment is going to be on its own, so
you can experiment and see what works and what doesn’t, without messing
up other parts of the code.
From here, our goal is to take the Anaconda program and get it to work on
creating the virtual environment that we want so that the package from
TensorFlow is going to work properly. The conda command is going to come
into play here again to make this happen. Since we are going through the
steps that are needed to create a brand new environment now, we will need to
name it tensorenviron, and then the rest of the syntax to help us get this new
environment created includes:
conda create -n tensorenvrion
After you type this code into the compiler, the program is going to stop and
ask you whether you want to create the new environment, or if you would
rather cancel the work that you are currently doing. This is where we are
going to type in the “y” key and then hit enter so that the environment is
created. The installation may take a few minutes as the compiler completes
the environment for you.
Once the new environment is created, you have to go through the process of
actually activating it. Without this activation in place, you will not have the
environment ready to go for you. You just need to use the command of
“activate” to start and then list out the name of any environment that you
want to work with to activate. Since we used the name of tensorenviron
earlier, you will want to use this in your code as well. An example of how
this is going to look includes:
Activate tensorenviron
Now that you have been able to activate the TensorFlow environment, it is
time to go ahead and make sure that the package for TensorFlow is going to
be installed too. You can do this by using the command below:
conda install tensorflow
When you get to this point, you will be presented with a list of all the
packages that are available to install in case you want to add in a few others
along with TensorFlow. You can then decide if you want to install one or
more of these packages, or if you want to just stick with TensorFlow for right
now. Make sure to agree that you want to do this and continue through the
process.
The installation of this library is going to get to work right away. But it is
going to be a process that takes some time, so just let it go without trying to
backspace or restart. The speed of your internet is going to make a big
determinant of whether you will see this take a long time or not.
Soon though, the installation process for this library is going to be all done,
and you can then go through and see if this installation process was
successful or if you need to fix some things. The good news is the checking
phase is going to be easy to work with because you can just use the import
statement of Python to set it up.
This statement that we are writing is then going to go through the regular
terminal that we have with Python. If you are still working here, like you
should, with the prompt from Anaconda, then you would be able to hit enter
after typing in the word Python. This will make sure that you are inside the
terminal that you need for Python so you can get started. Once you are in the
right terminal for this, type in the code below to help us get this done and
make sure that TensorFlow is imported and ready to go:
import tensorflow as tf
At this point, the program should be on your computer and ready to go and
we can move on to the rest of the guidebook and see some of the neat things
that you can do with this library. There may be a chance that the TensorFlow
package didn’t end up going through the way that it should. If this is true for
you, then the compiler is going to present you with an error message for you
to read through and you need to go back and make sure the code has been
written in the right format along the way.
The good news is if you finish doing this line of code above and you don’t
get an error message at all, then this means that you have set up the
TensorFlow package the right way and it is ready to use! With that said, we
need to explore some more options and algorithms that a programmer can do
when it comes to using the TensorFlow library and getting to learn how they
work with the different Machine Learning projects you want to implement.
Artificial Neural Networks
This chapter discusses the integral aspect of artificial neural networks. It also
covers their component in particular activation functions and how to train an
artificial neural network, as well as the different advantages of using an
artificial neural network.
Definition of artificial neural network
The employment of artificial neural networks is a widely used approach in
Machine Learning. It is inspired by the brain system of humans. The
objective of neural networks is to replicate how the human brain learns. The
neural network system is an ensemble of input and output layers and a hidden
layer that transforms the input layer into useful information to the output
layer. Usually, several hidden layers are implemented in an artificial neural
network. The figure below presents an example of a neural network system
composed of 2 hidden layers:
,
being the weights assigned to each input. The function F
can also be represented as:
Where W is a weight matrix and X a vector of data. The second formulation
is very convenient when programming a neural network model. The weights
are determined during the training procedure. Training an artificial neural
network means finding the optimal weights W that provide the most accurate
output.
To each neuron, an activation function is applied the resulted weighted sum
of inputs X. The role of the activation function is deciding whether the
neuron should be activated or not according to the model’s prediction. This
process is applied to each layer of the network. In the next sub-sections, we
will discuss in detail the role and types of activation functions as well as the
different types of neural networks.
What is an activation function and its role in neural network
models?
Activation functions are formulated as mathematical functions. These
functions are a crucial component of an artificial neural network model. For
each neuron, an activation function is associated. The activation function
decides whether to activate the neuron or not. For instance, let’s consider the
output from a neuron, which is:
The output Y can be of any value. The neuron does not have any information
on the reasonable range of values that Y can take. For this purpose, the
activation function is implemented in the neural network to check Y values
and make a decision on whether the neural connections should consider this
neuron activated or not.
There are different types of activation functions. The most instinctive
function is the step function. This function sets a threshold and decides to
activate or not activate a neuron if it exceeds a certain threshold. In other
words, the output of this function is 1 if Y is greater than a threshold and 0
otherwise. Formally, the activation function is:
The disadvantage of this activation function, as we can see from the figure
above, is the small change in the output for input values under -4 and above
4. This problem is called ‘vanishing gradient’ which means that the gradient
is very small on horizontal extremes of the curve. This makes a neural
network using the sigmoid function, learning very slowly when they
approach the edges and computationally expensive.
The tanh function
The tanh function is another activation function used that is similar to the
sigmoid function. The mathematical formulation of this function is:
This activation function is not bounded and takes values from 0 to +inf.
Although it has a similar shape as a linear function (i.e., this function is equal
to identity for positive values), the ReLu function has a derivative. The
drawback of the ReLu is that the derivative (i.e., the gradient) is 0 when the
inputs are negative. This means as for the linear functions, the
backpropagation cannot be processed, and the neural network cannot learn
unless the inputs are greater than 0. This aspect of the ReLu, gradient equal to
0 when the inputs are negative, is called the dying ReLu problem.
To prevent the dying ReLu problem, two ReLu variations can be used,
namely the Leaky ReLu function and the Parametric ReLu function. The
Leakey ReLu function returns as output the maximum of X and X by 0.1. In
other words, the leaky ReLu is equal to the identity function when X is
greater than 0 and is equal to the product of 0.1 and X when X is less than
zero. This function is provided as follows:
This function has a small positive gradient which is 0.1 when X has negative
values, which make this function support backpropagation for negative
values. However, it may not provide a consistent prediction for these negative
values.
The parametric ReLu function is similar to the Leaky ReLu function, which
takes the gradient as a parameter to the neural network to define the output
when X is negative. The mathematical formulation of this function is as
follows:
There are other variations of the ReLu function such as the exponential linear
ReLu. This function, unlike the other variations of the ReLu the Leaky ReLu
and parametric ReLu, has a log curve for negative values of X instead of the
linear curves like the Leaky ReLu and the parametric ReLu functions. The
downside of this function is it saturates for large negative values of X. Other
variations exist which all rely on the same concept of defining a gradient
greater than 0 when X has negative values.
The Softmax function
The Softmax function is another type of activation function used differently
than the one presented previously. This function is usually applied only to the
output layer when a classification of the inputs into several different classes is
needed. In fact, the Softmax function supports several classes and provides
the probability of input to belong to a specific class. It normalizes outputs of
every category between 0 and 1 then divides by their sum to provide that
probability.
Given all these activation functions, where each one has its pros and cons, the
question now is: which one should be used in a neural network? The answer
is: simply having a better understanding of the problem in hand will help
guide into a specific activation function, especially if the characteristics of the
function being approximated are known beforehand. For instance, a sigmoid
function is a good choice for a classification problem. In case the nature of
the function being approximated is unknown, it is highly recommended to
start with a ReLu function rather than trying other activation functions.
Overall, the ReLu function works well for a wide range of applications. It is
an ongoing research, and you may try your activation function.
An important aspect of choosing an activation function is the sparsity of the
activation. Sparsity means that not all neurons are activated. This is a desired
characteristic in a neural network because it makes the network learns faster
and less prone to overfitting. Let’s imagine a large neural network with
multiple neurons if all neurons were activated; it means all these neurons are
processed to describe the final output. This makes the neural network very
dense and computationally exhaustive to process. The sigmoid and the tanh
activation functions have this property of activating almost all neurons, which
makes them computationally inefficient unlike the ReLu function and its
variations that cause the inactivation of some negative values. That is the
reason why it is recommended to start with the ReLu function when
approximating a function with unknown characteristics.
What are the types of artificial neural networks?
Several categories of artificial neural networks with different properties and
complexities exist. The first and simplest neural network developed is the
perceptron. The perceptron computes the sum of the inputs, applies an
activation function, and provides the result to the output layer.
Another old and simple approach is the feedforward neural network. This
type of artificial neural network has only one single layer. It is a category that
is fully connected to the following layer where each node is attached to the
others. It propagates the information in one direction from the inputs to the
outputs through the hidden layer. This process is known as the front
propagated wave that usually uses what is called the activation function. This
activation function processes the data in each node of the layers. This neural
network returns a sum of weights by the inputs calculated according to the
hidden layer’s activation function. The category of feedforward neural
network usually uses the backpropagation method for the training process
and the logistic function as an activation function.
Several other neural networks are a derivation of this type of network. For
example, the radial-basis-function neural networks. This is a feedforward
neural network that depends on the radial basis function instead of the
logistic function. This type of neural networks have two layers, wherein the
inner layer, the features, and radial basis function are combined. The radial
function computes the distance of each point to the relative center. This
neural network is useful for continuous values to evaluate the distance from
the target value.
In contrast, the logistic function is used for mapping arbitrary binary values
(i.e., 0 or 1; yes or no). Deep feedforward neural networks are a multilayer
feedforward neural network. They became the most commonly used neural
network types used in Machine Learning as they yield better results. A new
dydydytype of learning called deep learning has emerged from these types of
neural networks.
Recurrent neural networks are another category that uses a different type of
nodes. Like a feedforward neural network, each hidden layer processes the
information to the next layer. However, outputs of the hidden layers are
saved and processed back to the previous layer. The first layer, comprised of
the input layer, is processed as the product of the sum of the weighted
features. The recurrent process is applied in hidden layers. At each step,
every node will save information from the previous step. It uses memory,
while the computation is running. In short, the recurrent neural network uses
forward propagation and backpropagation to self-learn from the previous
time steps to improve the predictions. In other words, information is
processed in two directions, unlike the feedforward neural networks.
A multilayer perceptron, or multilayer neural network, is a neural network
that has at least three or more layers. This category of networks is fully
connected where every node is attached to all other nodes in the following
layers.
Convolutional neural networks are typically useful for image classification or
recognition. The processing used by this type of artificial neural network is
designed to deal with pixel data. The convolutional neural networks are a
multi-layer network that is based on convolutions, which apply filters for
neuron activation. When the same filter is applied to a neuron, it leads to an
activation of the same feature and results in what is called a feature map. The
feature map reflects the strength and importance of a feature of input data.
Modular neural networks are formed from more than one connected neural
network. These networks rely on the concept of ‘divide and conquer.’ They
are handy for very complex problems because they allow combining different
types of neural networks. Therefore, they allow combining the strengths of a
different neural network to solve a complex problem where each neural
network can handle a specific task.
How to train an artificial neural network?
As explained at the beginning of this chapter, neural networks compute a
weighted sum of inputs and apply an activation function at each layer. Then it
provides the final result to the output layer. This procedure is commonly
named forward propagation. To train these artificial neural networks,
weights need to be optimized to obtain the optimal weights that produce the
most accurate outputs. The process of the training an artificial neural network
is as follows:
1. Initialize the weights
2. Apply the forward propagation process
3. Evaluate the neural network performance
4. Apply the backward propagation process
5. Update the weights
6. Repeat the steps from step 2 until it attains a maximum number of
iterations, or neural network performance does not improve.
The result is the output of the first layer, which is then fed to the next layer
as:
With W2 and b2 are the weights and bias of the second layer, respectively.
To this result, we apply an activation function F2: