Python Programming - 3 Books in - Ryan Turner
Python Programming - 3 Books in - Ryan Turner
Python Programming: The Ultimate Beginner’s Guide to Learn Python Step by Step
Introduction
1. What is Python Machine Learning?
2. How to Start Learning Python
3. Review of Data Samples and Visualization of Data
4. How to Create a Dataset with Visualization
5. Making Predictions with Algorithms
6. Examples of Coding
7. Decision Tree
8. Neural Networks
9. Bringing it All Together
Conclusion
Python Programming: The Ultimate Intermediate Guide to Learn Python Step by Step
Introduction
1. What Is Machine Learning
2. Supervised Machine Learning
3. Unsupervised Machine Learning
4. The Basics of Working with Python
5. Setting up Your Python Environment
6. Data Preprocessing with Machine Learning
7. Working with Linear Regression in Machine Learning
8. Using a Decision Tree for Regression
9. Random Forest for Regression
10. Working with a Support Vector Regression
11. What is Naive Bayes and How Does It Work with Machine Learning
12. K-Nearest Neighbors Algorithm for Classification
Conclusion
Python Programming: The Ultimate Expert Guide to Learn Python Step-by-Step
Introduction
1. Working with Inheritances in Python
2. Arguments in Python
3. Namespace and Python
4. Working with Iterators in Python and What These Mean
5. Exception Handling and How to Create a Unique Code with Them
6. The Python Generators
7. What are Itertools in the Python Language
8. What are Closures in Python and Why are they so Important
9. Working with Regular Expressions
10. What are the Conditional Statements and When Will I Need to Use Them?
11. Do I Need to Learn Assert Handling in This Language
12. How to Work with Loops in Your Python Code
13. When to Use User-Defined Functions in Your Code
14. Working with Memoization in Python
Conclusion
Copyright 2018 by James C. Anderson - All rights reserved.
The following eBook is reproduced below with the goal of providing information that is as accurate and
reliable as possible. Regardless, purchasing this eBook can be seen as consent to the fact that both the
publisher and the author of this book are in no way experts on the topics discussed within and that any
recommendations or suggestions that are made herein are for entertainment purposes only.
Professionals should be consulted as needed prior to undertaking any of the action endorsed herein.
This declaration is deemed fair and valid by both the American Bar Association and the Committee of
Publishers Association and is legally binding throughout the United States.
Furthermore, the transmission, duplication or reproduction of any of the following work including
specific information will be considered an illegal act irrespective of if it is done electronically or in
print. This extends to creating a secondary or tertiary copy of the work or a recorded copy and is only
allowed with an expressed written consent from the Publisher. All additional rights reserved.
The information in the following pages is broadly considered to be a truthful and accurate account of
facts, and as such any inattention, use or misuse of the information in question by the reader will render
any resulting actions solely under their purview. There are no scenarios in which the publisher or the
original author of this work can be in any fashion deemed liable for any hardship or damages that may
befall them after undertaking information described herein.
Additionally, the information in the following pages is intended only for informational purposes and
should thus be thought of as universal. As befitting its nature, it is presented without assurance
regarding its prolonged validity or interim quality. Trademarks that are mentioned are done without
written consent and can in no way be considered an endorsement from the trademark holder.
PYTHON PROGRAMMING: THE ULTIMATE
BEGINNER’S GUIDE TO LEARN PYTHON
STEP BY STEP
INTRODUCTION
This guide has been composed specifically for Newbies and Beginners. You
will be taken through each step of your very first program, and we will
explain each portion of the script as you test and analyze the data.
Machine learning is defined as a subset of something called artificial
intelligence (AI). The ultimate goal of machine learning is to first
comprehend the structure of the presented data and align that data into certain
models that can then be understood and used by anyone.
Despite the fact that machine learning is a department in the computer
science field, it truly is different from normal data processing methods.
In common computing programs, formulas are groups of individually
programmed orders that are used by computers to determine outcomes and
solve problems. Instead, machine learning formulas allow computers to focus
only on data that is inputted and use proven stat analysis in order to deliver
correct values that fall within a certain probability. What this means is that
computers have the ability to break down simple data models which enables
it to automate routine decision-making steps based on the specific data that
was inputted.
Any innovation client today has profited from machine learning. Facial
acknowledgment innovation enables internet based life stages to enable
clients to tag and offer photographs of companions. Optical character
acknowledgment (OCR) innovation changes over pictures of content into
portable kind. Proposal motors, controlled by machine learning, recommend
what motion pictures or TV programs to watch next in view of client
inclinations. Self-driving autos that depend on machine learning on how to
explore may soon be accessible to shoppers.
Machine learning is a ceaselessly growing field. Along these lines, there are a
few things to remember as you work with machine learning philosophies, or
break down the effect of machine learning forms.
In this book, we'll look at the normal machine learning strategies for managed
and unsupervised learning, the basic algorithmic methodologies including the
k-closest neighbor calculation, specific decision tree learning, and deeply
impactful techniques. We will also investigate which programming is most
used in machine learning, giving you a portion of the positive and negative
qualities. Moreover, we'll talk about some important biases that are
propagated by machine learning calculations, and consider what can be done
to avoid biases affecting your algorithm building.
There are plenty of books on this subject on the market. Thanks for choosing
this one! Every effort was made to ensure it’s full of useful information as
possible, please enjoy!
1
WHAT IS PYTHON?
HISTORY OF PYTHON
Python was invented in the later years of the 1980s. Guido van Rossum, the
founder, started using the language in December 1989. He is Python's only
known creator and his integral role in the growth and development of the
language has earned him the nickname "Benevolent Dictator for Life". It was
created to be the successor to the language known as ABC.
Van Rossum said one the reasons he created Python back in 1996:
““...In December 1989, I was looking for a "hobby" programming project
that would keep me occupied during the week around Christmas. My office ...
would be closed, but I had a home computer and not much else on my hands.
I decided to write an interpreter for the new scripting language I had been
thinking about lately: a descendant of ABC that would appeal to Unix/C
hackers. I chose Python as a working title for the project, being in a slightly
irreverent mood (and a big fan of Monty Python's Flying Circus).”
The next version that was released was Python 2.0, in October of the year
2000 and had significant upgrades and new highlights, including a cycle-
distinguishing junk jockey and back up support for Unicode. It was most
fortunate, that this particular version, made vast improvement procedures to
the language turned out to be more straightforward and network sponsored.
Python 3.0, which initially started its existence as Py3K. Funny right? This
version was rolled out in December of 2008 after a rigorous testing period.
This particular version of Python was hard to roll back to previous
compatible versions which are the most unfortunate. Yet, a significant
number of its real highlights have been rolled back to versions 2.6 or 2.7
(Python), and rollouts of Python 3 which utilizes the two to three utilities,
that helps to automate the interpretation of the Python script.
Python 2.7's expiry date was originally supposed to be back in 2015, but for
unidentifiable reasons, it was put off until the year 2020. It was known that
there was a major concern about data being unable to roll back but roll
FORWARD into the new version, Python 3. In 2017, Google declared that
there would be work done on Python 2.7 to enhance execution under
simultaneously running tasks.
Determine a Problem
Gather the Data
Analyze the Algorithms
Increase positive results
Define results & present them
The absolute easiest way to face with learning an unfamiliar platform or
language is to never give up, and keep working through a machine learning
case study until you get to the end. Just be sure to note all of the key points
and techniques being taught along the way. In particular, pay special attention
to the data loading, the data summarization, how to evaluate different
algorithms, and how to come up with certain predictions.
If you are not able to do that, you have a template that you have available to
you in each of the Datasets.
Try a new tool called the Iris Dataset; it is the easiest for those who are just
starting out with learning machine language. This is a great project because
of it’s easy to use interface.
Attributes are generally numerical which means you have to figure out how
to load and manage all of the data.
Due to its calculative nature, users are enabled to rehearse with a simpler kind
of Python formula or code.
It is a multiple level arrangement issues (ostensible) that may require a
detailed approach.
It just has four qualities and 150 columns which means it is extremely tiny
and effectively fits into the memory
The majorities of the numeric qualities are in similar units and a similar scale
and are not essential to any unique scaling or changes being made in order to
begin.
We should begin with a simple program, “welcome world” machine learning
program in Python.
During this segment, we will power through an easy and fun coding sample
from start to finish.
Here are some of the common Python features we will discover during this
section.
W e will be using the Fisher's Iris Dataset for this example. This
particular Dataset is extremely well known around the
programming community as the “hello world" command in
Python. It practically used in all beginning instructional manuals and training
guides.
The Dataset has 150 different perceptions of Iris blossoms. There are four
sets of estimations of the Iris in centimeters. The fifth set is the possible
different types or species of the Iris flower. It is determined that all of the
flowers that were observed are a part of one of three types of flower.
Wikipedia has a great article on the Fischer Iris Flower Dataset that you
might want to consider reading as a part of your learning.
So let's get on with the example, shall we? In the very first stage of this
process, we have to load the Iris data into Python using CSV format.
Step 4: Load in Parameters
As mentioned, we have to load all of the data, the segments, data functions
and even the objects that are going to needed to complete this example. It’s
best to load them all up front if you and have them available to save time
when trying to recall them later.
Watch and pay particularly close attention for any errors during this step. As
mentioned, if you encounter an error at this stage, please do not continue.
You have to have your environment set up properly for this step. Re-read the
section above if you need a refresher on how to do that. I provided you with a
great resource for beginners and setting that up.
Step 5: Load in all of the Datasets
Simply load in all of the data we need using the "UCI Machine Learning
Repository" to get the Iris data set.
We are utilizing pandas in this example, and what that means is that Pandas
are critical to any Python-based data. It's also extremely helpful in examining
the data further with detailed stats and visual data representation.
Tip: We are specifically naming each column as we load in the data. We can
easily make sense of it when want to analyze it later.
The Dataset should load in with no problems. You will encounter extremely
rare issues with loading the data. It is pretty basic.
In case issues arise, you can install the Iris files directly inside of your
operating directory and load it by utilizing a similar method which is simply
to change the URL to a new file name.
Step 6: The Fun Part
Now, the fun part. This is the part where we investigate and examine the data.
In this particular step of the example, you are going to learn how to analyze
the data in various formats.
Try not to stress. There will be common commands that you can keep in your
own library and use on virtually every future project you do.
Step 7: The Shape Property
Shape property allows us to see the data in rows and columns, similar to an
excel spreadsheet.
Y OUR FIRST 20 rows should look similar to the image below. If not, then see
where you went wrong in the previous steps.
Step 8: Analytical Summary
Let's look at each attribute and its summary. This means that we look at the
low, mid, and high values and its individual percentages.
1
2 # descriptions
print(Data set.describe())
Looking at this set of data, do you notice a certain pattern in the numbers?
They all range from 0 to 8 in centimeters. Can you see that?
Step 9: Class Distribution
We will now investigate the quantity of the examples (pushes) that have a
place in each class. We can see this as a straightforward examination.
You should be able to see here that every single version of the flower has the
exact same number of occurrences; in this case, it's 50. Can you see that in
the data here?
4
Bar Graph
Line Chart or Graph
Histogram Plot
Scatter Plot
Box and Whisker Plot
Matplotlib
Seaborn
ggplot
Bokeh
Plotly
Pygal
Altair
Geoplotlib
Gleam
Missingno
Leather
Y OU SHOULD , at best, have a pretty good idea of the basics about the Dataset.
Now we build on that basic foundation and add-on visualization using plots.
In this Chapter, we are going to dive into examining 2 different types of
plots.
T HE U NIVARIATE P LOT
We begin with taking a look at some univariate plots and what these plots are
of every possible individual variable.
We will use "boxplot" which means it will display the summary of a set of
data up to 5 numbers long. This gives us a much clearer thought of the
appropriation of the data characteristics.
We can likewise make a circle graph of each variable in order to get a clear
picture of how the input is distributed. It would appear that maybe two of the
info factors have a "Gaussian distribution". This means the input is assigned a
mean value. It is useful to you as a beginner which allows you to use specific
algorithms and patterns to test and measure the output.
“Boxplot” is used for another method to review the sharing of every single
attribute.
Boxplots summarize the issuance of all attributes, making a mark for the
average (also known as the middle value) as well as a box around the twenty-
fifth and 75th percentiles.
Histograms
By looking at histograms, you can get a strong sense of the equal distribution
of each and every one of the different attributes.
Histograms are utilized to cluster and evaluate information into collection
bins, then supply you an account of the range of examinations in every single
container. You can rapidly get a sense of if an element is skewed or not,
"Gaussian’", (Real value random), or even includes an exponential division,
through the structure of the containers. That can additionally assist you in
finding potential eccentrics.
The Density Plot
One other method of obtaining a rapid understanding of the delivery of each
property is "Density Plots". The plots resemble an "abstracted histogram"
with a sleek contour drawn thru the peak of every single container.
The Multivariate Plot
In this particular component, there are instances of plots with connections
between numerous parameters.
The Scatterplot Matrix
Whether you could recap the connection concerning 2 variables using a line,
Scatter plots are utilized for locating organized connections in the middle of
parameters, like. Properties that have organized connections might be
correlative and effective nominees for elimination from your data set as well.
A scatterplot also illustrates the connection amongst 2 factors as dots in 2
dimensions. Drawing each of these scatterplots collectively is actually
recognized as scatterplot matrix. A person can easily generate a scatterplot
for every set of properties in their data. Practical to detect the two-fold
relationships from a variety of viewpoints, each scatterplot matrix is
proportionate. Due to the fact, there is the very little point of drawing a
scatterplot for every single factor.
Correlation Matrix Plot
The correlation matrix is known for correlation amongst each pair of
characteristics that can be calculated. To get a good grasp of the knowledge
of which variables hold a large correlation with one another, plotting the
correlation matrix is a good idea.
Correlation provides an indicator towards how associated the changes are
among 2 variables. If the 2 variables shift in conflicting directions together
(one going downward, one going upwards) that means that they are
negatively correlated. If 2 variables shift in a matching direction, then that
means that they are positively correlated.
If there are a lot of exceedingly correlated input variables in your data, then
this is very important to acknowledge, for the reason that several machine
learning algorithms similar to the logistic and linear regressions can obtain
unfortunate or bad performance.
Line Chart
A line graph or line chart exhibits info as groups of data points which are
joined by straight lines, called ‘markers’. It’s a primary type of chart used in
various fields. Despite that the dimension points are ordered (particularly
using their x-axis value) and fused with straight line sectors, it is like the
scatter plot. Line charts are frequently used to envision an inclination in data
over periods of time. Hence, in a chronological way, is how frequently drawn
the line is. In these types of cases, they are known as run charts.
Bar Graph
What is a bar graph for?
A bar graph/chart showcases categorical data through bars that are
rectangular with lengths or heights that are corresponding to the values that
they signify. These bars are designed either horizontally or vertically.
In a nutshell, if you want to visualize and showcase the data in a decent
manner from distinctive groups which are being set against each other, then
the bar graphs are of good use.
I think it's important for a beginner, that we take a look at the difference
between the provided variables.
5
H ow are you feeling at this stage? Have you encountered any errors?
Are you feeling as if you have gained some new knowledge?
Hopefully, things are going smoothly and you are grasping the concepts well
at this point. Let’s take a look at how to make predictions with algorithms in
Python and what it means.
Your aggregated data tells a tale that is certainly complex. To withdraw the
insights, you are going to need an extremely accurate design which is
predictive. This might not be the best step as a beginner; nonetheless, it is
here for reference to the entire picture of Python capabilities.
Predictive analysis is being modeled after major mathematical models to
predict a conference or result. These designs forecast the desired outcome at
some future time based on modifications placed into data inputs. Using a
repetitive procedure, you create the models by choosing a training
information set where you will proceed to test and further validate the
information. After you examine it to ascertain its reliability and accuracy in
predicting forecasts, play around with different methods until you find one
that is comfortable for you. The important thing is that you choose one that
you can understand, learn and apply without much effort.
To give you an idea of some good examples of such a method, you can try a
time-series reversal model for predicting low and even high levels of flight
traffic or fuel. Of course, this is certainly predicting based on a linear
approach to speed compared to upload and continuing to be extremely
beneficial in real-world estimation models for conjecture.
EXAMPLES OF CODING
LOOPS
TYPE CONVERSION
Python has many data types. You have already seen and worked with some of
them. You have floats and integers to interact with numerical values; boolean
(bool) is used to interact with true/false values and strings to work with
alphanumeric characters. You can make use of Tuples, lists, dictionary, and
sets which are data structures where you can store a large collection of
values. To learn more about them, be sure to check out DataCamp Data
Types for Data Science Course.
Implicit and Explicit Data Type Conversion
Data conversion in Python can happen in two ways: either you tell the
compiler to convert a data type to some other type explicitly or the compiler
understands this by itself and does it for you. In the former case, you're
performing an explicit data type conversion, whereas, in the latter, you're
doing an implicit data type conversion.
Conversion, specifically type conversion, is the complete conversion of one
piece of information into another type of information. Understood Type
Conversion is naturally performed by the Python translator. You don't have to
do anything different. Python is notorious for not losing any of the data in the
conversion process.
Typecasting is another example of Type conversion. This particular process
involves the data types of objects, which are then transformed, using a set of
criteria that you or the end user defines. Unfortunately, loss of data is
common in this type of conversion because it is going from object to specific
type of data and not simply data to data or object to object.
7
DECISION TREE
Wizard: Fill out simple forms that give the questions, answers and
messaging for your tree, as well as prompt for more in-depth info.
Overview: Start your decision tree entirely from scratch (for the
more experienced or adventurous!)
Designer: Draw your decision tree using your imagination to create
each node easily and simply, along with their connections, and
expect navigational flow.
REGRESSION TREES
In the previous chapter, you have learned about Decision Trees introduced
the basic concepts underlying decision tree models, how you can build tree
models with Python starting from scratch. We have also introduced the
advantages and disadvantages of decision tree models as well as important
extensions and variations. For example, one drawback of Classification
Decision Trees is that they always need a target feature that is categorically
scaled. For example, Days = {Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday}.
Here arises a problem: What if we want our tree for instance to predict the
price of a house given some target feature attributes like the number of rooms
and the location? In this situation, the values of the target feature are
continuous and are no longer categorically scaled. Conceptually, a house can
have an unlimited number of prices.
That's where Regression Trees comes into action. Regression Trees work in
the same principle as the Decision Trees with the big difference that the
target feature values can now take on an infinite (never-ending) number of
continuously scaled values. Hence it proves that the task is now to foretell the
value of a continuously scaled target feature Y given the values of a set of
categorically (or continuously) scaled descriptive features X.
NEURAL NETWORKS
1. There is a small circle on the left side of the perceptron, which are
“neurons” and they have x subscripts 1, 2, … , m that carries data
input.
2. All of the inputs are multiplied by a weight, which is gets labeled
using a subscript 1, 2, … , m, along with a long arrow, which is the
synapse, and travels to the big circle in the middle. So you will have
w1 * x1, w2 * x2, w3 * x3 and so on.
3. After all of the inputs have been multiplied by weight, you will sum
them all up and add a bias that had been pre-determined.
4. The results are then pushed onto the right. You will then use the step
function. All this tells you is that if the number you get from step
three is greater than or equal to zero, you will receive a one as your
output, otherwise, if your result is lower than zero, the output will be
zero.
5. You will get an output of either zero or one.
If you were to switch the bias and place it on the right in the activation
function such as “sum(wx) ≥ -b” the –b would be known as a threshold value.
With this, if the sum is higher than or equal to your threshold, then your
activation trigger is one. Otherwise, it would come out to be zero. Pick the
one that helps you to understand this process because both of these
representations are interchangeable.
Now you have a pretty good understanding of how a perceptron works. All
it’s made up of is some mechanical multiplications, which then makes
summations, and then ultimately give you an activation, and that will give
you an output.
Just to make sure that you fully understand this, let’s have a look a super
simple example that is likely not realistic at all. Let’s assume that you have
found extreme motivation after you have read this book and you have to
decide if you are going to study deep learning or not. You have three major
factors that will help you to make your decision:
1. Will you be able to make more money once you master deep
learning: 0 – No, 1 – Yes.
2. Is the needed programming and mathematics simple: 0 – No, 1 –
Yes.
3. You are able to use deep learning immediately and not have to get an
expensive GPU: 0 – No, 1 – Yes.
Our input variables will be x1, x2, and x3 for all of the factors and we’ll give
them each a binary value since they are all simple yes or no questions. Let’s
assume that you really love deep learning and you are now ready to work
through your lifelong fear of programming and math. You also have some
money put away to invest in the expensive Nvidia GPU that will train the
deep learning model. You can assume that both of these have the same
importance, because they can both be compromised on. But, you really want
to be able to make extra money once you have spent all of the energy and
time into learning about deep learning. Since you have a higher expectation
of ROI, if you can’t make more moolah, you aren’t going to waste your time
on learning deep learning.
Now that you have a decent understanding of the decision preferences, we
can assume that you have a 100 percent probability of making extra cash
once you have learned deep learning because there’s plenty of demand for
not much supply. That means x1 = 1. Let’s assume that programming and
math are extremely hard. That means x2 = 0. Finally, let’s assume that you
are going to need a powerful GPU such as a Titan X. That means x3 = 0.
Okay, now that you have the inputs, you can initialize your weights. We’re
going to try w1 = 8, w2 = 3, w3 = 3. The higher the value for the weight, the
bigger the influence it has with the input. Since the money you will make is
more important, your decision for learning deep learning is, w1 is greater
than w2 and w1 is greater than w3.
Let’s say that the value of the threshold is five, which equals the bias of
negative five. We add everything together and add in the bias term. Since the
threshold value is five, you will decide to learn deep learning if you are going
to make more money. Even if the math turns out to be easy and you aren’t
going to have to buy a GPU, you won’t study deep learning unless you are
able to make extra money later on.
Now you have a decent understanding of bias and threshold. With a threshold
as high as five, that means the main factor has to be satisfied in order for you
to receive an output of one. Otherwise, you will receive a zero.
The fun part comes next: varying the threshold, bias, and weights will
provide you with different possible decision-making models. With our
example, if you lower your threshold from five to three, then you will get
different scenarios where the output would be one.
Despite how well loved these perceptrons were, the popularity faded quietly
due to its limitations. Later on, people realized that multi-layer perceptrons
were able to learn the logic of an XOR gate, but this requires the use of
backpropagation so that the network can learn from its own problems. Every
single deep learning neural networks are data-driven. If you are looking at a
model and the output it has is different from the desired output, you will have
to have a way to backpropagate the error information throughout the network
in order to let the weight know they need to adjust and fix themselves by
certain amounts. This is so, that in a gradual way, the real output from the
model will start getting closer to the desired output with each round of
testing.
As it turned out, when it comes to the more complicated tasks that involved
outputs that couldn’t be shown with a linear combination of inputs, meaning
the outputs aren’t linearly separable or non-linear, the step function will not
work because the backpropagation won’t be supported. This requires that
your activation function will have to have meaningful derivatives.
Here’s just a bit of calculus: a step function works as a linear activation
function where your derivative comes out to 0 for each of the inputs except
for the actual point of 0. At the point of 0, your derivative is going to be
undefined because the function becomes discontinuous at this point. Even
though this may be an easy and simple activation function, it’s not able to
handle the more complicated tasks.
Sigmoid function: f(x) = 1/1+e^-x
Perceptrons aren’t stable when it comes to being a neural network
relationship candidate. Look at it like this: this person has major bipolar
issues. There comes a day (f z < 0), they are all “down” and “quiet” and
doesn’t give any response. The next day (for z ≥ 0), they are all of a sudden
“lively” and “talkative” and is talking nonstop. A huge change, isn’t it? Their
mood doesn’t have any transition, and you’re not sure if it is going to go up
or down. This is a step function.
Just a bit of a switch in each of the weights within the input of the network
may cause a neuron to flip from zero to one, which could end up affecting the
behavior of the hidden layer, and this would cause a problem for the
outcome. It’s important that you have a learning algorithm that improves the
network because it slows the weights change without any sudden jumps. If
you aren’t able to use step functions to slowly change up the weight values,
then you shouldn’t use it.
We are now going to say goodbye to the perceptron with a step function. A
new partner to use in your neural network is the sigmoid neuron. This is done
by using the sigmoid function, which is written above. This is the only thing
that is going to change is the activation function, and all the other stuff that
you have learned up to this point about the neural network is going to work
the same for the new neuron type.
With this equation, the weight adjustment can be generalized, and you would
have seen that this will only require the information from the other neuron
levels. This is why this is a robust mechanism for learning, and it is known as
the backpropagation algorithm.
9
PRACTICAL IMPLEMENTATIONS
Python and Pygame is a decent dialect and system for fast amusement
prototyping or for apprentices figuring out how to make straightforward
recreations. To total up, Python isn't generally the best dialect for
programming games; however, it is an essential instrument in a game
software engineer’s toolbox.
Python has different modules, libraries, and stages that support game
improvement. For instance, PyGame gives usefulness and a library to game
advancement. There have been various games manufactured utilizing Python
including Civilization-IV, Disney's Toontown Online, Vega Strike and so on.
A French research team found that finding invasive brain cancer cells while
in surgery was hard, mainly because of the lighting in the OR. They
discovered that when they used neural networks along with Raman
spectroscopy during surgery, it allowed them to be able to detect the cancer
cells more easily and lowered the leftover cancer. Actually, this is only a
single piece of many over the last couple of months that have matched the
workings of advanced classification and recognition with several kinds of
cancers and screening tools.
The conventional venture stages are by need expansive and complex. They
rely upon expounding devices to oversee code, assembles, and organizations.
For some reasons, this is needless excess. Any software engineer ought to
have the capacity to go after her most loved dialect when motivation hits her,
and Python's instantaneousness makes it appropriate for basic mechanization
assignments and speedy prototyping. Engineers as a rule likewise feel that
Python gives them the headroom to move past a model without discarding
their past work. Without a doubt, Python can be utilized for substantial and
complex programming frameworks. YouTube, for example, runs primarily on
Python, and it is an oft-favored dialect at associations including Google,
NASA and Industrial Light, and Magic. Particular Python libraries and
structures exist for logical programming, information control, Web
administrations, XML exchange, and numerous different things
Language Development:
The module and design of Python’s architecture has shaken the development
of many languages. Boo language uses a syntax, object model, and
indentation, related to Python. Moreover, the syntax of these shares common
features with Python. For example, Apple’s Swift, Cobra, CoffeeScript, and
OCaml.
Prototyping:
Python is easy and simple to learn, it also has the advantage of being open
source and free with the help and support of an enormous community. This
makes it the preferred choice for prototype development. Further, the
nimbleness, scalability and extensibility and ease of refactoring code
associated with Python allow faster development from initial prototype.
These were the key points that are essential for anyone who wants to start
learning python. All of the points that are mentioned above are the basics of
python that will build a strong base for further learning. Wish You All Good
Luck In Your Journey Of Learning Python.
I want to thank all of you who got through my book. Secondly, if you guys
found this helpful in any way it would mean the world to me. Finally, if you
found this book useful in any way, a review on Amazon is always
appreciated!
PYTHON PROGRAMMING: THE ULTIMATE
INTERMEDIATE GUIDE TO LEARN PYTHON
STEP BY STEP
INTRODUCTION
Thank you for downloading the book Python Programming: The Ultimate
Intermediate Guide to Learn Python Step by Step. I hope you find it
educational about all the topics you need concerning the exciting field of
machine learning.
This guidebook is going to explore how the Python coding language can
work together with machine learning in order to create a program that can
learn all on its own. With traditional coding methods, this just isn’t possible.
You will be stuck with a program that can only do one activity and one that
isn’t able to deal with any learning along the way. But with machine learning,
your program can look through data, look through past performance, and
more and then learn how it should behave.
This guidebook is going to take some time to explore machine learning and
how you can get started with it. We will explore a bit about machine learning
and its basics before moving on to some of the basics that you need to know
about the Python coding language before getting started, and how to set up
the environment. When that information is organized, we will then move onto
some of the different things that you can do with machine learning, using
some of the different scripts from Python such as linear regression, Decision
Trees, random forests, and more.
When you are ready to learn how to use python with machine learning, make
sure to check out this guidebook to help you get started!
1
B efore we can learn more about how to use Python to help with
machine learning, it is important to learn more about what the field
of machine learning is able to do. Machine learning is a subset of
artificial intelligence that will deal with technology being able to learn from
the input it is given. With a traditional computer program, the program can
only do what was put into the code. It never takes the input from the user or
makes any decisions on its own to learn and grow. It simply repeats what the
programmer put into the code. But with machine learning, the program is
able to learn based on trial and error, by looking at patterns in the data that it
sees, and even from the input that the user adds.
The idea behind machine learning is to help the program learn how to read
data and then make decisions on its own. There are a lot of times when a
program needs to be able to behave without the programmer being there to
tell it what to do. For example, with a speech recognition program, the
program might have trouble with understanding some speech patterns in the
beginning, but over time, it will learn how to understand the person who uses
it the most and it will make fewer mistakes.
The first definition of machine learning was coined in 1959 by Arthur
Samuel. He defined machine learning in this manner, “Field of study that
enables computers to learn without being explicitly programmed.”
This is one of the neat things about machine learning. The machine is able to
figure out patterns out of a large amount of data, even if the programmer
didn’t specifically tell it how to behave. This can be helpful in uses such as
speech recognition, search engines, and for companies who need to search
through large amounts of data to find patterns and make decisions about how
to act in the future.
How Can a Machine Learn?
So, the next question is how our machines can learn. Before we dwell on all
the details that come with machine learning, we must take a look at the way
that humans learn. This is going to give us a good insight into how machine
learning is going to work.
For instance, we as humans know that we shouldn’t touch a heating plate
using our bare hands. But how do we know not to do this? There are two
possibilities with this one. Either we were burned in the past by touching one
of these plates, or a hot stove, or we were taught by others not to touch these
hot plates. In either case, there is some experience in the past that made us
not touch the heating plates when we see they are on. In other words, we have
some form of past information that we can use to make our future decisions.
Machine learning is going to work in a similar manner. In the beginning, the
computer program basically has no knowledge. These programs are just like
humans when they are first born, having zero knowledge and not knowing
how they are supposed to act. To make a machine learn, information needs to
be passed over to these machines. Then going from this information, the
machines are able to identify patterns with various techniques. Over time, the
machines are going to learn how to identify patterns from the data they have
in order to make decisions, and then they can move on to making some
decisions with data they haven’t even seen.
Training data can be fed into the machine learning algorithms that are nothing
but complex mathematical algorithms. The algorithms are then going to result
in models for machine learning. These models for machine learning are neat
because they have the capability of making predictions on new data, even
data that is unseen.
When Is Machine Learning Used?
While you may not have heard about machine learning in the past, there are
actually quite a few times when it can be used in our modern world. Machine
learning is always adding to its teaching set and learning more than we could
ever imagine. Some of the ways that machine learning can be used in our
modern world include:
Data Security
Malware is a huge problem. Kaspersky Lab said that it found 325,000 new
malware files every day in 2014. But, a company known as Deep Instinct
says that each piece of new malware is going to have a very similar code to
the previous versions. Only up to 10 percent of the code changes which is not
that much of a change. They have begun to use a learning model that is able
to predict what malware is before it can attack. Machine learning algorithms
have also been used to look for patterns in how data inside a cloud is
accessed and report anomalies that show up and could predict security
breaches.
Personal Security
If you have ever gone to a big event, it’s likely that you had to spend some
time in a long security line. Machine learning is proving that it is a big asset
when it comes to eliminating false alarms and being able to catch things that
humans may miss. This could be used at concerts, stadiums, and airports.
Financial Trading
There are many people who are interested in learning how to predict what the
stock market is going to do on any given day so they can make more money.
Machine learning algorithms are able to do this with some accuracy. This can
help you to estimate how the market is going to react so you can make
smarter predictions and keep more money in your pocket.
Healthcare
Marketing Personalization
Even the world of marketing can get in on machine learning. The more that
you are able to understand about your customer, the more the company is
able to sell. With the help of machine learning, a program can follow where
the customers have been on your site, as well as online with other websites,
in order to come up with an idea of how to market to that person. This opens
up a lot of opportunities in terms of how you can market and reach your
customers.
Fraud Detection
Machine learning has gotten better at being able to spot potential fraud causes
in different fields. For example, PayPal is using machine learning in order to
catch those participating in money laundering. The company has many tools
that they use to compare the millions of transactions that go through and they
are able to precisely distinguish between fraudulent and legitimate
transactions that occur.
Recommendations
Many sites, including those like Netflix and Amazon, will use the
recommendation feature. This helps them to look through your activity and
then compare it with the other users in the hopes of figuring out what you are
most likely to binge or watch or purchase next. The recommendations, as
they gather more information from users, are always getting better so it won’t
take long before they meet up with your needs, even if you are a new user.
Online Search
One of the most popular methods of machine learning is with search engines.
Each time that you put in a search, the program is going to watch how you
are responding to the results it sends. If you click the top result and you stay
on that page for a bit, it will assume that the program got the right results and
the search was successful. If you happen to click on the second page, or you
go and type in a new search and you don’t look at any results, the program is
going to learn from that mistake to do better next time.
When you first purchase one of these products, you may run into a lot of
issues. It may have trouble recognizing some of your speech patterns and
there may be some mistakes. Over time, the program is going to learn from
its mistakes and there will be a lot more accuracy in the results you get from
the voice recognition program.
As you can see, there are many different uses of machine learning, and this is
just the beginning. As the technology continues to change, it won’t take long
for machine learning to change and adapt with it as well. There are just so
many great things that you can do with the help of machine learning, and this
is definitely a field that is going to grow into the future.
The Importance of Machine Learning
The ultimate goal of artificial intelligence is to make a machine work in a
similar manner to humans. However, the initial work that has been done with
this shows that we are not able to make a machine as intelligent as humans.
This is because humans are constantly learning in an environment that is
evolving all of the time, while this isn’t really possible for a machine.
Therefore, the best way to make a machine intelligent is to make them learn
on their own. What this means is that machine learning is basically the
discipline of science that works to teach machines how to learn on their own
from data.
The idea that comes with machine learning is that instead of going through
and coding all of that logic and data into the program, the data is going to be
fed into the machine, and then the machine is able to learn from this data,
simply by finding the patterns that are there. What is interesting here is that
these machine learning techniques can be faster than humans at finding these
patterns.
The techniques that are used in machine learning have been around for some
time. But because, until recently, there has been a lack of hardware that is of
high performance enough, these techniques were not used to help solve
problems in the real world. But now, we have a lot of the complex hardware
that is needed for machine learning, and because of the huge amount of data
readily available, these techniques are coming back and have been used
successfully to help develop machines that are intelligent.
The Different Types of Machine Learning
To keep things simple, the techniques that are used with machine learning are
going to be categorized into two different types. These include unsupervised
learning and supervised learning. Let’s divide these up a bit and see what
each one is about.
Supervised Learning
In supervised learning, both the input data, as well as the corresponding
category that the input data belongs to is going to be put into the learning
algorithm. The learning algorithm is going to learn what the relationship is
between the output and the input, and then it can make predictions about the
output of any data samples that are unseen.
For example, this kind of learning algorithm can be fed to images of apples
that have been labeled as fruit, and the potatoes that are labeled as vegetables.
After the machine has gone through training with this data, the algorithm will
be able to identify new images of potatoes as vegetable and those of apple as
a fruit, even without those labels.
There are a few steps that are often seen when it comes to a supervised
learning algorithm. These include:
You will feed the algorithm with the input records of x, and then the
output labels of y.
For each input record, the algorithm is going to predict an output of
y.
The error in prediction is going to be calculated when you subtract y
from y.
The algorithm is able to correct itself by taking out that error.
The other steps will continue for multiple iterations until the
likelihood of an error is almost gone.
Supervised learning can be used for a wide number of things, but it will
usually help you to solve two different types of problems including
regression and classification.
Classification
This is going to refer to the process of being able to predict a discrete output
for a given input. So, if the given input is predicting whether your mail is
either ham or spam, a tumor is malignant or benign, or if a student is going to
fail or pass an exam.
Regression
Random forests
KNN
Decision trees
Regression algorithms
We are going to take a look at how to use some of these later on in this
guidebook so you can get a good idea of how to use supervised machine
learning techniques as well.
3
W hile there are a lot of times when you will rely on supervised
machine learning, there are also times when you will need to use
with the other type, the one called unsupervised machine
learning. With supervised machine learning, you will simply show some
examples to the computer and then you can teach the computer how to
respond the way that you want. There are a ton of programs that are going to
rely on this kind of machine learning. But there are also sometimes when it
may seem overwhelming to think of showing thousands or hundreds of
thousands of examples to the computer and this is very tedious. Plus, there
are many programs where you are not going to see that work well for your
program at all.
This is where you will want to work with unsupervised machine learning.
This is the next type of machine learning that we are going to look at.
Unsupervised machine learning is the type that you will use in order to let the
computer learn on its own based on the information that the user gives to it. It
may make mistakes on occasion, but the algorithm will be set up to learn
from these mistakes. What this means is that the algorithm for machine
learning that is unsupervised will be in charge of figuring out and analyzing
the data patterns based on the input that you give it.
Now, there will also be a few different types of algorithms that can work well
with unsupervised machine learning. Whichever algorithm you choose to go
with, it is able to take that data and restructure it so that all the data will fall
into classes. This makes it much easier for you to look over that information
later. Unsupervised machine learning is often the one that you will use
because it can set up the computer to do most of the work without requiring a
human being there and writing out all the instructions for the computer.
A good example of this is if your company wants to read through a ton of
data in order to make predictions about that information. It can also be used
in most search engines to give accurate results.
There are a lot of different techniques that you can use when it comes to
machine learning. Some of the most common methods include:
Markov algorithm
Clustering algorithm
Neural networks
You can use both uppercase and lowercase letters in the name of
your identifier. You can also work with underscore symbols and
numbers as well. Any combination of the above characters work as
well, just make sure that inside the name, there aren’t any spaces.
So, do not write out something like ‘My first program’. You would
write it as ‘Myfirstprogram’.
Your identifier can’t start out with a number. You can use the
number anywhere else that you want in the name, but it can’t be the
first character of your program. If you do put a number as the first
character, you are going to get an error signal when you have the
compiler try to do this one. You can write something like ‘one
program’, but you can’t name the identifier something like
‘1program’.
The name of the identifier can’t have one of the Python keywords in
it. If you add in the keyword to the name, you are going to cause
confusion in the compiler and you won’t get the program to work.
The rule for naming your identifier doesn’t have to be difficult. As long as
you follow these simple rules, you can give your identifier any name that you
would like. If you do happen to forget one of the rules for naming an
identifier, the compiler is going to notice and you will end up with a syntax
error. You simply need to go back through and fix it and this error will go
away.
Python Comments
You can also work with comments in Python. These comments can be useful
for explaining some of your code, especially if you are trying to explain it to
another programmer or person who is looking through your code. Any time
that a party of your code needs a little bit of clarification, you can add in a
comment. The compiler is not going to recognize the comment and simply
skips right over it without reading at all. Once you indicate to the compiler
that the comment is done, it will start reading the code again. Someone who
executes the code will have no idea how many comments you have or even
where you put them in the program.
It is pretty easy to write out the comments that you want to add into your
code. You simply need to use the (#) sign in front of the comment that you
want to write. So, you could write out something like #this is my Python
code. When the comment is done, you just hit return and start writing the rest
of the code on the next line. As long as you have that sign right in front of the
comment, then your compiler is able to just skip over it and will not read out
what you put in the comment.
You are able to choose how many comments you would like to do inside of
your code. Sometimes the code will just need a few of them while other times
the code may need a lot of comments. Keep the comments down to the ones
that you really need and do not waste time or space writing out more
comments which aren’t necessary.
Python Statements
You can also work with statements inside your Python code. These
statements are useful because they are simply a string of code that often has
some of the other parts that we talk about in this chapter. You can then send
these statements over to your chosen compiler so that the code can be
executed. You can write out any statement that you would like, but it must be
written out in a manner that the compiler will understand. Statements can be
as short or as long as you would like. Some statements are only going to be
one character long, and other times it will be many lines long.
Python Variables
Variables are important because they will save up spots on your computer’s
memory in order to hold onto parts of your code. When you go through the
process of creating a new variable, you are making sure that you are
reserving some space on your computer for this. In some cases, such as when
you are working with data types, your interpreter will do the work of
deciding where this information should be stored because that speeds up the
process.
When it comes to working with variables, your job will be to make sure that
the right variables are lining up with the right values. This will ensure that the
right parts show up in your code at the right time. The good news is that you
are able to give the variable the value that you would like, but do check that it
actually works inside of your code. When you are ready to assign a new value
to a variable, you can use the equal sign to make this happen. Let’s look at a
good example of how this would work:
#!/usr/bin/python
PRINT COUNTER
print miles
print name
With the above example, you will get the results to show up with the variable
that you placed with the value. So, when the counter shows up, it will show
you a 100 for the counter, 1000 for the miles, and John as the result of the
name.
Python Functions
Functions can be another part of your language that you need to learn about.
This is basically a part of the code that can be reused and it can help to finish
off one of your actions in the code. Basically, these functions are often really
effective at writing out your code without having a lot of wasted space in the
code. There are a lot of functions that you can use in Python, and this can be
a great benefit to the programmer.
When you are working on your code, you will need to make sure that you are
defining the function. Once you have been able to define the function and it is
considered finalized, it is time to execute it to work in the code. You have the
choice to call it up from the Python prompt or you can call it up from another
function. Below is an example of how you are able to do this:
#!/usr/bin/python
OOP is able to pull some of the emphasis away from the procedure
of your code and it places more of this emphasis on the data in your
code.
All of the programs that you write will now be divided up into
objects.
The data is structured in order to be characterized by the objects that
are inside of them. The data will be the classes that hold the objects,
and all the objects that are inside of the class need to match up in a
way that makes sense.
The functions that will be important for operating the data of your
objects are going to come together when you are working with the
structure of the data.
The data in your code is hidden so that the external functions will
have access to the data. This sometimes caused some issues in the
past when it came to the older programming languages because the
information could easily get mixed up.
The objects that you use have the ability to communicate with each
other thanks to the fact that they have functions that are in common.
The new data, as well as the functions that you have, can be added
into the code any time that you want.
Whenever you start to design one of your own programs, you have
to remember that you have to follow the bottom-up approach.
As you can see, there is a lot to enjoy when using the Python coding
language. It is designed with a beginner coder in mind, which is why it is one
of the best options for you to choose and why you find it being used with
some other complex topics such as machine learning and even hacking.
5
T he Python coding language is one of the best options that you can
choose to use when it comes to programming, especially when you
want to work with machine learning. Python is a very simple coding
language to learn, and it is often the first one that beginners will take a look at
because of its simplicity. But don’t be fooled. Just because it is simple to
learn Python doesn’t mean it won’t have the power and strength that you are
looking for in a programming language. This chapter is going to take some
time to explore how you can set up your Python environment to help with
machine learning.
The first thing that you will want to do after installing the Python program
from www.python.com is to download the proper IDE. This is a great
environment to use with Python and will give you all the help that you need.
It also includes the area to do a Python installation, your editors and your
debugging tools. The IDE that we are going to use in this section is
Anaconda. This IDE is easy to install, light, and it has a lot of great
development tools that you will enjoy. It also has its own command line
utility so you can install some third party software as needed. And with this
IDE, you won’t have to go through and install the Python environment
separate.
In order to download the Anaconda IDE, there are a few steps to complete.
We are going to look at the steps that you can use when installing this for a
Windows computer, but the steps for installing on a Linux or Mac computer
are pretty much the same:
1. To start, go to https://www.anaconda.com/download/
2. From here, you will be sent to the homepage. You will need to select
on Python 3.6 because it is the newest version. Click on “Download”
to get the executable file. This takes a few minutes to download but
will depend on how fast your internet is.
3. Once the executable file is downloaded, you can go over to its
download folder and run the executable. When you run this file, you
should see the installation wizard come up. Click on the “Next”
button.
4. Then the License Agreement dialogue box is going to appear. Take a
minute to read this before clicking the “I Agree” button.
5. From your “Select Installation Type” box, check the “Just Me” radio
button and then “Next”.
6. You will want to choose which installation directory you want to use
before moving on. You should make sure that you have about 3 GB
of free space in the installation directory.
7. Now you will be at the “Advanced Installation Options” dialogue
box. You will want to select the “Register Anaconda as my default
Python 3.6” and then click on Install.
8. And then your program will go through a few more steps and the
IDE will be installed on your program.
Y our data must be in a specific format before you are able to apply
any type of machine learning algorithm to them. Converting data
over to the right format to do machine learning is going to be
known as the process of data preprocessing. Depending on the type of dataset
you are working with, there are a few preprocessing steps that you will need
to use in order to make the data in the right form. The steps you will need to
use include:
Let’s take a closer look at each of these and how they can work together to
prepare your data the proper way.
Getting the Data Set
To start, all of the data sets that we are going to use in this guidebook can be
found at the following link to make things easier:
https://drive.google.com/file/d/1TB0tMuLvuL0Ad1dzHRyxBX9cseOhUs_4/
view
When you get to this link, you can download the “rar” file and then copy the
“Datasets” folder of to your D drive. All of the algorithms that we will use in
this book will access the datasets from “D:/Datasets” folder. The dataset that
you need for this chapter to help with learning more about preprocessing is
known as patients.csv.
If you take a look at this dataset, you will notice that it has information about
the Gender, BMI, and Age of twelve patients. This dataset also has an extra
column that shows you whether the patients have diabetes or not. The BMI
and Age columns are numeric here because they are going to contain
numerical values in them, and the Diabetic and the Gender columns will be
categorical in nature instead.
Another distinction that you can make here is between the independent and
the dependent variable. As a rule of thumb, the variable whose value is
predicted to be the dependent variable and the variables that will be used to
make predictions will be the independent variable. In this example, the
diabetic column is going to be the dependent variable because it is often
going to depend on the other three columns, and those columns are going to
be the independent columns.
Import Libraries
Now that you have some of the data that you need to get started on this, you
need to work on importing the library. Python automatically comes with
some prebuilt libraries that are able to perform a variety of tasks. For the
purpose of this book, we are going to use the Scikit Learn Library. To keep it
simple, we are going to install three libraries that are the most essential for
helping us do machine learning. These include NumPy, matplotlib.pyplot,
and pandas.
NumPy is an important library because it is able to go through and do a lot of
the more advanced mathematical functions that you need. Since machine
learning is going to use a lot of mathematics in it, it is worth your time to
install this Python library.
You can also install the matplotlib.pyplot library. This library is a great one
to use in order to plot out some good charts. Often, when you are looking at
the data that you want to understand through machine learning, you will need
to have this library.
And finally, you should download the pandas library. This library is an easy
one to use and it can allow you to easily import as well as view your datasets
while we do machine learning.
In order to import all of these libraries, you need to either create a new
notebook for Python in Jupyter or you can open up a new file inside Spyder.
The codes that we are going to use with this guidebook are done in Spyder so
you are aware. To import these libraries, use the following code:
import NumPy as np
import matplotlib.pyplot as plt
import pandas as pd
Import the Dataset
Once you have those three important libraries downloaded, it is time to move
on to the next step of importing the dataset that you want to use into the
application that we just created. This will also give you a good idea of why
we are using the pandas library here.
Our dataset is going to be presented in the Comma Separated Values, or CSV
format. The pandas library is going to contain the read_csv function that will
take the path to the CSV formatted dataset as a parameter and loads the
dataset into pandas data frame which is basically an object that stores dataset
in the form of columns and rows.
To make this happen, you can use the following script:
The script above is going to help you to load up the data set for patients.csv
in the dataset fold that you have it set. If you are using the Jupyter notebook,
this is even easier to do. You would just use the following script to help you
see what the data looks like:
patient_data.head()
But, if you are working with the Spyder program, you would go over to your
Variable explorer and then double click on a patient_data variable from the
list of variables that show up. Once you click on the right variable, you will
be able to see all the details of this dataset.
At this point, you should be able to see the pandas data frame that looks
similar to a matrix with zero-based index. Once you have the dataset loaded,
the next step is to divide the dataset into a matrix of features and vector of
dependent variables. The feature set will consist of all your independent
variables. For instance, the feature matrix for the patients.csv dataset is going
to contain the information about the Gender, BMI, and Age of the patient. In
addition, the size of your feature matrix is equal to the number of
independent variables by the number of records. In this case, our matrix is
going to be 3 by 12 because we have twelve records and three independent
variables.
Let’s first go through and create our feature features. You can give it any
name that you would like, but traditionally it is going to be denoted by the
capital X. To help us read the code a bit better, we are going to name it
“features” and then use the following script:
features = patient_data.iloc [:,0:3].values
With the script that we used above, the iloc function of your data frame is
used to help select all the rows as well as the first three columns from the
patient_data of the data frame. This iloc function is going to take on two
parameters. The first is the range of rows to select, and then the second part is
going to be the range of columns you want the program to select.
If you would like to create your own label vector from here, you would use
the following script to get it done:
labels = patient_data.iloc[:3].values
How to Handle Any Missing Values
If you take a look at your patient_data object, you are going to notice that the
record at index 4 is missing out on a value in the BMI column. To help
handle these missing values, the easiest approach will be to remove the
record that is missing a value. However, this record could contain some
crucial information so you won’t want to remove it at this time.
Another approach that you can use to help deal with this missing value is to
put something in there to replace that missing value. Often the best choice
here is to replace the missing value with the median or the mean of all the
other values in that same column. To be able to handle the missing values
that come in, we are going to use the Imputer class from the
sklearn.preprocessing library. The script that you need to make this work
includes:
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values=”Nan”, strategy=”mean”, axis =0)
imputer = imputer.fit(features[:,1:2])
features[:,1:2] = imputer.transform(features[:,1:2])
With the script that we wrote above, the first line is in charge of importing the
Imputer class from the right library. We then went on to create the object of
the Imputer class. This is going to take on three parameters including axis,
strategy, and missing_value. In terms of the missing_value parameter, we are
specifying that this is the value that we want to be replaced. In our data set,
the missing value is being shown by “nan”. The strategy parameter is going
to specify the type of strategy that we want to use in order to fill in this
missing value. You can also choose from most-frequent, median, and mean.
And then the axis parameter is going to denote which axis we want to impute
here.
How to Handle the Categorical Data
Right now we know that machine learning algorithms are all going to be
based on mathematical concepts, and to work with mathematics, we need to
work with numbers. Because of this, it is going to be more convenient to take
all of your categorical values and move them over to numeric values. When
we look at our example, we have two categorical values, the Diabetic, and the
Gender option.
The good news is that with the sklearn.preprocessing library, there is the
LabelEncorder class, the one that is going to take your categorical column
and then give you the right numerical output to make sense out of it. The
script that you can use for this one includes:
from sklearn.preprocessing import LabelEncoder
labelencorder_features = LabelEncoder)_
features[:,2] =labelencoder_features.fit_transform(features[:,2])
Just like what we did with the Imputer class, the LabelEncorder class is going
to have a fit_transform method, which is just a combination of the transform
and the fit methods. The class is going to be able to take the categorical
column that you have as the input and then will return the right numeric
values to help you out.
In addition, you can always take the labels vector and then convert it to a set
of numeric values as follows:
labels = labelencoder_features.fit_transform(labels)
Dividing the Data into Training Sets and Tests Sets
Earlier in this guidebook, we discussed that the machine learning models are
going to be trained on a subset of data and then tested on a different subset.
This splitting up your test and training set is done to make sure that the
algorithm you use for machine learning isn’t going to overfit. When we talk
about overfitting, we are referring to the idea that machine learning is going
to perform good results on the training data, but then will give poor results
with the test data.
A good model for machine learning is one that is able to give good results in
both the test data and the training data. That way, we are able to say that the
model we picked has correctly learned all the underlying assumptions from
our set of data and that we are able to accurately use it to make decisions on
any new set of data that you use. The script that is below is going to divide up
your data into 75 percent train size and the rest will be the test size.
from sklearn.model_selection import train_test_split
train_features, test_features, train_labels, test_labels = train_test_split
(features, labels, test_size = 0.25, random_state = 0)
When you execute this script above, you are going to see the train_features
variable is going to contain the matrix of 9 features (because this is the 75
percent of 12) while the train_labels is going to contain the labels that
correspond to this. However, with the test-features, you are going to have the
remaining three features and the test_label will have the corresponding labels.
How to Scale the Data
And the final step that we need to work on with the preprocessing method
here before we are able to put this data into our algorithm for machine
learning is feature scaling. We need to work with the scale features because
there are some data sets that will have a big difference between the values of
its features. For instance, if we add in the number of red blood cells of
patients from patients.csv, the column is going to have values that are in the
hundreds of thousands, but the age column would be much smaller. Many
machine learning models are going to use the Euclidean distance to help find
the distance between the data points in your data.
The good news is that the sklearn.preprocessing library is going to contain
the class known as StandardScaler that you can use in order to implement the
standardization features. Like with other preprocessing classes, it will also
contain the fit-transform that we talked about before and it will take a data set
that you provide it as the input, and then output a scaled data set. The
following script will make this happen for you.
from sklearn.preprocessing import StandardScaler
feature_scaler = StandardScaler()
train_features = feature_scaler.fit_transform(train_features)
test_features = feature_scaler.transform(test_features)
One thing to note is that there isn’t really a need for you to scale labels on
any of your classification problems. For regression problems, we will take a
look at how to scale labels for a regression section.
7
If the age of your applicant is over 25 and then also younger than 60,
then you can go to the next step. Otherwise, the bank will reject the
loan application.
If the first step is satisfied, then check to see if the person is salaried
or not. If the person does have a salary, then you would go to step
three. If the person doesn’t make any money (is jobless), then you
will reject the application.
If the person has a salary and is male, then you will go to step four.
If the applicant is female, go to step five.
If the salary of the male is over $35,000 per year, then you can
award the loan. Otherwise, the loan is going to be rejected.
If the salary of the female is over $45,000 a year, you can award the
loan. Otherwise, reject that application.
The rules that we have above are pretty simplistic, and we chose them at
random. In the real world, the decision tree is going to be way more complex
and there will be a lot more rules and a lot more branches on your decision
tree. There are even times when a statistical technique, such as entropy, will
be used to create these nodes. Entropy is going to refer to the impurity of
classifications in the labeled data.
To keep it simple, in a decision tree the features that result in a minimum
amount of entropy is going to be set as the root node. This helps give the
bank a starting point to go from and can ensure that the right people are given
the loan.
The Benefits of a Decision Tree
Decision trees can be very good to use when sorting through a lot of data.
They are simple and really easy to understand compared to some of the other
methods you may use with machine learning. Some of the benefits of using a
decision tree algorithm include:
Decision trees can work for many different types of problems. They
work well for classification and regression tasks which mean that
you can use them to predict continuous and discrete values.
Decision trees can be used in order to help you classify both non-
linear and linear data.
In comparison to some of the other machine learning algorithms you
may use, these decision trees are pretty fast to train.
Using the Python Scikit Library to Help Implement Decision Trees
Now it is time to use our learning libraries to put one of these decision trees
into action so we can see how it works a bit better. In this section, we are
going to try and predict the consumption of petrol (going in millions) in the
48 states of the US based on several different features. These features are
going to include the ratio of individuals with their license, per capita income,
petrol tax (cents) and paved highways (miles). Let’s go through the different
parts and see what we need to do to make our own decision tree.
Importing the Right Libraries
import pandas as pd
import NumPy as np
import matplotlib.pyplot as plt
%matplotlib inline
Importing the Data Set
When you are ready to import the right data set to use here, you need to use
the following command:
petrol_data = pd.read_csv)’D:\Datasets\petrol_data.csv’)
Data Analysis
To take a look at the data and see what is there, you will simply use the
following code:
petrol_data.head()
This is going to give you a chart with all of the numbers. These numbers are
going to correspond to the different categories that we had before.
Data Preprocessing
If you want to work with data preprocessing here, you can use the following
script to help divide out the data into feature and label set.
features = petrol_data.iloc[:, 0:4].values
labels = petrol_data.iloc[:,4].values
Then you can take this information and divide it up so that eighty percent
goes to training and then other twenty percent goes to a test set. Use the
following script to get this to happen.
from sklearn.model_selection import train_test_split
train_features, test_features, train_labels, test_labels = train_test_split
(features, labels, test_size = 0.2, random_state = 0)
Data Scaling
If we look through the data set, you will notice that the data is not scaled at
all. For instance, the feature of population_driver_license has values that are
between 0 and 1 while the paved_highways and average_income have values
that are in the thousands. Because of this, before we take the information to
the algorithm, we must go through and scale the features. To do this, you can
go through and use the following script:
from sklearn.preprocessing import StandardScaler
feature_scaler = StandardScaler()
train_features_poly = feature_scaler.fit_transform(train)features)
test_features_poly = feature_scaler.transform(test_features)
Training the Algorithm
Now that the features are scaled down, it is time to train the algorithm that we
are going to use. To implement the decision tree to do a classification, you
will need to work with the DecisionTreeClassifier from the sklearn.tree
library. The following script will make sure that the right labels and features
are passed on to the decision tree:
from sklearn.tree import DecisionTreeClassifier
dt_reg = DecisionTreeClassifier()
dt_reg.fit(train_features, train_labels
Make Predictions
Finally, it is time to make predictions using our predict method. This will
help us to get through the decision tree and see what predictions it makes
based on the data that we have available. You would need to use the
following script to see the predictions:
predictions = dt_reg.predict(test_features)
9
These algorithms are able to perform better than a lot of the other
algorithms that you can choose when working on categorical data. If
you are using this for numeric features, this algorithm is going to
assume a normal distribution.
With real-world data, you will find that features are going to be
mostly dependent on the other features. The independence
assumption that comes with the Naive Bayes algorithm can make it a
bad predictor for any set of data that has an interdependent feature.
If you are working with a categorical feature that has such a value in
test set which was not found in the training set, this algorithm is
automatically going to assign a zero probability to that instance. This
means you will need to go through and cross-validate any results that
you get when using this algorithm.
Credit Ratings
Many times the KNN algorithm will be used to help with credit ratings. First,
you will collect financial characteristics and then will compare that
information with people who have similar financial features in the database.
Just by the nature that comes with a credit rating, those who have similar
details financially would end up getting the same kind of credit rating. This
means that you could take a database that is already in existence and use it to
help predict the credit rating of a new customer, without having to go through
and perform all the calculations again.
The KNN algorithm can even be used in political science. You can
take a potential voter and class them as either “will not vote” or “will
vote” based on their characteristics and how they stack up against
others who have or haven’t voted in the past. It is even possible to
look at the information you have about the person and about others
in your database to make a prediction on which party they will vote
for.
#E XAMPLE OF INHERITANCE
#base class
class Student(object):
def__init__(self, name, rollno):
self.name = name
self.rollno = rollno
#Graduate class inherits or derived from Student class
class GraduateStudent(Student):
def__init__(self, name, rollno, graduate):
Student__init__(self, name, rollno)
self.graduate = graduate
W HEN YOU TYPE this into your interpreter, you are going to get this result:
ARGUMENTS IN PYTHON
T HE OUTPUT for this one would just list out all the names above with Hello in
front. For example, it would say:
Hello Monica
Hello Luke
Hello Steve
Hello John
W ITH THIS EXAMPLE , we have called the function with many different
arguments and these arguments are going to be wrapped up in a tuple long
before they move into the function. Then, when we are in the function, we
used the for loop to help us recover all the arguments without needing to
write out the code a bunch of times.
As you can guess by this point, the functions found in Python have a lot of
different features that are going to make the life of a programmer to Python a
lot easier. While some of these do have similar capabilities compared to other
programming languages, many of them are unique to using Python.
The extras for these can sometimes make the function easier to use. For
instance, it can help take out some of the noise and bring clarity to the
intention of your callers. With these, some of the subtle bugs that show up in
some codes, and which are sometimes hard to find, can be reduced.
3
I n real life, name conflicts can show up all the time. For example, think
back to your days at school. How many times did you meet more than
one person with the same name? Any time that a teacher asked for a
particular student, most of the other students would ask about which one they
were talking about. The teacher would have to give out the last name or some
other addition to the name to make sure they got the right one.
It would be easiest if everyone had a special name that no one else had. This
would help to get rid of any confusion and you wouldn’t have to go through
and provide additional information to get the right person. But since you
can’t convince parents to pick certain names for their children, it is kind of a
hard task to accomplish.
This can be a similar issue when you are dealing with a programming
language. When a programmer is writing a short program that doesn’t have a
lot of dependencies on outside information, it is easier to provide relevant and
unique names to all the variables. But, when there are thousands of lines in
the program and some outside modules are loaded up, it becomes harder to
do this. Namespaces can help make this process a bit easier and will ensure
that you don’t run into trouble with your code.
The Meaning of Namespaces
First, we need to take a look at what a namespace is and why it is so
important. A namespace is going to be a system of making sure that all the
names in a program are unique and that you are able to use them as the
programmer without any conflict. There are a few different types of
namespaces that you can use including:
T he next topic that we are going to talk about is how to handle and
raise your own exceptions. As you work on your codes, you will find
that there are certain exceptions that the program already brings out,
and then there are some that you will purposely add into the program to make
it work the way that you want. The automatic ones will be found in the
Python library. For example, if the user tries to divide by zero in your code,
the Python language won’t let this happen. But if there is a particular
exception that you would like to raise, or something that you don’t want your
user to be able to do, you can add that in as well.
Now, the first type that you can work with is an exception that your compiler
is able to recognize all on its own. If the user does one of these things, then
the program won’t let it finish. This could be if you add in an improper
statement to the code, or you misspell a class so the compiler has trouble
finding it. Or, it could be when you try to divide by zero. These are a few of
the examples of exceptions that the compiler can raise automatically for you.
As a programmer, it is a good idea to know some of the different exceptions
that are already found inside the Python library. This helps you to know what
to add into the code, and when an exception may turn up for you. Some of the
exceptions, and their keywords, that will come up while you do coding
include:
P ython generators are functions that will help you create sequences of
results. These generators are able to maintain their local state so that
the function will resume where they left off whenever it has been
called more than one time. You can think of your generator as a powerful
iterator. The function state is maintained with the keyword when you use
“yield”. In Python, this is similar to using ‘return’, but there are some big
differences that we will explore as we go through this guidebook.
How These Generators Work
The best way to see how a generator will work is to take a look at a simple
example.
# generator_example_1.py
D EF NUMBER G ENERATOR ( N ):
Number = 0
While number < n:
Yield number
Number + = 1
MY G ENERATOR = NUMBER G ENERATOR (3)
print(next(myGeneartor))
print(next(myGenerator))
The code that we have above is going to define a generator for you with the
name ‘numberGenerator’ that gets the value ‘n’ as the argument, before you
go through and define it using a while loop to help with a limit value. In
addition, it is going through and helps you define a variable that has the name
of ‘number’ and then it assigns a zero value with this.
When you call in your instantiated generator using the ‘myGenerator’ with
the method ‘next()’ in it, it is going to go through and run your generator
through the code until the initial ‘yield’ statement. For this example, it is
going to return 1. Even when you get a value returned to you, your function is
going to tend to keep the variable ‘number’ value for when you call up your
function next, and then it grows in value by one. What this means is that it is
able to start up again right where it left off at the next call of the function.
If you decided to call up this generator once more following what we have in
the code already, you are going to raise an exception here. It will say
“StopIteration” since it has finished up and reverted from its internal while
loop.
This may seem a bit silly at first, but it is a useful functionality since you are
able to use these generators to help you create some iterables as you go along.
For example, if you went through and used the ‘list()’ to wrap
‘myGenerator’, you would then get an array of numbers such as [0, 1, 2] back
rather than your generator object. In some cases, this is going to be a bit
easier for you to work with.
What is the Difference Between Yield and Between
Now, there are times when you will use the ‘return’ keyword. You will use
this to get a return of a value from a given function and when this happens,
the function is going to get lost from its local state. This means that when you
try to call on a function again, or a second time, it is going to have to start
fresh from the first statement.
On the other hand, you can use the ‘yield’ to help keep the state between the
different functions. This method is going to ensure that you are able to get the
function to go back to where it was when you first called it up. So, the one
that you choose will depend on where you want the function to end up when
you are done. Do you want it to go back to its original place where you called
it up, or do you want it to go all the way back to the beginning?
Using Return in a Generator
The generator can use the statement for ‘return’ but only when there isn’t a
return value. The generator will then go on as in any other function return
when it reaches this statement. The return basically tells the program that you
are done and you want it to go back to the rest of the code. Let’s take a look
at how you can change up the code to use these generators simply by adding
in an if else clause so that you can discriminate against any numbers that are
above 20. The code you would use for this includes:
if n < 20:
number = 0
while number < n:
yield number
number +=1
else:
return
print(list(numberGeneator(30)))
This particular example is going to show that the genitor will be an empty
array. This is because we have set it so that it won’t yield any values that are
above 20. Since 30 is above 20, you will not get any results with this one. In
this particular case, the return statement is going to work in the same way as
a break statement. But, if you go through this code and you get a value that is
below 20, you would then see that show up in the code.
Some more about Generators
Remember that a generator is going to be a type of iterator, one that your
code has defined with the notation of a function that is easier for you to use.
When you use these generators, you are working with a type of function that
will give you a yield expression. These won’t be able to give you a return
value though. Instead, when they are ready for you to use them, they are
going to just give you the results. The process to remember the context that
you need to get a generator is an automated process in this language. The
context is going to be the value of your local variables, the location of the
control flow you want to use, and even more.
Now, there are some options when it comes to calling up the generator that
you want to use. If you call it with the help of __next__, the yield you are
going to get will show up at the next iteration value in the line. You can also
choose to work with __iter__, which is one that will automatically implement
in your program, and it tells the program that it should take that generator and
use it in the best place where an iterator is needed.
As a programmer, there are a few options that you can choose from when it
comes to working on these generators. Some of the options that you can use
include:
Any time that you have a lot of data that you want to process
through. Generators will be useful here because they can offer
calculation on demand. This is a very common method to be used in
stream processing.
You may also use stacked generators in the process of piping. This is
the same way that you would use Unix pipes. Put differently, you are
able to use these generators in order to pipeline a series of operations
to make things easier.
7
T hese itertools are just a collection of tools that you can use to help
you handle the iterators that occur in your code. Iterators, to put it
simply, are just data types that can be used in one of your for loops.
The list is one of the most common iterators that are used in Python, but there
are many others that you may come to rely on as well.
Before you are able to get started with these, you must take the time to bring
in the itertools module. At the same time, it is often helpful to go through and
import your operator module. This is not always necessary, but you will find
that with many of your own personal codes, the operator module can be
handy to have around.
The itertools module is going to hold onto a bunch of functions. We are going
to spend some time talking about some of the different things that you can do
in your code with these itertools and why they are so good for creating strong
and powerful codes.
Itertools are really cool things that you can work within the Python language.
Even though this has a very technical sounding name, and it is often
something that is not emphasized much in beginner materials on Python,
these itertools will be a type of built-in package that you can use in your
programs to get more power out of them. As you work with this topic, the
biggest barrier that there is for this, is that Python has many methods that are
going to perform tasks that are very similar. This chapter is going to show
you some of the different methods that you can use with itertools in Python.
chain()
The first method we are going to take a look at is the chain(). This method is
going to help you provide a list of lists, iterables, and tuples and it will join
them together for you. Think about when you were younger and you would
use some tape to join pieces of paper together. This is the same process that
you will use to help you do the chain() method in Python.
count()
Let’s suppose that you want to conduct a sensitivity analysis of a very
important business simulation. This simulation is going to be about how the
cost of your tool is ten dollars, but over the next few months, you hope that
the demand for that tool is going to explode and you want to make sure that,
even if it costs you a bit more to keep up with demand, you will still make a
profit. You will then want to create a list of theoretical costs passing it to the
‘magic_business_simulation’. You may use something that looks like the
following.
[(i * 0.25) + 10 for I in range (100)]
>>>[ 10.0, 10.25, 10.5, 10.75, …]
This is not bad at all. It is sometimes hard to read through the chain, but it
shows that the syntax is not that hard. At this point, you may wonder how the
function that you just did is going to know when to stop. The answer is that it
won’t stop. Just like with some of the other methods that you may do in
Python, itertools will generate infinitely until you add in a break to help make
them stop.
Another thing to note is that the itertools are similar to iterables. You know
that having an infinite amount of iterables could be scary because you don’t
know when to stop. We are going to take a look in a moment at one of the
itertools that you can use to help you stop this process so you don’t get stuck
in an infinite loop.
ifilter()
This method is going to just need an easy invocation of the following syntax
to get it to work.
print list(intertools.ifilter(lambda x: x % 2, numbers))
>>>[23, 7]
This is the itertool you will want to work with when it is time to filter out
some of the information that you have in your code. If you are trying to
eliminate some things, or you want to search for something inside the code,
you would use a code that is similar to what we have above.
compress()
This is a method that a lot of programmers like to use. It is a perfect addition
to your code. Two lists given (a and b) return the ‘a’ elements and then the
corresponding elements of ‘b’ for it if (‘a’) are true.
imap()
This is the last method that we are going to look at for this chapter and we are
going to use it as a simple addition for those that already know how to use
some of the other programming options such as ‘map()’ ‘map and ‘filter’.
When you pass it as a function, it is going to grab the arguments
systematically and then throw them at the function to return the results.
These are the most important of the itertools that you can use when you are
creating some of your own codes. They are simple to use and you are going
to love how much they can add to your own code. Practice using some of
these itertools and find some codes that already have them in place to see just
what they are able to do for you.
8
def innerFunction():
print(text)
INNER F UNCTION ()
if __name__ == '__main__':
outerFunction('Hey!')
As you can see here, the innerFunction() part of the code is something that
your outerFunction can access, and you can use it as much as you want as
long as you are in that function. But if you leave this, or go to another part of
the code, you will not be able to access that innerFunction() part. In this case,
the innerFunction() is going to be the nested function, which will use text as
its non-local variable.
What are Closures?
Now that we have taken a look at a nested function and those non-local
variables, it is time to talk a bit more about closures. A closure is going to be
an object of a function that remembers values that are in your enclosing
scope. This happens even if those objects are not showing up in the memory.
Your closure is going to be a record that can help store a function along with
its environment, a mapping associating all the variables in the function that
are free. Remember that the variables are going to be used locally, but they
are then defined in your enclosing scope. It is able to do this with the value,
or the reference that you bound to the namespace of the closure when you
first created it.
A closure is going to be a bit different than your plain functions. These
closures will make it so that the function is able to access these variables that
you captured by going through the closure’s copies of the values or
references. This can happen, even when that particular function is called up
outside of its own scope. A good example of a code that you can write out
that works with this is the following:
# Python program to illustrate
# closures
def outerFunction(text):
text = text
def innerFunction():
print(text)
if __name__ == '__main__':
myFunction = outerFunction('Hey!')
myFunction()
Take a moment to type this code into your compiler and see what happens.
What you should be able to observe from this code is that the closure is there
to help you call the function up, even when you are not in the right scope.
The function known as innerFunction has its scope present only in your
outerFunction. But when you use one of these closures, like we did before,
you are able to extend the scope so you can call it up anywhere that you
would like.
The code above did the same thing that we did originally, make it so that the
nested function is called up only inside its original function. But if you want
to be able to call up the function at any point, even when you are outside its
scope, you would need to use a code like this one.
# Python program to illustrate
# closures
import logging
logging.basicConfig(filename='example.log', level=logging.INFO)
def logger(func):
def log_func(*args):
logging.info(
'Running "{}" with arguments {}'.format(func.__name__, args))
print(func(*args))
# Necessary for closure to work (returning WITHOUT parenthesis)
return log_func
add_logger = logger(add)
sub_logger = logger(sub)
add_logger(3, 3)
add_logger(4, 5)
sub_logger(10, 5)
sub_logger(20, 10)
If you want to see the difference between these two codes, all you need to do
is bring up your compiler and try it out. You will see that the output is a bit
different, which helps you to see how the closure can help you out with your
code, so you can reach what you need regardless of whether you are in the
right scope or not.
Basically, you need to decide whether you want to be able to reach the nested
function only in its original function, or if you would like to be able to reach
it no matter where you are in the code. If you are going for the first option,
then you will need to just use a code that is similar to what we first talked
about. If you want to be able to reach the function outside of its scope, then
you need to make sure that you add in a closure to help.
When would I use Closures?
There are several different reasons why you would like to use a closure in
your coding. First, these closures are often used as a type of callback
function. This means that they are able to provide you with some sort of
hiding for the data. In your code, this is going to help you reduce the use of
global variables, which can clean up the code and can keep some of the bugs
out of it.
In addition, these closures can help with functions. If you are trying to work
with a few functions at the same time, closures can be a good and efficient
way to deal with these. But, if you do plan to have a lot of different functions
in the code, then you will want to go with a class.
It will often depend on your end result when it comes to whether you want to
use closures or not. Some programmers find that they don’t need to be able to
access their nested functions outside of its current scope, so there is no reason
for them to use these closures. Others may need it to show up in other places
as well, or to work outside its original scope, and a closure can make that
easier to accomplish for them.
9
W hen you are working with Python, one thing that you will notice
is that its library is really amazing. This library is going to be
full of things that are called regular expressions, and it is the part
that is responsible for handling your searches while also extracting different
tasks for you behind the scenes.
These regular expressions are going to be used in your coding to help you
filter out different texts or text strings. It is possible to check and then see if a
string or a text that is already present inside your code is going to match up
with a regular expression as well. When it comes to working with regular
expressions, you will be able to stick with a similar syntax no matter which
language you choose. Learn how to do this with Python, and you will be well
on your way to using it with other coding languages as well, if you choose.
At this point, you are probably wondering what regular expressions are and
how you would be able to use them properly in your coding. A good place to
start with this process is to bring out the text editor and have your program
locate a word that was spelled in two different manners in your code. We are
going to help you do a few things with the use of regular expressions to help
clear out confusions that would come with this problem.
Of course, regular expressions open up a world of things that you are able to
do with your code. This is why it is so important to learn how to use them the
proper way. If you want to start using these in your code, the first step is to
import your expression library. You can do this when you first start up the
program because you will probably use it quite a bit.
There are many regular expressions that you can choose to work with when
writing out statements, and if you know what they can all do and how they
work, it makes a big difference in what you can do with your code. Let’s take
some time to look at the most common regular expressions that you can work
with, how they work, and how are you going to get them to perform correctly
inside your own codes.
Basic Patterns
One thing that you are going to like about regular expressions is that you
won’t be stuck just using them for specific fixed characters. They can also
help you to watch out for some patterns if you need them to. Some of the
patterns that are common with regular expressions include:
The unit that you use for testing should keep its focus on a tiny bit of
the functionality. Then it should work to prove that part is correct.
Each of the units that you are doing need to be fully independent.
Each test must have the ability to run alone, as well as with the test
suite, regardless of what order it is called up with. The implication
with this rule is that your test needs to be loaded up with a fresh
dataset and it needs to be able to clean up a bit afterward. You can
handle this with two methods known as the teardown() and setUp()
methods.
When creating a test, you need to work on creating one that is able to
run fast. If you find that even one of your tests takes more than a few
milliseconds to run, then the whole process is slowed down, or the
tests won’t be able to run as often as you need. There are some times
when the tests just can’t be as fast as you want because they need to
work with some complex data, and then you need to load up the
structure each time you run the test. You should keep some of these
heavier tests in their own area so that the other tests can run when
needed.
You need to fully understand all the tools that come with your
program and learn how to run either a test case or a single test. Then,
when you are developing your own function inside a module, you
can do these tests as often as possible. Set it up so that this happens
automatically any time that you save your code.
You should always go through and run a full test suite right before
you get started with a session of coding, and then consider running it
again when you are done. This helps you feel more confident that
nothing was broken when you worked on the code.
If you are working on a development session and then have to leave
right in the middle, you can write in a broken unit test about what
you are planning on working on next. Then, when you come back to
the work, you will still have a pointer there where you can get right
back on track.
When you are doing a code and trying to debug it, you should work
on a new test that is responsible for finding the bug. This isn’t
always something that is possible, but the bug catching tests are
going to be very valuable when you do your project.
When you are testing out a function, make sure that you use
descriptive and long names. The style guide for this point is often
going to be a bit different than what you would do when running a
code, for those you want names that are kind of short. The reason
that your testing functions need to be longer is because you want
them to display on the screen when the test fails. When you have
them as descriptive as possible, it is easier to tell what is going on in
the code.
When you are working on the code and you find that something goes
wrong or you need to go through and change something, and if the
code has gone through a good set of tests, then you are able to rely
on the testing suite to help fix your problems. The testing code needs
to be read as much, and sometimes even more than running a code.
This isn’t always a bad thing to rely on the testing code. But you do
need to be careful and make sure that the testing code you work with
is secure and that it will actually be able to catch any of the problems
that come up inside your actual code.
Another way that you are able to use this testing code is as a type of
introduction to some new developers. When you have someone who
will work on the code base, having them read through your testing
code is a good place for them to start. If they have to add in some
functionality, then they need to go through and add in a test to make
sure that this functionality is not something that is already there and
just needs to be adjusted to work properly.
Testing your program is a great way to make sure that it is going to work the
proper way and that there isn’t going to be any major bugs or problems that
will show up in it when someone else tries to use the code. Use the assert
statement and the steps that are above, and you will be able to get your code
up and running the way that you want.
12
HOW TO WORK WITH LOOPS IN YOUR PYTHON
CODE
T he next topic that we are going to discuss inside Python is the idea of
a loop. These are going to be very important to your code and can
work well with some of the conditional statements that you will do in
your coding. Loops can clean up your program, can help you get a lot done in
a few lines of code, and is a wonderful way to make the code really intense
and powerful without having to learn a lot of new things.
These loops are going to be really helpful when you are writing a code where
you need that particular program to repeat something, at least a few times,
within the code, but you don’t want to make the code messy and write out
those lines a bunch of times. It isn’t a big deal to go in and write the line two
or three times, but if you want something to repeat one hundred times, or
infinite amount of time until a specific result is reached, then the code can be
tedious and messy if you write them all out. A loop will be able to handle this
for you, and you can get it all done in just a few lines.
For example, let’s say that if you are working on a code where you want all
the numbers from one through ten listed out, you don’t want to write out so
many lines of code to tell the compiler to do this. These loops can do the
work for you, making the program repeat itself until a certain condition (one
that you can set out) is met.
While this may sound a bit complex, these loops are actually really easy to
work with. These loops are there to work with your compiler, telling it that it
should just repeat that same block of code over and over again. It will do this
until a condition that you inserted is met. If you want your code to be able to
count up from one to ten, you would just tell the compiler that you want it to
stop once the output is higher than ten. We will take a look at a few examples
of codes that work like this so you can get a better idea of how this works.
Of course, when writing a loop condition, you need to be careful about
getting the condition set up. If you don’t set up your condition from the
beginning, then the program will just keep reading the code over and over
again, getting stuck in a continuous loop. You need to have a condition or a
break in your code to help it stop and move on to the next thing the program
should do.
With the traditional methods of coding that you may have used in the past,
you would have to write out every line of code. Even if there were some
similar parts of the code that were the same, or you were basically retyping
the same piece of code over and over again, that is how you had to do it as a
beginner because that is the only way that you knew how to do things.
With the help of these loops, you can get rid of that way of thinking. You can
combine a lot of lines of code into just a few and instead convince the
compiler to read through that same line as many times as you need. If you
need it to do it 100 times, then that is what the compiler will do. With one
line of code, thanks to these loops, you can get a ton of things done without
having to write out 100 lines, or more, of code.
With that said, there are a few types of loops that are available for you to try
out. The one that you pick will depend on what you want to happen inside the
program and how many times you want the code to go through a loop. Let’s
take a look at some of these loops, including the for loop, the while loop, and
the nested loop.
The While Loop
The first type of loop that you can work within your Python code is known as
the while loop. The while loop is the type that you will use if you want to
make sure that the code goes through a cycle a predetermined number of
times. You can set this number of times when you write the code to make
sure the loop goes for as long as you would like.
With the while loop, your goal is not to make the code go through its cycle an
indefinite amount of times, but you do want to make sure that it goes through
for a specific number of times. If you are counting from one to ten, you want
to make sure it goes through the loop ten times to be right. With this option,
the loop is going to go through at least one time and then check to see if the
conditions are met or not. So, it will put up the number one, then check its
conditions and put up the number two, and so on until it sees where it is.
To give us a little bit better understanding on how these loops work, let’s take
a look at some sample codes of the while loop and see what happens.
counter = 1
while(counter <= 3):
principal = int(input(“Enter the principal amount:”))
numberofyears = int(input(“Enter the number of years:”))
rateofinterest = float(input(“Enter the rate of interest:”))
simpleinterest = principal * numberofyears * rateofinterest/100
print(“Simple interest = %.2f” %simpleinterest)
#increase the counter by 1
counter = counter + 1
print(“You have calculated simple interest for 3rd time!”)
Before we move on, take this code and add it to your compiler and let it
execute this code. You will see that when this is done, the output is going to
come out in a way that the user can place any information that they want into
the program. Then the program will do its computations and figure out the
interest rates, as well as the final amounts, based on whatever numbers the
user placed into the system.
With this particular example, we set the loop up to go through three times.
This allows the user to put in results three times to the system before it moves
on. You can always change this around though and add in more of the loops
if it works the best for your program.
What is the for loop, and why is it different from the while loop?
The while loop that we discussed above is a great one to use. It is going to
work in many of the situations where you want to work with a loop and it
will often be enough. But there are times when you will need something a bit
different, and that is when the for loop can be useful. The for loop is actually
the one that is considered the more traditional method to use loops in coding,
but it is also one that you can use with many of your codes.
With the for loop, you will have it set up so that the user isn’t the one who
goes in and gives the program information that determines when the loop will
stop. Instead, the for loop is set up to go over the iteration in the order that
things show up inside your statement, and then this information is going to
show up on your screen. There isn’t any need for input from an outside force
or user, at least until it reaches the end. An example of the code that you can
use to work on a for loop includes:
# Measure some strings:
words = [‘apple’, ‘mango’, ‘banana’, ‘orange’]
for w in words:
print(w, len(w))
When you work with the for loop example that is above, you are able to add
it to your compiler and see what happens when it gets executed. When you do
this, the four fruits that come out on your screen will show up in the exact
order that you have them written out. If you would like them to show up in a
different order, you can do that, but then, you need to go back to your code
and rewrite them in the right order, or your chosen order. Once you have
them written out in the syntax and they are ready to be executed in the code,
you can’t make any changes to them.
The Nested Loop to Finish It Out
And the final type of loop that you can choose to work with is known as the
nested loop. This one is going to work slightly different than the while loop
and the for loop, but there are times when it is helpful to use this kind of a
loop. When you do work with one of the nested loops, you are taking one
loop and then placing it so it is inside of another one. Then both of these
loops are going to keep on running until they are done.
This may seem like a silly thing to add into your code, but there are many
times when you write out a code that will need this. For example, you may be
working on a code where you need to write out your own multiplication
table. Maybe you want to be able to write it so it goes from 1 all the way to
10 and has the answers for each one in there.
This would be a huge amount of code if you wrote out each line to tell the
program how to behave. And you can certainly do that if you want to waste
some time practicing your code writing. But a better method to use to make
this work, a way that would get it done in relatively few lines of code and
save you time includes the following:
#write a multiplication table from 1 to 10
For x in xrange(1, 11):
For y in xrange(1, 11):
Print ‘%d = %d’ % (x, y, x*x)
When you got the output of this program, it is going to look similar to this:
1*1 = 1
1*2 = 2
1*3 = 3
1*4 = 4
All the way up to 1*10 = 10
Then it would move on to do the table by twos such as this:
2*1 =2
2*2 = 4
And so on until you end up with 10*10 = 100 as your final spot in the
sequence.
Go ahead and put this into the compiler and see what happens. You will
simply have four lines of code, and end up with a whole multiplication table
that shows up on your program. Think of how many lines of code you would
have to write out to get this table the traditional way that you did before. This
table only took a few lines to accomplish, which shows how powerful and
great the nested loop can be.
The loops are great options to add into your code. There are a lot of reasons
when you would need to take a loop and add it inside your code. You will be
able to use it as a way to get a lot of coding done in just a few lines, and a
way to clean up the code so that you can still get the same thing done without
writing out too much. The compiler is set up to keep reading through the loop
until the condition that you set is no longer valid. This can open up a lot of
things that you are able to do with your code, while also keeping things clean
and manageable all at the same time.
13
WHEN TO USE USER-DEFINED FUNCTIONS IN
YOUR CODE
These functions can also be very useful. You can use them to help
with anything that you want from writing out specific logic in
business to working on common utilities. You can also modify them
based on your own requirements to make the program work
properly.
Now that we know a little bit more about the basics of a user-defined
function, it is time to look at some of the different arguments that can come
with these functions before moving on to some of the codes that you can use
with this kind of function.
The Function Arguments You Can Use
When you are working with Python, these user-defined functions have the
ability to take on four different argument types. These types, and their
meanings are going to be pre-defined and the developer is not able to change
these. Instead, the developer has the option to follow these rules and then add
in some things to make custom functions. There are four types of arguments
that you can use here and the rules that go with them.
1. Declare your function. You will need to use the “def” keyword and
then have the name of the function come right after it.
2. Write out the arguments. These need to be inside the two
parentheses of the function. End this declaration with a colon to keep
up with the proper writing protocol in this language.
3. Add in the statements that the program is supposed to execute at this
time.
4. End the function. You can choose whether you would like to do it
with a return statement or not.
An example of the syntax that you would use when you want to make one of
your own user-defined functions includes:
def userDefFunction (arg1, arg2, arg3, …):
program statement1
program statement2
program statement3
….
Return;
14
WORKING WITH MEMOIZATION IN PYTHON
RETURN ANS
The basic logic that comes with this is pretty obvious. The cache is going to
be the results dictionary of your previous calls to the
fib_default_memoized(). The ‘n’ parameter is the key. It is going to be the
nth Fibonacci number. If this is true, then you are done. But if it is not true,
then you have to take the time to evaluate this as the version of the native
recursive and keep it in the cache before the return of the results.
The thing here is ‘cache’ is the function’s keyword parameter. Python is
usually going to evaluate the keyword parameters only one time, which is
when you import the function. This means that if there are any issues with
mutability in your parameter, it is only going to be initialized one time. This
is usually the basis of small bugs that happen in the program, but in this case,
you are going to mutate your parameter in order to take advantage of it.
Manual Memoization: Objects
Some programmers who use Python argue that going through and mutating
your formal parameters is a bad idea. For others, especially those who like to
work with Java, the argument for this is that all functions that have state need
to be turned into objects. An example of how this would look like in your
compiler includes the following:
class Fib():
CACHE = {}
RETURN ANS
If you are doing this one, the __call__ dunder method is going to be used to
make the Fib instances behave like a function. The Cache is shared by all the
Fib instances because that is its class attribute. When you are looking at
Fibonacci numbers, this is a desirable thing to do. However, if your object
made calls to a server well defined in the constructor, and the result was
going to depend on the server, this may not be a good thing. Instead, you
would need to move it over to an object attribute by taking it right to the
‘__init__’ part. Either way, you will get the speed up process from this.
Manual Memoization: Using ‘Global”
Another thing that we can work on with this process is manual memoization
with the help of the ‘Global’ function. You can go through and evade your
default parameters and some of the hacky mutations simply by adding in
‘global’. This is one thing that sometimes gets a bad reputation with
programmers, but it is a good one to learn how to use. Many times you would
use the global here declaration because it works better, but you would use the
same kind of coding that we had above.
Decorators
The last thing we are going to talk about here is a decorator. This is simply a
higher order function. What this means is that the function is going to be the
argument and then it will return to you another function. When it comes to
these decorators, the returned function is going to usually be the original
function, which has been augmented to be more functional. An example of
this would be to make decorate that is going to allow you to print text each
time a function is called. The way that you can write this out is:
def output_decorator(f):
def f_(f)
f()
print(‘Ran f…’)
return f_
You can take the decorated version to replace the f. You just need to do
‘F=output_decorator(f)’. Just by calling the f(), you are going to get your
decorated version. Python is going to make this even easier if you just use the
following syntax to help.
@output_decorator
def f()
#...define f…
Now, if you go through and try to do this, you will find that the result from
the output_decortor is not that motivating. But you can go beyond this and
augment the operation of the function itself. For example, you could include
a type of cache with the decorator and then intercept the calls to the function
if needed.
But if you try to write out your own decorator, there are times when you get
confused in the particulars of the argument passing, and then getting really
stuck with the introspection of Python when you figure this out. Introspection
is the capacity to determine when you run the program, the type of an object.
This is one of the strengths of the Python language, but if you are using a
decorator, things can become messy.
If you are going to use one of the decorators, be careful with what you are
doing here. You want to make sure that you understand how to make them
work and that you actually need to use it in your code. Otherwise, you may
run into some issues with the code, and it may not interpret in the compiler
the right way.
CONCLUSION