Python
2 Books in 1: The Crash Course for Beginners to Learn Python Programming, Data Science and Machine Learning + Practical Exercises Included (Artificial Intelligence, Numpy, Pandas)
Book 1 Python for Beginners
Book 2 Python Data Science
Ó Copyright 2019 by Aaron Khan – All rights reserved.
This eBook is provided with the sole purpose of providing relevant information on a specific topic for which every reasonable effort has been made to ensure that it is both accurate and reasonable. Nevertheless, by purchasing this eBook, you consent to the fact that the author, as well as the publisher, are in no way experts on the topics contained herein, regardless of any claims as such that may be made within. As such, any suggestions or recommendations that are made within are done so purely for entertainment value. It is recommended that you always consult a professional before undertaking any of the advice or techniques discussed within.
This is a legally binding declaration that is considered both valid and fair by both the Committee of Publishers Association and the American Bar Association and should be considered as legally binding within the United States.
The reproduction, transmission, and duplication of any of the content found herein, including any specific or extended information, will be done as an illegal act regardless of the end form the information ultimately takes. This includes copied versions of the work, both physical, digital, and audio, unless express consent of the Publisher is provided beforehand. Any additional rights reserved.
Furthermore, the information that can be found within the pages described forthwith shall be considered both accurate and truthful when it comes to the recounting of facts. As such, any use, correct or incorrect, of the provided information will render the Publisher free of responsibility as to the actions taken outside of their direct purview. Regardless, there are zero scenarios where the original author or the Publisher can be deemed liable in any fashion for any damages or hardships that may result from any of the information discussed 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 continued 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 for Beginners
The Crash Course to Learn
Python Programming in 3-Days (or less).
Master Artificial Intelligence for Data
Science and Machine Learning + Practical
Exercises.
Table of Contents
Chapter 1 Data Types and Variables
Chapter 4 Types of Machine Learning
Introduction
In python programming, the English language is mainly used in coding many keywords. The mastery of these keywords means knowledge of the fundamental aspects of python programming. However, before delving into these primary keywords, you have to understand the basic concepts associated with Python. These concepts are necessary to understand every other aspect of the scripting language. Below, a detailed outline is given in these basic concepts.
Properties:
Python is typically typed in an implicit and dynamic format; hence, there is no requirement to declare variables. These types are enforced, and the variables are sensitive to cases. For instance, bar and VAR are considered to be two distinct variables in themselves. There is no definite array of characters used to terminate statements in Python. The use of indentations specifies blocks; thus, to begin a block, you indent, and to end it, you de-dent. Any statement which expects a level of indentation is concluded using a colon sign. The sign (#) is used in each line to add comments. In making multi-line comments, multi-line strings have to be used. Assigning values are done using the sign (=), and equality testing is carried out with the use of two signs of equality (==). Values can be incremented or decremented with the value on the right side using the operators (+=) and (-=). Operations of this sort can also be done on strings as well as other data types. Multiple variables can also be used on a single line.
Variables:
Consider variables to be a word that codes, stores, or contains a value. In python programming, defining a variable and assigning a value to it is a relatively straightforward process. For instance, if you want to store a number, you can assign it to a variable. You could assign 5 to a variable named “five.” You can assign a value to any variable of your choice. The variable “two” stores the integer 2, and “some_number” is used to store 10000. Asides integers, booleans (True or False), float, strings, among others, constitute the data types used in python programming.
• Looping and Iterator:
There are several forms of iteration in python programming, but in this segment, only two would be talked about. — “for” and “while.”
• While Looping:
While the statement entered is True, the code passed into the block would be run. The code would proceed to print numbers 1 to 10. A loop condition is needed to run a while loop, and should it continue being True, iteration continues. Let’s consider an example of a variable “num.” When NUM is 11, the loop condition evaluates to False. That is, iteration would continue until it is set to False.
• For Looping:
When a variable “num” is passed to the block, the “for” statement iterates it. The code would print similarly as the “while” code from numbers 1 to 10.
Quotation:
String literals are denoted by single (‘), double (“) or triple (‘’’ or “””) quotes in Python, provided the type of quote used to begin the string is used to end it as well. In spanning the string across multiple lines, the three quotes (”’ or “””) is usually used. Let’s consider an example below;
word = ‘word’
sentence = “This is a sentence.”
paragraph = “””This is a paragraph. It is made up of multiple lines and sentences.”””
• Python Identifiers:
A python identifier refers to a name used in showing the identity of a function, variable, class, object, or module. Identifies typically begin with a letter from a to z, or A to Z or an underscore (_) closely succeeded by zero or more underscores, digits (0 to 9) and letters. In Python, punctuation characters like modulus (%), at (@) and the dollar sign ($) are not allowed as identifiers. Moreover, as was earlier discussed, Python is case sensitive and would interpret Manpower and manpower as two distinct identifiers in themselves.
Below are some naming conditions for Python identifiers:
All class names begin with a first letter in the uppercase while other identifiers begin with lower case letters.
Beginning an identifier with one leading underscore is an indication of a private identifier. On the other hand, starting with two leading underscores shows that the identifier is strongly private.
Should the identifier ends with two trailing underscores, the identifier is said to be a language-defined special name.
- Suites:
Suites refer to a group of individual statements that combine to form a single block of code. Complex statements like while, if, class and def need a suite and a header line. Header lines start a statement with a keyword and are terminated by using a colon sign (:) which is closely succeeded by one or multiple lines that constitute the construct of the suite. For instance;
if expression:
Suite
elif expression:
Suite
else:
Suite
Importing:
In python programming, external libraries can be accessed by using a specific keyword; import[library]. For individual functions, you can use [libname] or [funcname] to import. Let’s consider an example below:
syntax:
import random
from time import clock
Strings:
Strings are made using single or double quotation marks. It is also possible to use quotation marks of a specific kind in a string that uses a different one. As such, the following string is valid in Python:
“This is a ‘valid’ string.”
Multi-strings are contained in single, double, or triple quotation marks. Python can support Unicode from the very start when using the syntax outlined below:
u “This is Unicode.”
In filling strings with values, the modulo (%) operator is used alongside a tuple. Each module is swapped out for a tuple item, proceeding to the right from the left. Dictionary substitutions can also be used in such cases.
- Functions:
The keyword ‘def’ is used in the declaration of functions. By assigning default values to them, optional arguments can be placed in the function declaration behind mandatory arguments. Should be named arguments arise, a value is attached to the argument name. Functions can be in a tuple, and several values can be effectively returned by using tuple unpacking. Parameters are transmitted via reference, but strings, ints, tuples, as well as other immutable types, are unchangeable. The reason being that only the memory location of the item is sent. In binding another object to the variable, the older one is taken out, and immutable types are replaced.
• Data types:
In Python, every value is assigned a specified datatype. Since every aspect of Python consists of an object, data types are in fact classes, and variables are instances or objects of these classes. Python has several data types used in its operations. Listed below are some of the basic, important ones.
Python Numbers:
Python numbers is a category constituted by floating-point numbers and integers as well as complex numbers. They are usually defined as int (integer), float (floating number), and complex (complex numbers) class in Python. To determine the class value or variable belongs in the type() function is used, and to find out if an object belongs in a specific class, the instance() function is used.
In python numbers, the length of integers is boundless and can only be limited by the amount of available memory. Also, a floating-point number can maintain accuracy up to 15 decimal places. The presence of a decimal point shows the distinction between an integer and floating points. For example, 1 and 1.0 are the same in basic maths, but in Python, the former (1) is an integer, and the latter (1.0) is a floating number. Complex numbers are usually written in the following format, x + y; where x constitutes the real part of the equation, and the imaginary part is denoted by y.
Python List:
A list refers to an ordered sequence of items. In python programming, the list is one of the widely used datatypes owing to its relative flexibility. The items contained in a list do not have to share similarities in type to be on the list. Also, the process of declaring a list is quite easy. Items that are separated using commas and are contained within brackets []. The slicing operator [] is used in extracting a specific range of items or a single item from a list. In Python, the index begins from the number 0. Lists can be mutable, meaning the value of the elements in a list is capable of being changed.
Python Tuple:
In Python, a tuple refers to an ordered sequence of items similar to the list. The distinction, however, is that unlike lists, tuples are immutable. That is, once they have been created, the elements of a tuple cannot be changed or modified. Tuples are mainly used in place of lists to write-protect data as they are much faster and are incapable of changing dynamically. Tuples are defined with parentheses () where the items within are separated using commas. Moreover, even though a slicing operator [] can also be used to extract items, the values cannot be changed.
Python Set:
A set is a collection of special items that are not in any particular order. It is defined by values contained within braces {} and separated using commas. Like sets in basic mathematics, operations can be carried out on python sets such as the intersection and union of two sets.
Every set has a unique value, and since they are unordered, they do not require indexing. In this vein, the slicing operator [] cannot be used on sets.
Python Dictionary:
Like sets, python dictionary is a collection of key-value pairs in an unordered state. Dictionaries are used when there are vast volumes of data. Dictionaries are optimized to retrieve data. To do this, the key to retrieve a specific value must be known. In python programming, dictionaries exist as items being paired in the format key: value and contained within braces {}.
Moreover, the pair must not necessarily be of the same type. That is, key and value can be different types. In the pair, the key is used to retrieve a respective value, but it doesn’t work vice versa.
Conversion between data types:
Conversion can be done from one data type to another using several type conversion functions such as str(), float(), int() among others.
In converting from float to int, the value would be truncated to make it nearer to zero.
Only compatible values can be used in converting from and to a string.
Conversion can also be done from one sequence to another sequence.
In converting to the dictionary, every element must exist as a pair.
What are iterators in Python?
Iterators exist in every aspect of python programming. They can be found implemented in generators, comprehensions, for loops, and so on, but they are hidden away from plain sight. An iterator is merely an object which can be iterated upon. That is, an iterator refers to any object which returns data at the rate of one element per time. Python iterator objects have to implement two special methods, namely, __iter__() and __next__(). Collectively, both special methods are known as the iterator protocol.
An object is only iterable if an iterator can be obtained from it. Many of the in-built containers in python programming, such as string, tuple, list, etcetera are iterables. The items of an iterator can be iterated through manually using a next() function. If at the end of an iteration process, there isn’t any more data that can be iterated, the function would raise a StopIteration.
Type Conversion:
Type conversion refers to a process of converting the value of a specific data type (string, integer, etcetera) to another data type. In python programming, there are two types of type conversion, namely:
- Implicit Type Conversion
- Explicit Type Conversion
Implicit Type Conversion:
Implicit type conversions are automatically done by Python in a process in which the system converts one data type to a different type. Since Python handles the conversion, no external involvement by a user is required. An example of implicit type conversion can be seen in the conversion of a lower data type to a higher data type, as in integer to float.
Explicit Type Conversion:
Explicit type conversion requires a user to convert the data type of an object to another data type needed of them. Predefined functions such as float(), str(), int(), among others are used in the process of explicit type conversion.
This type of conversion can also be known as typecasting; the reason being that the user is required to cast (change) the data type of the objects. Typecasting is done by assigning a needed data type function to an expression. An example of explicit type conversion can be seen in the addition of string and integer.
• Python Variables:
In Python, a variable refers to any named location used in the storing of data to memory. Variables can be seen as containers which hold data that can be modified over time throughout the programming process. For instance, number = 10. In this instance, a named variable (number) is created, and the value (10) has been assigned to it. Consider variables similar to a box used to store clothes. Moreover, those clothes are capable of being replaced at any point in time. Keep in mind that in python programming, values can’t be assigned to variables, in as much as Python provides the reference of the object (value) to the variable.
As is evident in the example, the assignment operator (=) is used to assign values to variables.
- Constants:
Constants refer to any type of variable whose value is incapable of being changed. Consider constants to be containers that hold information that cannot be replaced over time, or a box containing clothes, which once stored, cannot again be taken out and replaced. Constants have to be declared and assigned on a module in Python. Here, the module signifies a new file filled with functions, variables, and so on which are imported to the main file. In a module, constants have all their characters written in uppercases, and words are separated using underscores.
There are specific rules and naming conventions that apply for variables and constants. They are outlined below:
- Only create sensible names. For instance, vowel is a more sensible name than v.
- In declaring a variable, camelCase notation should be used. That is, it should begin with a lower-case letter. Take an example; myName.
- When necessary, capital letters should be used to declare constants — for instance, PI, G, TEMP, etcetera.
- Avoid the use of special characters such as #, %, !, $, @, among others.
- Never begin a name using a digit.
- Constants are to be imputed in python modules and are to remain unchanged.
- The names of variables and constants should be a combination of letters in lowercase (a to z), uppercase (A to Z), digits (0 to 9), or underscores (_) — for instance, limbo_case, CapWords, MACRO_CASE, etcetera.
- Literals:
Literals refer to raw data provided in a constant or variable. There exist several literals in Python. They include the following;
• Numeric Literals:
Numeric literals refer to literals that are immutable or unchangeable. They can be found under three different numerical types, namely, Complex, Float, and Integer.
• String literals:
String literals refer to any sequence of characters contained within quotes. As was earlier mentioned, a string can be included within single, double, or triple quotes.
• Character Literals:
A character literal refers to a single character enclosed in single or double-quotes.
• Boolean literals:
Boolean literals are capable of having any of these two values: True or False.
• Special literals:
There exists only one special literal in Python. That is, None. It is used to indicate a field that isn’t created.
• Literal Collections:
Literal collections are of four types, namely; Tuple literals, Set literals, List literals, and Dict literals
Data Types and Variables
You can look at variables as the nouns of the programming language. They are data or values on which you perform an action. In Python, as well as most programming languages, variables must have a data type. Essentially, they have two components. One is the type of data and the name.
Variables are needed to store values within an action. These values are of different types. For instance, they can be integers, floats, lists, strings, and so on. In this chapter, we are going to focus on all major data types that you normally use when programming an application. You cannot write a program without them.
Besides, you will go through several examples and exercises to learn how each type of variable is used. You will learn the proper syntax and understand the differences between various data types.
Python Labels
Before we dive into learning all the data types, let’s take a side step and discuss labels. Writing code involves naming variables and objects appropriately so that you can understand what you’re looking at immediately. Labels, also known as identifiers, are words represent something in such a way that it makes the code easier to read. For instance, if you’re talking about a bottle of water in your code, you shouldn’t name the variable that represents it as “var1”. That tells you nothing and it makes the entire code confusing. You would have to waste a great deal of time until you figure out which variable you’re talking about.
Whenever you name your variables, make sure they are well represented by the label and that they are unique. Do not use the same name multiple times or you will confuse yourself, and worse the program itself. Furthermore, you should avoid similar words as well. We discussed earlier how important code commenting is, however, if used in combination with proper identifiers you will not have any problems understanding your code. However, you should take note that certain words cannot be used as labels. These words are exceptions because they are part of Python’s own library of keywords that are reserved for various commands. You should read the language’s documentation to learn which words are reserved, however, some of them are: global, while, False, class, import and so on.
Keep in mind that using an IDE or even certain text editors can help you out with writing proper labels. They can’t read your mind, but they will tell you whenever you are trying to use a reserved keyword. This is another advantage of using dedicated programming tools. You won’t have to keep a sticky note with all the keywords attached to your monitor.
Finally, you should also consider the naming system you use. There are several ways you can name your variables and they are all just as valid. For instance, it is extremely common to begin the identifier with an underscore or a letter. Just make sure you don’t start with a number. Furthermore, you need to keep in mind the fact that Python is case sensitive. That means that if you have a variable called “myVariable”, another one called “my_variable” and “MYVARIABLE”, you have three different variables with nothing in common.
Now that you know a bit more about making your code as readable as possible, let’s finally get down to business and talk about variables and data types.
Introduction to Variables
In Python, the variable definition is handled in two steps. The first step is called the initialization and it refers to determining the container which is identified via a label. The second step involves the assignment, which means you attach a value to your variable and therefore determine the type of data it holds. These two steps are actually taken at the same time and the process is more of a theoretical one that you don’t really notice. Here’s how all of this can be formulated:
myVariable = thisValue
The two steps we discussed are taken through the equal operator. What we have here is called a statement, in this case, an assignment statement. When you write code, you should always keep your statements in proper order. Keep in mind that Python processes code by analyzing it from the top to bottom and then it starts over. Furthermore, you could write the statements in the same line, however, that would lead to a lot of chaos.
Now that you know what a variable is, let’s see how Python is able to determine which data type is assigned to the variable.
Python has this feature called dynamic typing, which means that it is able to automatically determine what kind of variable it is dealing with. So if you assign an integer to a variable, Python automatically knows that the variable has an integer data type. When working with other programming languages, you have to declare what type of data your variable will contain. Therefore, if it’s an integer you have to declare it first and then assign an integer value to it. This is another advantage of working with Python. All you have to do is write your code without worrying too much about the details. Let the interpreter do the heavy lifting for you. Furthermore, if you make the mistake of performing an operation on a data type that is not eligible for that operation, Python will let you know. However, there is one disadvantage when relying on this system. Because you don’t have to declare your variable’s data type, as a beginner, you might accidentally create a variable when you don’t need one, or assign the wrong data type to it. It’s not always easy to pay attention to all the variables you created, however, this problem can easily be fixed.
The best practice when writing a program is to declare all of your variables at the beginning of your project. Keep in mind that the program isn’t affected by simple assignments because you aren’t instructing the interpreter to perform any operation. If you say x is equal to 10 that’s all there is to it. It doesn’t really mean anything else. However, by keeping all of your eggs in the same basket you will be able to keep track of them. Keep in mind that this doesn’t mean you have to come up with all of your variables from the beginning. You can always return to the start of your program and declare them whenever you need to.
Strings
The string is the most basic data type, along with numbers. You have actually already used a string when you wrote your first program. The line of text you printed is considered a string. Simply put, strings are sets of characters that are defined between quotation marks. Keep in mind that text also includes numbers and punctuation marks. Even though numbers are normally classified under their own data types such as integers and floats, if you write them between quotes, they are considered textual characters part of a string.
In your first program, you had a single statement that was printed with the print function. Keep in mind that you can also print any number of statements, even in the same line, even if they are represented by several variables. This is done with one of the most popular operations you will perform on strings called concatenation. This concept is simple. All it involves is linking multiple strings together. Here’s a simple example:
charRace = “human”
charGender = “male”
print (charRace, charGender)
The output will be “human male”.
As you can see, we have two variables and each one of them holds a string. We can print both of them by separating the variables with commas when writing the print statement. Keep in mind that there are multiple ways you can do this. For instance, if you don’t want to use variables but you need to concatenate the strings, you can get rid of the commas inside the print statement. You will notice a little problem, though. Here’s the example:
print (“school” “teacher”)
The result is “schoolteacher”. What happened? We didn’t leave any whitespace. Take note that whitespace can be part of a string just as numbers and punctuation marks. If you don’t leave a space, words will be glued together. The solution is to simply add one blank space before or after one of the strings, inside the quotes.
Next, let’s see what happens if you try to combine the two methods and concatenate a variable together with a simple string.
print (charRace “mage”)
This is what you will see:
File “<stdin>”, line 1
print (characterGender “warrior”)
^ SyntaxError: invalid syntax
Congratulations, you got your first syntax error. What’s the problem here? We tried to perform the concatenation without using any kind of separator between the two different items.
Let’s take a look at one more method frequently used to concatenate a set of strings. Type the following:
x = “orc”
y = “ mage”
x + y
As you can see you can apply a mathematical operator when working with string variables. In this case, we add x to y and achieve string concatenation. This is a simple method and works just fine, however, while you should be aware of it, you shouldn’t be using it. Mathematical operations require processing power. Therefore, you are telling your Python program to use some of your computer juice on an operation that could be written in such a way as to not consume any resources. Whenever you work on a project, at least a much more complex one, code optimization becomes one of your priorities and that involves managing the system’s resource requirement properly. Therefore, if you have to concatenate a large number of string variables, use the other methods that don’t involve any math.
Numbers
Numbers, just like strings, are basic but frequently used no matter how simple or complex your program is. Assigning a number to a variable is done exactly the same as with any other data type. You simply declare it like so:
x = 10
y = 1
Don’t forget that Python will automatically know which data type we’re assigning. In this case, it identifies our values as integers. Integers are whole numbers that can be either positive or negative. It cannot contain decimal points.
Another numeric data type we mentioned earlier is the float. This is a float:
x = 10.234
Floats can be negative or positive numbers but they must have decimal points, otherwise, they’re just integers.
Finally, we have the third numeric data type, which is the boolean. This type holds only two values. It can either be true or false or in computer language 1 or 0. Booleans are normally used together with logical operators instead of mathematical operators like integers and floats.
Basic Operators
Now that you know a few data types and got used to working with variables, let’s start actually performing some handy operations. Variables that hold integers or floats can be manipulated by using the most basic arithmetic operators. For instance, you can subtract, add, multiply, and divide. Whenever you work with these operators you will create an expression instead of a statement. What does that mean? Expressions are essentially code that has to be processed by the computer system in order to find the value. Let’s take a look at some exercises you can play with:
apples = 10 + 2
bananas = 10 – 4
pears = 6 * 2
fruit = apples + bananas * pears
fruit
84
Did you perhaps forget your elementary math and expected to see 216 as the result? That’s probably because you calculate the sum in your head and then multiplied it. However, that’s not how this calculation works. Python automatically knows which rules to follow and it processes each operation in the order it is required.
As you can see, Python is capable of evaluating the expression and then deciding which blocks need to be processed before other blocks. This order that the programming language follows is called operator precedence. Always pay attention to basic mathematical and logical rules because if you don’t, Python will. If you had something else in mind for your program for instance, and you wanted the result to be 216, you need to write the operation to reflect that. In order words, you need to calculate the sum first and then multiply it.
In this example, we only worked with integers in order to showcase the most basic operators. However, if you would replace them with floats, the same rules apply.
Also, it’s worth mentioning that Python is capable of converting an integer to a float or even to a string. Any number can be converted to an integer by typing “int (n)”, or a float by typing “float (n)” or a string by typing str (objectname).
You’ll notice that these are functions because they follow the same structure as the print function which you used earlier. Once we declare the function we want to use, we need to place the value or variable or object in between the parentheses in order to manipulate it. Here’s how these basic conversions work:
float (13)
Result: 13.0
int (13.4)
Result: 13
Now that you know the basics, you should start practicing on your own. Create a few different variables and think of all the operations you can perform with them. Don’t forget that reading is not enough and you should always take the extra time to practice.
Data Structures
Sequence
Sequence is a very basic term in python that is used to denote the ordered set of values. There are many sequence data types in python: str, unicode, list, tuple, buffer, and xrange.
Tuples
A tuple consists of a number of values separated by commas. Tuples are also a sequence data type in Python, like strings and lists. We need to keep in mind that tuples are immutable. It means that they can’t be changed.
The tuples consist of the number of values separated by a comma. The tuples are enclosed in parentheses, while the lists are enclosed in brackets.
Now let us see an example:
>>> m = (14, 34, 56)
>>> m
(14, 34, 56)
>>> m[0]
14
>>> m[ 0:2 ]
(14, 34)
Tuples also have the properties like indexing and slicing. Tuples can be nested. Elements in a tuple can be grouped with ()
Now let us see an example:
i = 1
j = 2
t1 = i, j # is a tuple consists to elements i and j
t2 = (3, 4, 5) # is a tuple consists to elements 3,4 and 5
t3 = 0, t1, t2 # is a tuple consists to elements 0, t1 and t2
print t3 # result is (0, (1, 2), (3, 4, 5))
Lists
A list consists of a number of heterogeneous values separated by commas enclosed by [and] and started from index 0. Lists can be used to group together other values. Unlike Tuples, Lists are mutable in nature. In other words, they can be changed by removing or reassigning existing values. Also, new elements can be inserted to the existing ones.
Now let us see an example:
>>> a = [1, 2, 3, 4, 5]
>>> a
[1, 2, 3, 4, 5]
As strings, lists can also be indexed and sliced.
>>> a = [1, 2, 3, 4, 5]
>>> a
[1, 2, 3, 4, 5]
>>> a[0]
1
>>> a[4]
5
>>> a[ 0:2 ]
[1, 2]
>>> a[ 3:5 ]
[4, 5]
Unlike strings, lists are mutable (i.e. the values can be changed)
>>> b = [1, 2, 4, 7, 9]
>>> b
[1, 2, 4, 7, 9]
>>> b[2] = 6
>>> b
[1, 2, 6, 7, 9] # Here the index [2] is changed to 6 (the initial value is 4)
>>> b[0] = 9
>>> b
[9, 2, 6, 7, 9] # Here the index [0] is changed to 9 (the initial value is 1)
The values in the list can be separated by using comma (,) between the square bracket. Lists can be nested. List can be used as a Stack or a Queue.
For example:
list1 = [ 1, 2, 3, 4]
print len (list1) # returns 4 – which is the length of the list
list1[2] # returns 3 – which is third element in the list Starts
list1[-1] # returns 4 – which is extreme last element in the list
list1[-2] # returns 3 – which is extreme last but one element
list1[ 0:2 ] = [ 11, 22] # replacing first two elements 1 and 2 with 11 and 22
stackList = [ 1, 2, 3, 4]
stackList.append(5)# inserting 5 from the last in the stack
print stackList# result is: [1, 2, 3, 4, 5]
stackList.pop()# removing 5 from the stack Last In First Out
print stackList# result is: [1, 2, 3, 4]
queueList = [ 1, 2, 3, 4]
queueList.append(5)# inserting 5 from the last in the queue
print queueList# result is: [1, 2, 3, 4, 5]
del(queueList[0] )# removing 1 from the queue First In First Out
print queueList# result is: [2, 3, 4, 5]
Sets
A set doesn’t have any duplicate elements present in it and it is an unordered collection type. It means it will have all distinct elements in it with no repetition.
Now let us seen an example:
fruits = [‘apple’, ‘orange’, ‘apple’, ‘pear’, ‘orange’, ‘banana’]
basket = set (fruits) # removed the duplicate element apple
print ‘orange’ in basket # checking orange in basket, result is True
print ‘pine apple’ in basket # checking pine apple in basket, result is False
a = set(‘aioeueoiaeaeiou’) # create a set without duplicates
b = set(‘bcokcbzo’) # create a set without duplicates
print a # a = [‘a’, ‘i’, ‘e’, ‘u’, ‘o’]
print b # b = [‘z’, ‘c’, ‘b’, ‘k’, ‘o’]
print a & b # letters in both a and b ( A ∩ B )
print a | b # letters in either a or b ( A B )
print a – b # letters in a but not in b ( A – B )
Dictionaries
Dictionaries are the data structures in Python that are indexed by keys.
Key and values separated by : and pairs of keys separated by a comma and enclosed by { and }
Lists cannot be used as keys.
Now let us see an example:
capitals = { ‘AP’ : ‘Hyderabad’, ‘MH’ : ‘Mumbai’ }
capitals[ ‘TN’ ] = ‘Chennai’
print capitals[ ‘AP’ ]# returns value of AP in the dictionary
del capitals[ ‘TN’ ] # deletes TN from the dictionary
capitals[ ‘UP’ ] = ‘Luck now’ # adding UP to the dictionary
print ‘AP’ in capitals # checks where AP key exist in dictionary
print ‘TN’ in capitals
Numbers = {‘1’: ‘One’, ‘2’: ‘Two’}
for key, value in Numbers.iteritems() :
print key, value
Strings
In Python, a string is identified by the characters in quotes, such as single (‘’) and double (””). They can only store character values and are a primitive datatype. Please note that strings are altogether different from integers or numbers. Therefore, if you declare a string “111”, then it has no relation with the number 111.
>>> print “hello”
hello
>>> print ‘good’
good
The string index starts from 0 in Python.
>>> word = ‘hello’
>>> word[0]
‘h’
>>> word[2]
‘l’
Indices may also be negative numbers, to start counting from the right. Please note that negative indices start from -1 while positive indices start from 0 (since -0 is same as 0).
>>> word = ‘good’
>>> word[-1]
‘d’
>>> word[-2]
‘o’
The slicing in Python is used to obtain substrings, while index allows us to obtain a single character.
>>> word = ‘develop’
>>> word[ 0:2 ]
‘de’
>>> word[ 2:4 ]
‘ve’
Please note that the starting position is always included and the ending position is always excluded.
D e v e l o p
0 1 2 3 4 5 6 —- Index value
In the above example, the word is assigned a value develop. Considering the first statement word [0:2], the output is ‘de’. Here the starting position ‘d’ (0th index) is included and the ending position ‘v’ (2nd index) is excluded. Similarly, in the second statement word [2:4], the starting position ‘v’ (2nd index) is included and the ending position ‘l’ (4th index) is excluded.
The important point to be noted in strings that are Python strings are immutable (i.e. Strings cannot be changed).
There are many in-built functions available with a String. They are used for various purposes. Let’s see some of the basic ones that are most commonly used.
- Len: It is the length function that is used to calculate the number of characters present in the string.
- Lower: It will convert all the uppercase characters present in the string to lowercase letters. Therefore, after using this function, all characters in the string will be small cases only.
- Upper: It will convert all the lowercase characters present in the string to uppercase letters. Therefore, after using this function, all characters in the string will be upper case only.
- Split: It helps to split the string into parts by using a delimiter. It can be separated using spaces, new lines, commas, or tabs.
Control Flow Statements
If –else statement
The if-else statement is used to make the choices from 2 or more statements. It becomes helpful when you want to execute a particular statement based on a True or False condition.
The syntax of if statement is:
If condition:
action-1 # Indentation
Else:
action-2 # Indentation
Here the indentation is required. The actions action-1 and action-2 may consist of many statements but they must be all indented.
if <expression> :
<statements>
else :
<statements>
The example is shown below.
>>> e = 6
>>> f = 7
>>> if(e < f):
… print( ‘f is greater than e’ )
… else:
… print(‘ e is greater than f’)
…
Output: f is greater than e
def numberProperty1 ( input ) :
if input % 2 == 0 :
print input , ‘ is an Even number ‘
else :
print input , ‘ is an Odd number ‘
numberProperty1( 10 ) # result is 10 is an Even number
numberProperty1( 11 ) # result is 11 is an Odd number
Nested If
It consists of more than 2 statements to choose from.
def numberProperty2 ( input ) :
if input < 0:
print input , ‘ is a Negative number ‘
elif input == 0:
print input , ‘ is Zero ‘
else:
print input , ‘ is a Positive number ‘
numberProperty2( -100 ) # -100 is a Negative number
numberProperty2( 0 ) # 0 is Zero
numberProperty2( 100 ) # 100 is a Positive number
While Loop
The while loop will execute until the expression is true and it stops once it is false.
The syntax of while loop is:
While expression:
statement
For example:
>>> a = 1
>>> while(a < 10 ):
… print “The number is:” , a
… a = a + 1
…
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
In the above example, the block consists of print and increment statements, it is executed repeatedly until the count is no longer less than 5.
def printSeries( start, end, interval ) :
print ” \n ”
temp = start
while ( temp < end ) :
print temp,
temp += interval
printSeries( 1, 11, 1 ) # result is 1 2 3 4 5 6 7 8 9 10
printSeries( 1, 11, 3 ) # result is 1 4 7 10
For Statement
Any object with an iteration method can be used in a for a loop in Python. The iteration method means that the data can be presented in list form where there are multiple values in an ordered manner. The syntax of for loop is:
for item in list:
action # Indentation
The action consists of one or more statements and it must be indented. The examples are shown below.
For example:
>>> for i in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10):
… print i
…
1
2
3
4
5
6
7
8
9
10
>>> list = [‘a’, ‘bb’, ‘ccc’, ‘dddd’]
>>> for l in list:
… print l,len(l)
…
a 1
bb 2
ccc 3
dddd 4
The above program shows that the values of a list and its length are printed using the for loop.
Functions
A function is a block of organized and reusable code that is used to perform related activities. We can break our huge lines of programming code into smaller modules with the help of functions. It also helps in avoiding the repetition of code, as we don’t need to write the same lines of code again and again. Instead, we can write it once inside a function and then use the function anywhere in the program.
You need to make sure that the function name is unique.
Rules to define a function in Python
- In Python, a function is defined using the keyword def.
- The arguments will be placed within the parenthesis ().
Now let us see an example:
>>> def printdetails(name, age):
… print “Name:”, name;
… print “Age:”, age;
… return;
…
>>> printdetails(name = “Mary”, age = 30);
Name: xxx
Age: 30
In the above example ‘printdetails ‘is the function name and name and age are the parameters.
Syntax of user defined method
def < function name> :
[ < declaration of local variables > ]
[ < statements > ]
Now let us see an example:
Language = “Python”
def printString( input ) :
print input
def multiply ( x, y ) :
return x * y
def power( x, y):
return x ** y
printString( Language ) # returns Python
z = multiply( 10, 20 )
print z# returns 200 – which is equal to 10 * 20
print power( 10, 2 ) # returns 100 – which is equal to 10 ** 2
Accepting inputs during the runtime
raw_input() is a built-in python function provides the facility to accept input during the execution of the script
Now let us see an example:
name = raw_input( “\n Please enter your name : ” )
This statement provides a message to the user to provide input for name.
Control Statements
Break
The break statement breaks out of the smallest enclosing for or while loop.
Now let us see an example:
def primeNumberValidation ( input ) :
for x in range( 2, input ) :
if input % x == 0:
print input, ‘is not a prime number and equals’, x, ‘*’, input/x
break
else:
print input, ‘is a prime number’
primeNumberValidation( 3 )
primeNumberValidation( 14 )
Continue
The continue statement continues with the next iteration of the loop.
Now let us see an example:
def evenNumbers( start, end ) :
print “\n\nEven numbers in between “, start , ” and “, end
for n in range( start + 1, end ) :
if n % 2 != 0:
continue
print n
evenNumbers( 1, 11 ) # result is 14 is 2 4 6 8 10
evenNumbers( 10, 30 ) # result is 12 14 16 18 20 22 24 26 28
Pass
The pass is a valid statement and can be used when there is a statement required syntactically, but the program requires no action.
Now let us see an example:
while True :
pass # In condition loop press (Ctrl + c) for the keyboard interrupt
In this example, while followed by pass does not execute any statement.
There is a necessity to include at least one statement in a block (e.g. function, while, for loop, etc.) in these cases, use pass as one statement, which does nothing but includes one statement under ‘:’
Now let us see an example:
def x() :
pass # one valid statement that does not do any action
Here pass is considered a statement for the declaration of function x.
String Manipulation
We can use built-in functions to manipulate strings in python. The package “string” provides more functions on strings.
For example:
print name = “ABCD XYZ xyz”
print len(name) # It will return the length of the string name
print list(name) # It will return the list of characters in name print
name.startswith( ‘A’ ) # It will return True if name starts with A else returns False
print name.endswith( ‘Z’ ) # It will return True if name ends with Z else returns False
print name.index( ‘CD’ ) # It will return the index of CD in name
print ‘C’.isalpha( ) # It will return True if C is alpha or returns False
print ‘1’.isdigit( ) # It will return True if 1 is digit or returns False
print name.lower( ) # It will return a string with lowercase characters in name
print name.upper( ) # It will return a string with uppercase characters in name
Exception Handling
Exceptions are the errors detected during execution and these are not unconditionally fatal.
Exception blocks will be enclosed with try and except statements.
try :
<statements>
except <exception type > :
<statements>
Let’s see an example:
# Defining an exception block
try :
print ( 1 / 0 )
except Exception as excep:
print “exception : “, excep
# Defining a user-defined exception
class UserDefinedException( Exception ) :
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
# Raising a user-defined exception explicitly
try:
raise UserDefinedException( ” input is null ” )
except UserDefinedException as userdefinedexception:
print ‘userdefinedexception : ‘, userdefinedexception.value
In the above mentioned program, first (try, except, block) handles Zero division exception.
UserDefinedException is a userdefined exception to raise business exceptions in the program.
Second (try, except) block raises a user-defined exception.
The Files
In this chapter, we are going to spend some time looking at how you can deal with the input and output of files within Python. There are a lot of different things that you are able to do with this, and you will need to be able to bring all of them out at some point or another as you are working on your code. The four things that we are going to look at is how to create a new file, how to close a file, how to look for and move a file, and how to write on a file that you have already created and saved
Creating a new file
The first option that we are going to take a look at in this chapter is how to create a new file for your needs. If you would like to make a new file that you can write out code on, then you need to open up the IDLE and then choose the mode you want to use. There are going to be three modes that work with creating a file including mode(x), write(w), and append(a).
Any time that you are looking to make some changes to the file that you have opened, you want to use the write method because this one is easiest for you to use. And if you would like to open up a file and get a new string written in that file, you would also need to work with the write method. This ensures that everything goes in the right place and the characters are going to be returned by the compiler.
You will be able to use the write() function on a regular basis because it is easy and allows the programmer to come in and make any of the changes that they want to the file. You can add some new information, change up some of the information you have, and so on. To look at how the code is going to appear in the compiler when you are working with the write() function, use the code below:
#file handling operations
#writing to a new file hello.txt
f = open(‘hello.txt’, ‘w’, encoding = ‘utf-8’)
f.write(“Hello Python Developers!”)
f.write(“Welcome to Python World”)
f.flush()
f.close()
In addition to being able to create and write on a file that is brand new, there may be times when you need to go through and overwrite some of the information that you have to ensure that a new statement, or a new part, shows up that wasn’t there before. Python does allow for it, and the code that you need to use to make this happen will be below:
#file handling operations
#writing to a new file hello.txt
f = open(‘hello.txt’, ‘w’, encoding = ‘utf-8’)
f.write(“Hello Python Developers!”)
f.write(“Welcome to Python World”)
mylist = [“Apple”, “Orange”, “Banana”]
#writelines() is used to write multiple lines into the file
f.write(mylist)
f.flush()
f.close()
The next thing that we can work on doing is binary files. This is simple to do because it is going to take the data that you have and change it over to a sound or an image file instead of a text file. You can go through and change up any of the text that you want to write in Python, and then move it into the sound or image. The syntax that is going to make this possible includes:
# write binary data to a file
# writing the file hello.dat write binary mode
F = open(‘hello.dat’, ‘wb’)
# writing as byte strings
f.write(b“I am writing data in binary file!/n”)
f.write(b“Let’s write another list/n”)
f.close()
Now that we have had a chance to create a file and even turn it into a binary file if it is needed, it is time to work with opening up a file to use again, after it has been closed, of course. There are times when you will want to open up the file and make some changes or work with the text in some way or another and opening up the file will help this to happen. The code that is going to make sure that you can get this done includes:
# read binary data to a file
#writing the file hello.dat write append binary mode
with open(“hello.dat”, ‘rb’) as f:
data = f.read()
text = data.decode(‘utf-8’(
print(text)
The output that you would get from putting this into the system would be like the following:
Hello, world!
This is a demo using with.
This file contains three lines.
Hello, world!
This is a demo using with.
This file contains three lines.
And the final thing that we are going to look at doing here is how to seek out your file. This could help you to move your file over to a new location so it is easier to find and does the work that you need. For example, if you are working with a file and you find that things are not matching up the right way because you chose the wrong directory or spelled things in the wrong way, then you may need to work with the seek option to make sure that it can be fixed.
You can go through and change up where the file is located to make sure that the file ends up in the right spot and to make it easier to bring it up and find it later on if it is needed. You just have to use the right input to tell the code where to place the file, and then make the changes to do this.
Working the files in this kind of language is going to be helpful when you are trying to get things to work out the right way in your code when you want to make a new file when you want to make changes, and so much more. Learning how to use some of these files and what you can do with all of the different parts can help you to make sure your code works properly.
Types of Machine Learning
Even though this chapter will be talking about types of machine learning, you may come across some parts that we have discussed earlier in the last chapter. But this chapter goes more in-depth into this same excellent subject called machine learning.
You see, machine learning has made huge impacts by amplifying multiple elements of the business operation, becoming a diverse and reliable business tool from a science fiction fancy. With the implementation of its algorithms essential for the maintenance of competitiveness in several industries and fields, the influence of machine learning on the performance of companies is quite significant. Requiring a lot of resources, machine learning implementation into the operations of the business is a tactical measure. As a result, understanding various perks different machine learning algorithms bring to the table and your desired work for machine learning.
As you read on, not only will you know the great purpose of the algorithms of machine learning, but you will also have an in-depth insight into the machine learning algorithms’ significant types.
Types of Machine Learning Algorithms
Algorithms of supervised machine learning
For the involvement of an operation’s unswerving management, supervised learning algorithms come to play here. In this situation, while the developer place firm restrictions on the process of each algorithm, it is also the developer’s work to label sample data corpus. A favored form of this algorithm is machine learning:
- For you to feed the algorithm, you only select the type of output and samples of information.
- Including false/true or yes/no, the desire of this type of results.
It is more or less like connecting the missing links with this process from the perspective of the machine. Making predictions of unseen, unavailable, and future data based on data of labeled sample and scaling the scope of data is the primary purpose of supervised learning. Regression and classification are the two main procedures of supervised machine learning.
Classification:
In a case where the algorithm obtains a manual training when you label incoming data based on the samples of the past data, not only categorize specific types of objects accordingly but to also recognize particular kinds of objects is the process of classification. Whether to perform a binary recognition, an optional image, and character, in a situation where there is compliant or non-compliant of a bit of data to individual requirements in a true or false approach, it is in the power of the system to know how to differentiate types of information.
Regression:
For you to calculate and identify continuous outcomes is the process of regression. Not only the groupings such as widths, heights, and so on but also their values are what the system is supposed to understand, as well as the numbers and their significance.
Some of the algorithms that are widely used include:
- Support vector machines
- Linear regression
- Nearest neighbors
- Logistical regression
- Random forest
- Decision trees
- Gradient boosted trees
- Neural networks
- Naive Bayes
Use cares of supervised learning algorithms
Trend forecasting in retail commerce, stock trading, and sales, as well as price prediction, is supervised learning’s mainly universal fields of use. For the two situations, to calculate possible outcomes and assess the possibility, the algorithm utilizes incoming data. Highspot and Seismic are the platforms of sales enablement that make use of this type of an algorithm by looking for consideration while presenting scenarios from several angles.
As the sequence of ad content delivery components, ad tech operation is part of business cases for supervised learning. The algorithm of supervised learning has the role of keeping the spending of the budget under particular margins such as overall budget for a specific period or a single purchase price scope, and also assessing possible prices of ad spaces and its value in the course of the real-time bidding process.
Algorithms of unsupervised machine learning
The process whereby the builder has no involvement in undeviating power is unsupervised learning. The desired results have not been defined and are not known in case of the unsupervised machine learning algorithms, unlike the primary position of supervised learning where the role of the developers is to classify the data since they know the results. More distinction between them is that it is on unlabeled data that unsupervised learning nourishes, while the exclusive use of data is what supervised learning purposes.
Some of the things developers use the algorithm of unsupervised machine for include
- Extraction of valuable insights
- Exploration of the information structure
- Increasing efficiency of operation through implementation
- Detection of patterns
In other words, by sifting through it, unsupervised machine learning describes the information and makes sense of it. Here are some of the methods unsupervised learning algorithm applies for data description:
Clustering:
Based on their internal patterns devoid of prior knowledge of group credentials and used to segment it into meaningful groups, that is, clusters, the process of clustering is simply data exploration. The individual data objects similarity defines the credentials and the test as well as used for anomalies detection features of its dissimilarity.
- Dimensionality reduction:
With the incoming data, the noise is so overwhelming. Even while distilling the pertinent information and for the removal of this noise, the algorithm of machine learning makes use of dimensionality reduction.
Hence, the algorithms that developers use widely include:
- K – means clustering
- t – Distributed Stochastic Neighbor Embedding, T – SNE
- Association rule
- Principal Component Analysis, PCA
For a maximum impact, the domains where they use unsupervised learning are ad-tech and digital marketing. Also, in most cases, for them to make befitting adjustment of the service while exploring the customer information, they apply the use of this algorithm. Indeed, in the incoming data, the fact is plenty of self-styled “known unknowns.” exist. Having the power for the extraction of appropriate foreknowledge out of unlabeled data while they make sense of it is the actual effectiveness of the business operation.
The data management of the recent gets itself ready through the unsupervised algorithms. This algorithm of machine learning has its implementation among the most cutting-edge platforms of data management like Salesforce and Lotame at the moment. Thus, with the use of specific credentials, including the setting of particular software, personal data elements, behavioral data, and so on, they can use unsupervised learning to identify target audience groups. Also, for the campaign performance patterns identification, as well as ad content, they can use this algorithm to develop more efficient targeting.
Between supervised and unsupervised algorithms, it is the middle ground that the algorithms of semi-supervised learning represent. Mainly, the semi-supervised model absorbs a thing of its own through a combination of some aspects of both.
Algorithms of semi-supervised work in some of these ways:
- For shaping the requirements of the operation, that is, train itself, the algorithm of semi-supervised machine learning makes use of a restricted labeled set sample data.
- The results of the limitation in a model with incomplete training that label data that is not marked in a sequential task. They consider pseudo-labeled data for the results because of the sample data set boundaries.
- Ultimately, for the combination of predictive and descriptive features of unsupervised and supervised learning, there is a development of a distinct algorithm with the combination of pseudo-labeled and labeled data collections.
For clustering to group data into different elements, while identifying data assets, semi-supervised learning makes use of the process of classification.
Through the support of semi-supervised learning, healthcare, and legal industries as well as others, manage speech and image analysis and classification of the content of the web. They apply semi-supervised learning for the systems of the content aggregation and crawling engines in the case of classification of web content. It arranges collections of labels in specific configurations and uses them for content analysis in both cases. However, for further sorting, this process usually requires the input of a human. uClassify is the perfect example of this procedure. General Architecture for Text Engineering, GATE is another popular tool of this category.
Based on a sample corpus and labeling performance, an algorithm provides a viable speech and image analytic model and in the case of expression and image analysis, coherent transcription. For example, this can be a CT or MRI scan. There is a possibility of identifying anomalies in the image by providing a model with a small set of exemplary scans.
Machine learning artificial intelligence has its representation in reinforcement learning. Essentially, reinforcement learning is all about a system that is self-sustained and improves itself with the use of the combination of the interactions with the incoming data and the combination of labeled data throughout the contiguous sequences of tries and fails.
Exploitation or exploration is the technique of reinforcement machine learning uses. They are simple mechanics, that is, a situation happed, they observe the consequences, and subsequent action takes consideration for the first action’s outcomes.
Occurring upon particular task performance, they are the reward signals in the center of reinforcement learning algorithms. Indeed, the reinforcement algorithms navigation tool is the reward signals, giving it an understating of the action course, whether wrong or right.
Here are the main types of reward signals:
- Encouraging the performance of particular sequences of action is the positive reward signal
- Pressing to stop the algorithm by correcting it from getting is the signal, and it also penalizes for performing specific activities is the negative reward signal
However, based on the information quality, the role of the reward signal may vary. Therefore, further classification of the reward signals may be found on the operation prerequisites. Generally, the concept of the system is to minimize negatives and maximize the positive.
The standard reinforcement learning algorithms are:
- Monte-Carlo Tree Search (MCTS)
- Q-Learning
- Asynchronous Actor-Critic Agents (A3C)
- Temporal Difference (TD)
For inconsistent and limited information available, reinforcement machine learning matches these instances. As a result of interactions with data and relevant processes, the operating procedures of an algorithm can form. This type of machine learning is what video games like contemporary NPCs mostly use. As it provides viable challenges, flexibility to the reactions of AI is what reinforcement learning offers to the player’s action. For example, in the Grand Theft Auto series, for moving people and vehicles, a feature of collision detection uses this type of machine learning algorithm.
Also, relying on reinforcement learning algorithms are self-driving vehicles. For example, it is likely for the car that self-drives to activate the ‘turn left’ scenario and so on with ‘turn left’ road detection. AlphaGo is the most popular model of this reinforcement learning variation which, by calculating the sequences of actions out of the current board position, outplayed him when diving at loggerhead with the second-best Go player in the world.
Also, also using reinforcement learning is the Ad Tech and Marketing operations. Making use of the retargeting process much more efficient and flexible is possible through this algorithm of machine learning to deliver conversion through close adaptation of the user’s surrounding context and behavior. Also, to adjust and amplify chatbots’ dialogue generation and natural language process, NLP is part of functions for which people use reinforcement learning:
- Advance more informative, engaging responses
- Impersonate a message style input
- According to the reaction of the user, identifying relevant answers
More than a technical feat, it is typical of a UX challenge for some bot with the emergence of Google DialogFlow.
As it is, solving various problems are possible for multiple algorithms of machine learning algorithms. A dominant pattern capable of valuable insights extraction process out of the entire information classes and handling a wide variety of tasks is the combination of different algorithms.
Due to the emergence of machine learning and big data, several previously speculated or deduced data became available. Based on more credible sources and for businesses to gain value-added benefits, this data provided the means to use more sophisticated techniques of data analysis. In other words, since we are equipped with more information, it is now time to further our goal from simply obtaining information to understanding and analyzing the data that was already coming to us.
Rooted in the middle of all the tools used in Big Data is pattern recognition. Pattern recognition involves the analytics of big data, uncovers the hidden meanings behind the data while getting the juice out of it. The company receives its strategic advantage through pattern recognition, and that makes it capable of continuous evolution and improvement in the market that is ever-changing.
The process of segmenting and distinguishing data following common elements or with the set criteria which unique algorithms perform is pattern recognition. Because pattern recognition gives room for further improvement and enables learning per se, it is one of the integral elements of machine learning technology.
Through its features, pattern recognition identifies things. Through flat lines and spikes, flows, and ebbs, these patterns tell stories to the data. Some of what data can include:
- Image
- Text
- Sounds
- Sentiments, and so on
Pattern recognition algorithms can process any information of the sequential nature, enabling the practical use of the sequence and also making it understandable.
Pattern recognition has three main models, and they are:
- Statistical: for the identification of the place a piece belongs. For example, to identify whether it is a football or not. The supervised machine learning is what this model uses.
- Structural/Syntactic: for the definition of a more complicated correlation between elements. For example, parts of speech. Semi-supervised machine learning is the technique this model uses.
- Template matching: for complementing the features of the object with the predefined template and identifying of the object by proxy. Plagiarism checking is one of the uses of such a model.
A lot is going on underneath even though self-descriptive is the most significant part of the pattern recognition operation. Mostly, algorithms of pattern recognition have two main parts, and they are:
- Explorative – data commonalities recognition
- Descriptive – specific manner commonalities categorization
Including the use in the big data analytics and for the extraction of insights out of the data, they make use of the combination of these two elements. To uncover a critical understanding of details in the subject matter, it is with the correlation of the common factors analysis.
Here is the picture of the pattern recognition process:
- Through input or tracking, and from its sources, you gather data
- Then, you clean up the data from noise
- Then, you examine the information for common elements or relevant features
- Subsequently, you group these elements in specific sections
- For data sets insights, you analyze the segments
- Then, you implement the extracts insights into the operation of the business
Audience research, stock market – data analytics
The interconnection between data analytics and the technology of pattern recognition is so profound that it is easy to confuse the two of them. For example, an analytic tool which is the software of stock market pattern recognition. While placing it into a broader perspective on the data analytics context and reveals its distinct features, i.e., the patterns itself, the use of pattern recognition is for data description.
Here are the exceptional use cases of pattern recognition:
- Forecast for the stock market – for the prediction of the possible results, they use pattern recognition for stock exchange comparative analysis. Businesses that engage in the study of pattern recognition include YardCharts.
- Audience research – through selected features, segmenting, and analyzing available data of the user is what pattern recognition refers. Among the companies that provide these features is Google Analytics.
Natural language processing – text translation, text analysis, chatbots, text generation
A machine learning field that has its focus on comprehensive training of the machines on communication and message generation is natural language processing (NLP). Though it seems like hard science-fiction, it has not dealt with “reading between the lines” with the essence of community in reality, and also with the direct expression of the message. By constructing its variation while finding the connections, natural language processing deciphers the text in explicit forms. To differentiate the sentences is where the process begins; while ultimately classifying the manner people can use these words in a sentence, then, that is the process of sorting out speech parts and the point the words integrate well.
To achieve this and to handle the proceedings, natural language processing combines specific techniques like segmentation, tagging, and parsing to create a model. At various stages, there is involvement of the algorithms of unsupervised and supervised machine learning in the process.
Some of the fields that use natural language process include:
- Detection of plagiarism – with the assistance of web crawler focused on a comparative study of the text, this field is a variation of text analysis. To check for matches elsewhere, tokens are the forms the words then get. Copyscape is an excellent tool that does this.
- Text analysis – for topic modeling and discovery, categorization, Buzzsumo, content marketing tools, uses this technique.
- Text generation – automated generation of content or for AI Assistants and chatbots. For example, Twitterbot updates, email auto-generation, and so on.
- Contextual extraction and text summarization – finding the meaning of the text. An example of some of these online tools for this task is Text Summarizer.
- Text translation – for recreating a near similar matching of the message in other languages, and in addition to word substitution and text analysis, the engine also combines sentiment and context analysis. Google Translate is mainly a well-known example.
- Text adaptation and correction – for simplification of the text, people use this method in addition to correcting formal mistakes and grammar, – from the words’ choice to the structure. One of such prominent examples is Grammarly.
Other use cases include recognition of optical character which they use for verification of signature and classification of the document. Prominent for analysis and conversion of images into text that is machine-encoded, its common uses include text translation, handwriting recognition, and document classification.
There is also image recognition, which is all about the identification of face and visual search. The simple concept behind image recognition is to understand what is in the picture, and the two main use cases of image recognition include face detection and visual search. Voice recognition is another use case, and it deals with the importance of sound as a source of information.
Ultimately, the key to further evolution of computational technology is pattern recognition. With the aid of pattern recognition, big data analytics can progress further, and everyone can benefit from the machine learning algorithms as they get smarter and smarter every day.
Neural Networks
The idea behind neural networks came decades ago, with the initial goal to humanize an artificial intelligence by attempting to apply a human’s learning ability to its training model. This may sound like science fiction, but keep in mind that we are not talking about cloning our neural network in order to load it into a machine. We’re not quite there yet. However, this idea has inspired the artificial neural network that we have today.
As you may already know, neural networks are collections of neurons that communicate with each other through neural nodes. The neural node is represented by code which keeps being executed until a certain goal is achieved. When the solution to this goal is found, it is sent to the next node, which repeats the process and then continues communicating with the next node that is waiting in line. In addition, neural nodes are distributed into layers. This structure forces the neurons to first communicate with their neighboring neurons found in their collective layer. Together they will fulfill that layer’s ultimate purpose and then send the output to the next layer where the process is repeated.
As you can see, artificial neural networks work in a linear model by forming a chain of communications based on a hierarchy. Human neural networks are a lot more capable at processing data because they can perform all these tasks in parallel. Furthermore, they are also developed in such a way that there are various compartments that handle specific data. For example, your ability to see and absorb visual data is handled in one section of the network. In other words, artificial neural networks aren’t optimized to such an extensive degree.
So far, you learned that data has to go through a chain of processes. We need to explore it and analyze it, then classify it and send it for processing. Neural networks work a bit differently. They are comprised of three layers. The starting layer is known as the input layer, and the final layer represents the output. In the middle, we have the hidden layer. The input layer sends its data to this layer; however, keep in mind that neural networks can function without it, or they can contain hundreds of them instead of following the three-layer structure. This type of neural network is known as a deep learning system, which we are not going to explore at this time.
Now that you have a basic idea about artificial neural networks, let’s discover its various architectures and their uses.
The Architecture
A neural network’s structure depends entirely on the way neurons are distributed. This means that we have several types of architectures to work with and each one of them has different features and applications. But before we dive into specific networks, such as perceptrons and the Boltzmann machine, we need to fully understand the characteristics that all networks have in common:
- As mentioned in the beginning of the chapter, the standard neural network is built using three layers: the input layer, hidden layer, and the output layer. Hidden layers can theoretically go from zero to any number you can handle. However, going outside of the three-layer structure already leads to two different kinds of neural networks. If we have multiple hidden layers, then we are working with deep learning structures. However, if there are no hidden layers at all, then the neural network turns into a logistic regression instead.
- The input layer can be broken down into a five-unit matrix, which holds a set of numerical values. These values can only go between zero and one. What this means is that when we have categorical features to deal with, they will have to be transformed into numerical ones instead. As for the output layer, it is built from three units. This means that we have to perform an operation to tell the difference between the three classes. This operation is known as a three-class classification.
- Finally, we have the hidden layer, which holds eight units. Keep in mind that neural networks not only contain a number of hidden layers from zero to infinite, but their hidden layers work in a similar way. They can technically hold anywhere between eight and infinite units. What this means for you is that, with experience, you will learn when you need to increase the number of layers or layer units in order to take advantage of the most efficient neural network that serves your purposes.
From here onwards, neural networks start picking up characteristics that set them apart from each other. Let’s start exploring the most basic form of a neural net, which is the feedforward neural network.
If, Then Statements
At this point we’ve already gone over a lot of the most important components of any programming language. Variable, loops and operations are all crucial for any program and inputs also play an important role a lot of the time.
But there is perhaps nothing that characterizes programming as a whole quite as well as the ‘IF, THEN’ statement. This is a little sequence of code that basically tests a condition and produces output based on that. The result is an instruction that reads ‘if this, do that’.
For instance then, we can allow a user only to progress when they’ve answered a question correctly, or when they have entered the correct password.
But before we get onto that, there’s one more thing you need to know about…
Comparing Variables
We’re going to carry on with If and Then in a bit but before we do, there’s one more thing to consider: comparing variables.
Sometimes it will be useful to look at one variable and then compare that to another variable. For instance, we might want to compare a string to a stored password if we’re asking someone to log in. Alternatively, we might be trying to find out if someone is older or younger than a certain age.
To do this, we have a few symbols and conventions. To ask if something ‘equals’ something else, we will use the symbol ‘==’ (using ‘==’ compares two variables, whereas one ‘=’ forces them to be the same). This is what will allow us to test certain conditions for our IF, THEN statements. This way we can say ‘IF’ password is correct, ‘THEN’ proceed.
For example:
Password = “guest”
Attempt = “guest”
if Attempt == Password:
print(“Password Correct”)
This essentially tests the imaginary password attempt against the true password and only says ‘correct’ when the two strings are the same. Notice that we aren’t actually using the word ‘next’ at any point. In some programming languages (such as BASIC) you actually do write ‘next’ but in most it is implicit. Anything that comes after the colon is next, which is just the same way that loops work! Python is nice and consistent and it’s actually a very attractive and simple language to look at when you code with it well…
(That’s right – programming languages can be attractive! In fact, there is even such thing as ‘code poems’!)
We can also use an input to make this a bit more interactive!
Doing this is very easy:
Password = “guest”
Attempt = input(“Please enter password: “)if Attempt == Password:
print(“Password Correct”)
Try entering the right password and you should be presented with the correct message – congrats!
There’s just one problem at the moment, which is that our user will still be able to get into the program if they get the program wrong! And there is nothing to tell them that they answered incorrectly…
Fortunately, we can fix this with our next statement: ‘else’.
As you might already have guessed, ‘else’ simply tells us what to do if the answer is not correct.
This means we can say:
Password = “guest”
Attempt = input(“Please enter password: “)if Attempt == Password:
print(“Password Correct”)
else:
print(“Password Incorrect!”)
Note that the ‘else’ statement moves back to be in-line with the initial ‘if’ statement. Try entering wrong passwords on purpose now and the new program will tell you you’ve made a mistake!
Okay, so far so good! But now we have another problem: even though our user is entering the password wrong and being told as much, they are still getting to see whatever code comes next:
Password = “guest”
Attempt = input(“Please enter password: “)if Attempt == Password:
print(“Password Correct”)
else:
print(“Password Incorrect!”)
print(“Secret information begins here…”)
Of course this somewhat negates the very purpose of having a password in the first place!
So now we can use something else we learned earlier – the loop! And better yet, we’re going to use while True, break and continue. Told you they’d come in handy!
Password = “guest”
while True:
Attempt = input(“Please enter password: “)
if Attempt == Password:
print(“Password Correct”)
break
else:
print(“Password Incorrect!”)
continue
print(“Secret information begins here…”)
Okay, this is starting to get a little more complex and use multiple concepts at once, so let’s go through it!
Basically, we are now starting a loop that will continue until interrupted. Each time that loop repeats itself, it starts by asking for input and waits for the user to try the password. Once it has that information, it tests the attempt to see if it is correct or not. If it is, it breaks the loop and the program continues.
If it’s not? Then the loop refreshes and the user has another attempt to enter their password!
We’ve actually gone on something of a tangent here but you may recall that the title of this was ‘Comparing Variables’. What if we don’t want to test whether two variables are the same? What if we want to find out if one variable is bigger than another? We can ask if something is ‘bigger’ using the symbol ‘>’ and ask whether it is smaller using the ‘<’ symbol. This is easy to remember – just look at the small end and the big end of the character!
Adding an equals sign will make this test inclusive. In other words ‘>=’ means ‘equal or bigger than’.
Likewise, we may also test if two strings are different. We do this like so: ‘!=’ which basically means ‘not equal to’.
Using that last example, we can turn our password test on its head and achieve the exact same end result:
Password = “guest”
while True:
Attempt = input(“Please enter password: “)
if Attempt != Password:
print(“Password Incorrect!”)
continue
else:
print(“Password Correct”)
break
print(“Secret information begins here…”)
Of course when you get programming you’ll find much more useful ways to use this symbol!
Elif and Or
Else is one more command you need to learn to use If statements properly. Equally important though is ‘Elif’. Elif is essentially a portmanteau (two words combined) of the words ‘Else’ and ‘If’.
So what we now have is a statement that gives the following instruction:
‘If this is true, do this’ (IF)
‘If that’s not true, but this is, then do this’ (ELSE IF – ELIF)
‘Or else, do this’ (ELSE)
So what might this look like? Here’s a little example of some code that talks to you differently dependent on your gender. You might find that this also contains another new word you’re not familiar with…
Gender = input(“Are you male or female? “)
if Gender == “male” or Gender == “Male”:
print(“Hello sir!”)
elif Gender == “female” or Gender == “Female”:
print(“Hello ma’am!”)
else:
print(“What is one of those?”)
The new word in question is of course ‘or’. Let’s see how all this works…
Basically, our program is asking you to tell it if you’re male or female. If you say male, it calls you sir. If you don’t say male but you do say female, then it says ‘hello ma’am’.
If you say something else entirely though, then it gets a little confused and asks what you just said.
The ‘or’ meanwhile, let’s us perform the same if either one of two statements is true. In this case, we’re allowing the program to proceed with either the lower case ‘female’ or proper case ‘Female’. There are other ways to do this but this is a good way to demonstrate the power of ‘or’.
If it weren’t for or, we would be forced to do this:
Gender = input(“Are you male or female? “)
if Gender == “male”:
print(“Hello sir!”)
elif Gender == “Male”:
print(“Hello sir!”)
elif Gender == “female”:
print(“Hello ma’am!”)
elif Gender == “Female”:
print(“Hello ma’am!”)
else:
print(“What is one of those?”)
This is another good demonstration of how some code can be more elegant than other code. You might wonder why it matters but when you come to look back over this and you have to sift through thousands of lines, it actually makes all the difference. Moreover, reducing the amount of writing you have to do will save you time and energy – and will also mean the programs you write actually run more quickly!
Let’s Make Our First Game!
We’ve talked an awful lot of theory at this point so perhaps it’s time for us to make our first game! It’s not going to be that much fun, seeing as you’ll know the answer – but you can get your friends to play it to impress them with your coding know-how (unfortunately, it’s still not all that fun even then!).
The game is simply going to get the player to guess the number it is thinking of and will then give clues to help them get there if they get it wrong.
CorrectNumber = 16
while True:
GuessedNumber = int(input(“Guess the number I’m thinking of!”))
if GuessedNumber == CorrectNumber:
print(“Correct!”)
break
elif GuessedNumber < CorrectNumber:
print(“Too low!”)
continue
elif GuessedNumber > CorrectNumber:
print(“Too high!”)
continue
print(“You WIN!!!”)
Nesting Ifs and Loops
It’s also possible to ‘nest’ your ifs and your loops in order to make even more elaborate programs that test multiple different conditions.
We already did this once when we put the ‘if’ inside the loop. However we can also put ifs inside ifs inside ifs if we want!
Conclusion
The next step is to start practicing some of the codings that we spent time in this guidebook. We spent a lot of time exploring the different things that you are able to do with Python and a lot of the different aspects that are going to show up in your own coding adventures. While we may not have gone into as much depth as you would see in other locations, we made sure that we knew what each part was about and explored the actual coding that you would need to do with this.
Python Data Science
The Complete Guide to Data Analytics + Machine Learning + Big Data Science + Pandas Python. The Easy Way to Programming (Exercises Included).
Table of Contents
Chapter 3 The Basics of Working with Python
Chapter 4 Data Structures and the A* Algorithm
Chapter 5 Reading data in your script
Chapter 7 Probability – Fundamental – Statistics – Data Types
Chapter 8 Distributed Systems & Big Data
Chapter 9 Python in the Real World
Introduction
Data Science might be a relatively new multi-disciplinary field, however its integral parts have been individually studied by mathematicians and IT professionals for decades. Some of these core elements include machine learning, graph analysis, linear algebra, computational linguistics, and much more. Because of this seemingly wild combination of mathematics, data communication, and software engineering, the domain of data science is highly versatile. Keep in mind that not all data scientists are the same. Each one of them specializes based on competency and area of expertise. With that in mind, you might be asking yourself now what’s the most important, or powerful, tool for anyone aiming to become a data scientist.
This book will focus on the use of Python, because this tool is highly appreciated within the community of data scientists and it’s easy to start with. This is a highly versatile programming language that is used in a wide variety of technical fields including software development and production. It is powerful, easy to understand, and can handle any kind of program whether small or complex.
Python started out in 1991, and it has nothing to do with snakes. As a fun fact, this programming language loved by both beginners and professionals was named this way because its creator was a big fan of Monty Python, a British comedy group. If you’re also one of their fans, you might notice several references to them inside the code, as well as the language’s documentation. But enough about trivia – we’re going to focus on Python due to its ability to develop quick experimentations and deploy scientific application. Here are some of the other core features that explain why Python is the way to go when learning data science:
- Integration: Python can integrate many other tools and even code written in other programming languages. It can act as a unifying force that brings together algorithms, data strategies, and languages.
- Versatility: Are you a complete beginner who never learned any kind of programming language, whether procedural or object-oriented? No problem, Python is considered by many to be the best tool for aspiring data scientists to grasp the concepts of programming. You can start coding as soon as you learn the basics!
- Power: Python offers every tool you need for data analysis and more. There are an increasing number of packages and external tools that can be imported into Python to extend its usability. The possibilities are truly endless, and that is one of the reasons why this programming language is so popular in diverse technical fields, including data science.
- Cross-Platform Compatibility: Portability is not a problem no matter the platform. Programs and tools written in Python will work on Windows, Mac, as well as Linux and its many distributions.
Python is a Jack of all trades, master of everything. It easy to learn, powerful, and easy to integrate with any other tools and languages, and that is why this book will focus on it when discussing data science and its many aspects. Now let’s begin by installing Python.