Python Artificial Learning: Rahul Mula
Python Artificial Learning: Rahul Mula
ARTIFICIAL LEARNING
Hove you ever thought the
different devices you ore using
with the prefix smort can be smarter
than you? Yes oil the smart devices like
smartphones, smort tv's, or even smort
homes ore oil machines that ore
created or more specifically
programmed by humons
to think like humans i.e.
Aritificial intelligence. So do
you wont to learn the intelligence
behind these and even leorn to use
these intelligences in your favour,
then you've got covered!
Rahul Mula
Artificial Intelligence with Python
by Rahul Mula
© 2021 Artificial Intelligence with Python
All rights reserved. No portion of this
book moy be reproduced in any form
without permission from the copyright
holder, except os permitted by U.S.
copyright low.
ISBN : 979-8-58-957189-9
The reader is assumed to have a knowledge of
advanced level programming and data science
skills. The book is for the beginners at Artficial
intelligence not for beginners at programming. You
can check the following books to catch up now:
http://bit.ly/PyBooks
Python for
Complete Beginners
Learn programming
from the basics
with Python and
become from a
beginner to an
advanced level
programmer
Basic
conncepts
Data science is
on of the most
in-demand skills
nowadays and python
has made it easy
to learn
Operating
data
Learn to perform
tasks like
data processing,
cleansing,
analysing and
visualizing
MACHINE
LEARNING
Machine Learning WITH PYTHON
MACHINE LEARNING
TH PYTHON
SCIKIT
learn Lti>S TEXT
MORECODE
Models &
Algorithms
Learn about
different models
and algorithms
We are living in an age where the machines or
devices like smartphones, computers, etc. are
becoming intelligent i.e. they can do their work
with much human interaction or guidance. THe best
example is your smartphones! So why are they
called ''smart-’phones? Well, earlier we had
telephones that only satisfied the function of
calling someone where you had to input the number
through keys and then the call is connected to the
person you want. They were considered
telephone but as techonligies grew
that telephone which only satisfied
the function of calling someone has
evolved into a smartphone that has
barely any tasks which it can't do.
Calling someone is the same function
which is satisfied by both but the input has
changed a lot, telephones required exclusive human
guidance i.e. dailing the numbers but now whoever
you want to call just think out loud and your
smartphone will call the person you want, look how
intelligent the device has became. But that's not
the end of it there is still more room for growth!
Decades ago no one thought that we will just tell
the phone to call this person and it
will do so. In the field of
artificial intelligence this is
called natural language processing
which we will learn about
This book will help you to
understand about the steps and
methods in the creation of
intelligence artificially. You may find these a
bit hard or too difficult if you're a complete
beginner but don't worry these are created by
human and you're likely to be a human too so don't
loose to the artificial intelligences and be the
smartest out there. I'm also a student so I know
what were the fears of a beginner, everything just
seems too difficult but that is the thought which
you have to go about. It is easy for the
knowledgeable to go about every thing but hard for
the beginners because even if they have good
programming skills but these things just seem out
of place, so keeping those in mind
I will explain every piece of code so nobody feels
left out! But I also want your cooperation to be
persistent. I will not guarantee you that youJll
become a pro at it immediately but I will make
sure you become a pro definitely!
r MACHINE LEARNING
03 I
CHAPTER | WV
• What is machine learning?
• Uses of machine learning
• How do machines learn?
1
PANDAS
1
ii
|>;l • Features of Pandas Library
CHAPTER pandas ’Series
• Data Frames
MATPLOTLIB
k
CHAPTER
. «.ru ** Features of matplotlib
matplvthb .Datavisualizationp
• PyPlot in matplotlib 1
SCIKIT LEARN
k
08
dAPTER
CHAPTER
• Features of Scikit-learn library
• How to work with data?
• Why use Python?
1
09 A
TYPES OF MACHINE LEARNING
CHAPTER
HAPTER Q O
• Supervised learning
• Unsupervised learning
• Deep learning 1
■? IMPORTING DATA
CHAPTER
I 1
I I
I
I I
• Importing CSV data
• Importing JSON data
• Importing Excel data
1
£ DATA OPERATIONS
CHAPTER
’ NumPy operations
• Pandas operations
• Cleaning data 1
DATA ANALYSIS 8 PROCESSING
k
CHAPTER
• Data analytics
• Correlations between attributes
• Skewness of the data
1
DATA VISUALIZATION
13
CHAPTER
• Plotting data
• Univaritae plots
• Multivariate plots
1
CLASSIFICATION
CHAPTER
• Decision tree
• Linear regression
• Naive Bayes
1
NEURAL NETWORKS
• Creating neural networks artificially
• Perceptor classifier
CHAPTER
• Layers of networks
DEEP LEARNING
k
21
CHAPTER
• Deep learning v/s Machine learning
• Deep learning models
• Regression using deep learning
1
ARTIFICIAL
U1 INTELLIGENCE
• What is Al?
• How Al works?
• Applications
of Al
o
01
_______ /
ARTIFICIAL INTELLIGENCE INTRODUCTION
□--------------------------------- J
What is Artificial Intelligence?
//
How Al works?
So how does your brain works? For example how do
you talk? Since we are born we learn multiple
words from our surrounding people and speak the
same. It is a simple method of learning and
performing tasks. The same is for the Al systems.
We train them and they perform but in some cases
they even learn on their own. So how do even they
learn, we know that we have brains with complex
network of nerves but how do machines learn? Well
there are a lot of approches used by machines to
learn. Machine learning algorithms, deep learning
algorithms, neural networks, etc. are used to
create Al systems. But learning isn't a prominent
part of Al because Al focuses more on performing
tasks rather than learning
r*---------------------------------------------------------------------
16 Al INTRODUCTION
Applications of AI
As mentioned earlier AI is equivalent to human
intelligence so anything that humans can perform,
AI systems can too or in development where not.
There are lot of AI applications around you like:
• The games like chess or cards where you play
against the computer or mobile. AI is used
to learn all the possibilities
in the game and perform
them like, what do if the
user does this or does that
J
/------- Q A A
Al Systems
There are many Al systems like location tracking
systems, clinical systems, that are called Expert
systems. These systems are used in complex areas
like healthcare which requires high intelligence.
They can conclude some results, predict some
results, explaining, etc.
These systems have three essential parts namely
Knowledge base, Inference engine and UI(User
Interface)
-------------------------------------------------------------
19 TYPES OF Al
Condition 1
Condition 2
Condition 3
Result l
Result 2
Al Algorithms
Algorithms are widely used to solve a lot of
problems like searching. In Al these algorithms to
solve a specefic problem. For example let's
consider an example of an algorithm that will play
tic toe with the user. The algorithm will have the
following attributes:
• Search Space, the set of possible *'
solutions i.e. to decide which
box to use out of 9
X o
• Start state, the state from o
where the search begins i.e. X
the empty boxes if you have the
first turn or one filled box if you
have the second turn
• Goal test, is a function which checks the
current state with the state which is needed
to be achieved i.e. whether three crosses or
circles are in row or not
• Problem instance, it is the intial state and the
Goal
• Problem Space graph, is the representation of
the problem state i.e. we need three crosses or
circles in a row
• Depth of a problem, shortest path to the goal
from the intial state i.e. how to align three
crosses or circles
IP
X
o X
IQ
O
22
--
TYPES OF Al
Speech
Speech is a widely used Al for speech
recognition, speech generation, etc.
Voice search is one of the
applications of speech recognition.
NLP and Speech can be used
together to create applications 4
that can understand human
language and perform different
tasks like translation and then
convert it back into speech
Anomaly detection
Anomaly detection is a method which can be used
to monitor unusual behaviours or errors in a
dataset or system. It is used in many factories to
monitor the status of machines and detect whether
everything is running smoothly or not
Computer Vision
Computer vision is an Al that is used to help the
softwares or programs to use images as input and
perform different actions like image analysis,
object detection, Image classification, face
recognition, etc. There are a lot of application
used for special facial effects i.e. face
recognition. Computer vision
can be combined with Robotics
to create devices for
monitoring tasks at factories.
Computer vision has application
like OCR i.e. Optical Character
Recognition which is used to
recognise handwritten text or
printed texts
25
----------
TYPES OF Al
Neural Networks
Inspired from the human neural
networks; a new research area of
Al came into existence i.e.
Artificial Neural Networks(ANN).
Like neurons they create
different nodes and take the
input and pass the input to
different nodes which analyze.,
classify, etc. and create the output.
But the downside to these networks is
that, it doesn't provide any resoning why it came
to that decision or the working of ANN are a
mystery
Deep learning
Deep learning models are based on Artificial
Neural Networks (ANN), or more specifically
Convolutional Neural Networks (CNN)s. There are
several architectures used in deep learning such
as deep neural networks, deep belief networks,
recurrent neural networks, and convolutional
neural networks.
These models are used to solve
problems like:
• computer vision, image
classification, etc.
• bioinformatics
• drug design
• games, etc.
Deep learning is entirely a
concept in itself that it is
completely different type of machine learning or
different from it, we will not discuss it in much
detail but let's see what is it and how do it
solves problems.
26
----------
TYPES OF Al
MACHINE
UO LEARNING
• What is Machine
Learning?
• Use of Machine
Learning
• How Machines
Learn?
0
o
A
03 MACHINE LEARNING
\________/ □------------------
What is Machine Learning?
//
Forecasting weather of
a day beforehand through finding
patters of weather in the data
of weather of previous days
suggesting a product to a
customer in an oniine store
according to the users previous
search terms
Z—• F*-----------------------------------------------------------
30 MACHINE LEARNING
3500
3000
o'
c
| 2500
<u
| 2000
§
1500
----------------------------- >
60 65 70 75 80 85
Price
Price
Now we can use the line as reference and predict
values. SOj for 2600Mhz the cost will be about 77
So how do we draw out the line. We follow the
formula cost - a + b * Mhz, but what are a & b?
a and b are parameters of the straight line which
you don't need to sweat about.
1500
z—• /----------------------------
32 MACHINE LEARNING
-
<\/| PANADAS
MH LIBRARY
• Features of Pandas
library i'll
• Series I'1
• Dataframes pandas
o J
A
04 PANDAS library
________________________________________ >
Pandas
Data science requires high-performance data ma
nipulation and data analysis., which we can achieve
with Pandas Data Structures.Python with pandas is
in use in a variety of academic and commercial
domains, including Finance, Economics, Statistics,
Advertising, Web Analytics, and more. Using
Pandas, we can accomplish five typical steps in
the processing and analysis of data, regardless of
the origin of data - load, organize, manipulate,
model, and analyse the data.
Series
Pandas deals with data with itJs data
structures known as series, data frames and panel.
Series is an one-dimensional array like structure
with homogeneous data. For example, the following
series is a collection of integers
10 17 23 55 67 71 92
As series are homogeneous data structure, it can
contain only one type of data [here integer]. So,
we conclude that Pandas Series is:
• It is a homogeneous data structure
• Its size cannot be mutated
• Values in series can be mutated
Data Frames
DataFrame is a two-dimensional array with
heterogeneous data.
Day Sales
Monday 33
Tuesday 37
Wednesday 14
Thursday 29
Z—• F*-----------------------------------------------------------
36 PANDAS LIBRARY
—
r
Key Points
• Pandas library is a high performance data
manupilation and data analysing tool.
• Pandas data structures include series and
data frames
• Series is a 1-Dimensional array of
homogeneous data, whose size is immutable
but values in a series are mutable.
• Data Frames is a 2-Dimensional array of
heterogeneous data of 2 or more series,
whose size and data are mutable.
■x
NUMPY
UD PACKAGE
• Features of NumPy
• ndarray Objects
• List vs. ndarrays
o J
/• A
k
05 ______ /
NUMPY PACKAGE
________________________________________ J
NumPy
NumPy is a Python package which stands for
'Numerical Python'. It is a library consisting of
multidimensional array objects and a collection of
routines for processing of array.
3D array
2D array
ID array
5.2 3.0 4.5
axis 0 axis 1
ndarray objects
NumPy aims to provide an array object that is up to
50x faster that traditional Python lists. The array
object in NumPy is called ndarray, it provides a lot
of supporting functions that make working with
ndarray very easy. Arrays are very frequently used in
data science, where speed and resources are very
important.
In NumPy, we can create 0-D,l-D,2-D and 3-D
ndarrays.
0-D (33)
1- D ([11,27,18])
2- D ([ 3, 5,6],
[5, 7,11])
3- D ([ 5,8,19],
[ 6,9,10],
[4,1,11])
Lists ndarrays
• List is an array of • ndarray is an array of
heterogeneous objects homogeneous objects
• List arrays are • ndarrays arrays are
stored in different stored in one
places in the memory continuous place in
which, makes it slow the memory which,
to process data. makes it faster to
• Lists are not process data.
optimized to work • ndarrays are
with latest CPU's optimized to work
• A 1-Dimensional with latest CPU's
List • A 1-Dimensional
ndarray
Lists arrays
memory toe
-12044567
memory too
-12044568
PyObject_Head
"Length
items
ndarrays
PyObject_Head 1
data 2
3
“7
dimensions
4
strides
5
f------------------------------------ s
6 ndarrays memory
7 allocation
\______________________ z
Key Points
• NumPy stands for Numerical Python, which
is a Python Package used for working with
arrays.
• It also has functions for working in domain
of linear algebra, fourier transform, and
matrices.
• ndarrays or n-dimensional arrays are
homogeneous arrays, which are optimized
for fast processing.
• ndarrays also provide many functions that
makes it suitable to work with data
■x
AQ SCIPY
MO PACKAGE
• Features of SciPy
• Data Structures 1
• SciPy
Sub-Packages
o
—--------------------------------------------------- A
06 SCIPY PACKAGE
k
SciPy
The SciPy library of Python is built to work with
NumPy arrays and provides many user-friendly and
efficient numerical practices such as routines for
numerical integration and optimization. Together,
they run on all popular operating systems, are
quick to install.
A—
in [1]: #Import packages
from scipy import integrate
import numpy as np
def my_integrator(a,b,c):
my_fun = lambda x: a*np.exp(b*x)+c
NumPy
y,err = integrate.quad(my_fun,0,100)
print(’ans: %1.4e, error: %1.4e’ % (y,err
return(y,err) /
#CaLL function
my_integrator (5,-10,3)
Data structures
The basic data structure used by SciPy is a mul
tidimensional array provided by the NumPy module.
NumPy provides some functions for Linear Algebra,
Fourier Transforms and Random Number Generation,
but not with the generality of the equivalent
functions in SciPy. Except for these, SciPy offers
Physical and mathematical constants, fourier
transform, interpolation, data input and output,
sparse metrics, etc
11 92 4 3 2 2 3 3 2 1 11 4 2 1
3 9 13 8 21 17 4 2 1 4 1 4 1
8 32 1 2 34 18 7 78 10 7 8 3 1
9 22 3 9 8 71 12 22 17 3 9 1 17
13 21 21 9 2 47 1 81 21 9 13 21 9 2 47 1 81 21 9
21 12 53 12 91 24 81 8 91 2
61 8 33 82 19 87 16 3 1 55 19 8 16 55
54 4 78 24 18 11 4 2 99 5 54 4 11
13 22 32 42 9 15 9 22 1 21 2 22 21
SciPy sub-packages
As we already know, SciPy is organized into
sub-packages covering different scientific comput
ing domains, we can import them according to our
needs rather than importing the whole library.
The following table shows the list of all the
sub-packages of SciPy :
[next page]
—• *
45 SCIPY PACKAGE
Key Points
• SciPy Package is a toolbox which is used
for common scientific issues.
• SciPy together with NumPy creates a
dynamic tool for data processing.
• Along with NumPy functions, SciPy provides
a lot of functions to perform different
tasks with ndarrays.
• SciPy is divided into sub-packages
determined for different tasks.
■x
r\~f MALPLOTLIB
\Jf LIBRARY
’SSOf matplstlib
• Data Visualization
• PyPlot in Matplotlib
o
A
07
X______ 7
MATPLOTLIB LIBRARY
________________________________________ J
MatPlotLib
Matplotlib is a python library used to create 2D
graphs and plots by using python scripts. It has a
module named pyplot which makes things easy for
plotting by providing feature to control line
styles, font properties, formatting axes etc.
50
40
30
■ ■ +
20
10
Data Visualization
Data visualization is the graphical representa
tion of information and data. By using visual
elements like charts, graphs, and maps, data visu
alization tools provide an accessible way to see
and understand trends, outliers, and patterns in
data.
PyPlot in Matplotlib
matplotlib.pyplot is a collection of functions
that make matplotlib work like MATLAB. Each pyplot
function makes some change to a figure: e.g.,
creates a figure, creates a plotting area in a
figure, plots some lines in a plotting area,
decorates the plot with labels, etc.
To test it yourself, jump to lupyter Notebook and
start of by importing the matplotlib. pyplot
module.
6
in
>
4
r
Key Points
• MatPlotLib is a library used for
visualizing our data using itJs MATLAB
like functions
• MatPlotLib-’s PyPlot module makes it easier
to plot data, with full control over
color, line & shape, font, axis-labels,
etc.
• It supports wide range of graphs and plots
like, histogram, bar graphs, pie charts,
and even 3-D graphs.
• MatPlotLib is the best library for data
visualization
rQr ■x
r^QSCIKIT LEARN
UO LIBRARY
• Features of
Scikit learn
library team
o J
A
08
______ /
SCIKIT LEARN LIBRARY
______________________ J
Scikit Learn or Sklearn
Scikit-learn or Sklearn is the most useful and
robust library for machine learning in Python. It
provides a selection of efficient tools for
machine learning and statistical modeling
including classification, regression, clustering
and dimensionality reduction via a consistence
interface in Python. This library, which is
largely written in Python, is built upon NumPy,
SciPy and Matplotlib.
$
• Unsupervised learning algorithms like
clustering, factor analysis, PCA (Principal
Component Analysis) to unsupervised neural
Z—• F*------------------------------------------------------------------
✓
TYPES OF
kJ <3 MACHINE LEARNING
Supervised leorning
• Unsupervise leorning
• Deep learning
• Reinforcement
leorning
• Deep reinforcement
learning
o-------------
A
09
\______ /
TYPES OF MACHINE LEARNING
_______________________J
In the previous lessons we learned about the
various libraries and packages used in the process
of machine learning. Now letJs look at the types
of machine learning
reinforcement
Learnifig Supervised
Learning
Ur]supervised
Learning
deep
reinforcement
Learnirfg
D^ep Learning
Supervised learning
As it's name suggest in supervised
learning we train a machine learning
model with supervision. We feed the
data, train the model and tell the
model to make predictions. If the
predictions are correct we leave the
model to work on itJs own from there else
we help the model to predict correctly
until it learns so. It is the same as
teaching a child to solve questions at first
until he can solve them on his own.
58*J^TYpES OF MACHINE learning j
Reinforcement learning
Reinforcement learning consists of learning
models which doesn't require any input or output
data instead they learn to perform certain tasks
like solving puzzles or playing games. If the
model performs steps correctly it is rewarded
points, unless it penalized. The models learns the
more it performs from it's mistakes. The model
creates data as it performs the functions unlike
receiving data at the beginning.
For example consider the following model:
Z—• ------------------------------------------ *
62 TYPES OF MACHINE LEARNING
—
1 IMPORTING
IV DATA
• Importing CSV
Data
• Importing JSON I I
Data I I
I
• Importing XLS
Doto
o J
z Oz A
10 J IMPORTING DATA
__________________________________________ >
Installing Anaconda
Head to anaconda.com/products/individuat to
download the latest version of Anaconda.
Anaconda Installers
64-Bit Graphical Instcliter (466 MB) 64-Bit Graphical Installer (462 MB) 64-Bit (x86) Inst.liter (550 MB)
32-Bit Graphical Instailler (397 MB) 64-Bit Command Line Installer (454 MB) 64-Bit (Power8 ;>nd Power9) Installer (290
MB)
Best match
® Uninstall
(base) C:\Users\Rahul>
Jupyter Notebook
The Oupyter Notebook is an open-source
web application that allows you to create
and share documents that contain live
code, equations, visualizations and jupyter
explanatory text. We will use it to perform
our data processing, analytics and
visualization, etc. on the go.
To open iupter Notebook, write jupyter notebook in
the anaconda command prompt and press enter.
5 Anaconda Prompt (anaconda3) □ >
(base) C:\Users\Rahul>jupyter notebook
Anaconda will redirect you to your browser, [it may
ask you, in which browser to host your jupyter
notebook if you have more than one browsers] a new
tab will appear with your Jupyter notebook hosted.
You can host your Python files here, and also run the
code on the fly.
P’ jupyter Quit Logout
67 IMPORTING DATA
•
jupyter
A tutorial introducing basic features JupyterLab is the new interface for A basic example of using Jupyter
of Jupyter notebooks and the Jupyter notebooks and is ready for with Julia.
I Python kernel using the classic general use. Give it a try!
Jupyter Notebook interface.
We can rename our file, by clicking the name
[untitled]
3 jupyter Untitled Last Checkpoint: a few seconds ago (unsaved changes) Visit repo Copy Binder link
File Edit View Insert Cell Kernel Widgets Help Trusted | Python 3 O
I In [ ]: Q
File Edit View Insert Cell Kernel Widgets Help Trusted | Python 3 0
In [ ]:
File Edit View Insert Cell Kernel Widgets Help Trusted / | Python 3 O
B + | >5t 4 4> H Run ■ C » Markdown v E3 A Download <1 Ct O GitHub % Binder Memory: 219/2048 MB
I # Jupyter Notebook
File Edit View Insert Cell Kernel Widgets Help Trusted Z | Python 3 O
Jupyter Notebook
in [ ]:
f •
69 IMPORTING DATA
---- ------------------
In code cells, we can write Python codes and execute
them instantly.
^JUpyter New Last Checkpoint: 12 minutes ago (autosaved) Visit repo Copy Binder link
File Edit View Insert Cell Kernel Widgets Help Trusted / | Python 3 0
Jupyter Notebook
In [ ]: 123*525|
^JUpyter New Last Checkpoint: 13 minutes ago (unsaved changes) Visit repo Copy Binder link
File Edit View Insert Cell Kernel Widgets Help Trusted | Python 3 O
Jupyter Notebook
In [1]: 123*525
Out[l]: 64575
File Edit View Insert Cell Kernel Widgets Help Trusted | Python 3 O
Jupyter Notebook
In [1]: 123*525
Out[l]: 64575
In [ ]:
File Edit View Insert Cell Kernel Widgets Help Trusted | Python 3 O
Jupyter Notebook
Z—• F*-----------------------------------------------------------------------------------------
70 IMPORTING DATA
• -J
# Jupyter Notebook
## IPython
###Data
In [ ]: |
Jupyter Notebook
IPython
Data
in [ ]:
Biscuits 5 2
Milk 7 5L
Z—• F*-----------------------------------------------------------------------------------------
72 IMPORTING DATA
—
HereJs a complete list of shortcuts of various
operations with cells.
r
Operations Shortcut
change cell to code y
change cell to markdown m
change cell to raw r
close the pager Esc
restart kernal 0 + 0
copy selected cell c
cut selected cell X
delete selected cell d + d
enter edit mode Enter
extend selection below Shift + j
extend selection above Shift + k
find and replace f
ignore Shift
insert cell above a
insert cell below b
interrupt the kernal i + i
Merge cells Shift + m
paste cells above Shift + v
paste cells below V
run cell and insert below Alt + Enter
run cell and select below Shift + Enter
run selected cells Ctrl + Enter
save notebook Ctrl + s
scroll notebook up SHIFT + Space
scroll notebook down Space
select all Ctrl + a
show keyboard shortcuts h
toggle all line numbers Shift + 1
toggle cell output 0
toggle cell scrolling Shift + o
toggle line numbers 1
undo cell deletion z
Z—• F*-----------------------------------------------------------------------------------------
73 IMPORTING DATA
• -J
Data files
Often we have data in multiple file formats like,
data of sales of any product, number of
subscribers, etc. There are a lot of ways to import
data but we will use pandas library here
Importing CSV Data
Reading data from CSV(comma separated values) is a
fundamental necessity in Data Science. Often, we get
data from various sources which can get exported to
CSV format so that they can be used by other
systems. To work with csv files we need one first,
you can download the sample file from here
http://bit.Ly/csv_dt
To import it, you need move the csv file in the
place where your lupyter Notebook is hosted, to find
it use the below code
In [32]: import os
print(os.getcwd())
C:\Users\Rahul
74 IMPORTING DATA
• -J
Out[39]: 0 227
1 158
2 50
3 24
4 85
5 121
Name:: sales, dtype: int64
0ut[44]:
name sales
0 biscuits 227
1 cookies 158
2 cake 50
3 whey_supplement 24
4 protein_bars 85
5 potato_chips 121
0ut[46]:
name sales
4 protein_bars 85
5 potato_chips 121
Z—• F*-----------------------------------------------------------------------------------------
75 IMPORTING DATA
• -J
Out[50]: 227
0 12 3 4
id name price sales brand
V
dt. values J
/1O1 biscuits 5^^(227) HomeFoodsJ
'dt,vatues[Q,3]}
ID Name Sales
0 101 Biscuits 227
1 102 Cookies 158
2 103 Cake 52
3 104 Whey Supplement 24
4 105 Protein Bars 85
5 106 Potato Chips 121
0 Biscuits
1 Cookies
2 Cake
3 Whey Supplement
4 Protein Bars
5 Potato Chips
Name: Name, dtype: object
HomeFoods
IMPORTING DATA
78 IMPORTING DATA
—
We have removed the Unnamed: 5 column
DATA
U OPERATIONS
• NumPy
operations
• Pandas
operations
• Cleaning data
o
—11T------------
data operations
A
'--- '□----------- y
NumPy Operations
To start working with NumPy, we need to import
numpy to create NumPy arrays.
In [ ]: import numpy
[1 5 7]
[[1 5 7]
[2 3 9]]
1-D array
r2l-D array
1
81 data operations
-
[[1 5 ?]]
[T '5' ’7’]
[1. 5. 7.]
[1.+0.J 5.+0.j 7.+0.j]
Pandas Operations
Pandas handles data through Series,Data Frame,
and Panel. We will learn to create each of these.
Pandas Series
We already know what Pandas Series is. A pandas
Series can be created using the SeriesO function
so, let's import pandas and create series.
0 1
1 5
2 7
dtype: int64
A 1
B 5
C 7
dtype: int64
0 1.000000+0.000000j
1 5.000000+0.000000j
2 7.000000+0.000000j
dtype: complexl28
Z—•
83 data operations
•
We can use a ndarray to create a pandas series
Q 1
1 5
2 7
dtype: int32
[1 5 7]
<PandasArray> ta type
[1, 5, 7]
Length: 3, dtype: int32
Product Sales
0 Cookies 157
1 Biscuits 227
Product Sales
1 Cookies 157
2 Biscuits 227
df = pandas.DataFrame(data = ar,
index = ['A','B'],
columns = [,C1','C2'])
print(df)
Cl C2
A 1 3
B 6 2
df = pandas.DataFrame(data = ar,
index = ['A','B'],
columns = ['Cl','C2'])
df['C3'] = (df['Cl']*5)
print(df)
Cl C2 C3
A 1 3 5
B 6 2 30
df = pandas.DataFrame(data = ar,
index = ['A','B'],
columns = [,C1,,'C2'])
df['C3‘] = (df['Cl']*5)
del df['C2']
print(df)
Cl C3
A 1 5
B 6 30
df = pandas.DataFrame(data = ar,
index = ['A','B'],
columns = [,C1',,C2'])
print(df['Cl'])
A 1
B 6
Name: Cl, dtype: int32
data operations
Slicing Syntax
To get a single element from a ndarray or pandas
series or pandas dataframes, we need to use the
slice syntax <array>[start:end:step(optional)]
Let's extract some elements from the arrays we
have created so far.
1
3
4
[[[1, 3]j
[5, 2]]j
l[2, 4]j
[4j 6]] ]
[[[1, 3]j
[5, 2]]j
[[2> 4 ],
[4, 6]] ]
ar3[l,0,l]
Final Slice
1
1
print(sr.loc[4])
print(sr.iloc[l])
3
3
sr[4] = 7
print(sr)
del sr[6]
print(sr)
2 1
4 7
6 5
dtype: int64
2 1
4 7
dtype: int64
Let's say we have a DataFrame like this
0ut[27]:
Product Sales
1 Biscuit 227
2 Cookies 158
Out[32]: 1 227
2 158
Name: Sales, dtype: int64
Out[37]: 158
0ut[41]:
Product Sales
1 Biscuit 227
2 Cookies 158
0ut[45]:
Product
1 Biscuit
2 Cookies
0ut[48]:
Product Sales
1 Biscuit 227
2 Cookies 158
Out[51]: 158
data operations
In [55]: ar = ar[::-1]
ar
In [64]: ar.sort()
ar
Data Cleansing
Let's consider a situtation like below
0ut[71]:
C1 C2 C3
a 1 2 3
c 4 7 2
e 4 9 1
In [72]: df = df.reindex(['a','b','c','d','e'])
df
0ut[72]:
C1 C2 C3
Out[73]: a False
b True
C False
d True
e False
Name: Cl, dtype: bool
z—• /
93 data operations
-
Pandas provides various methods for cleaning the
missing values. The fillna function can filAna NaN
values with non-null data in a couple of ways like
replacing NaN values with 0
In [74]: df.fillna(O)
Out[74]:
C1 C2 C3
C1 C2 C3
In [76]: df.dropna()
0ut[76]:
C1 C2 C3
Out[78]:
C1 C2 C3
a 1 2 13
c 4 7 2
e 4 9 1
1 O DATA ANALYSIS
lu S PROCESSING
• Doto Analytics
• Correlations
between
attributes
• Skewness of
the data
o
A
data analysis s processing
k ________________________________________________ J
Data analytics
Before training any model we need to check the
data and it's details. We will use the rtress.csv'
as your data for now. You can get the
rC bit.ly/trees csv dt
file either scanning the qr-code or http://bit.ly/trees_csv_dt
the link given -
Make sure to move the file your home
directory of lupyter Notebook and then
import the csv data. Before doing any
further action, let's have a look at
our raw data Scan Me!
Out[2]:
Index "Girth (in)" "Height (ft)" "Volume(ftA3)"
0 1 8.3 70 10.3
1 2 8.6 65 10.3
2 3 8.8 63 10.2
3 4 10.5 72 16.4
4 5 10.7 81 18.8
97 DATA ANALYSIS & PROCESSING
-------- '9
The first analysis is to know the shape of the
data or how amny rows and columns are present in
the data. We can do so by using the shape
attribute of the dataframe object
0ut[4]: (31, 4)
Out[5]:
Index "Girth (in)" "Height (ft)" "Volume(ftA3)"
0 1 8.3 70 10.3
1 2 8.6 65 10.3
2 3 8.8 63 10.2
3 4 10.5 72 16.4
4 5 10.7 81 18.8
5 6 10.8 83 19.7
6 7 11.0 66 15.6
7 8 11.0 75 18.2
8 9 11.1 80 22.6
9 10 11.2 75 19.9
Data Processing
Before feeding the data to models we need to
pre-process the data because the algorithms are
completely depended on the data so it must be
clean and appropriate as much as possible. While
finding skewness we found that you data is skewed
i.e. it needs to be closer to 0 for better
results, so let's look at some processes to ready
our data
----------------------------------------
^101 DATA ANALYSIS S PROCESSING
Scaling
Our data is spread over a wide range with
different scales i.e. not suitable to train
models. We need to bring our data in a more
appropriate scale, we can do so using the
MinMaxScater class and itJs fit_transform()
method of the scikit-learn library. We can scale
our data in the range of 0 to 1 which is the most
appropriate range for the algorithms
skl_dt.round(1).loc[5:10]
Out[28]:
S.No. Height Height Volume
print('L2 Normalization\n')
print(nm_ar[:5])
L2 Normalization
1.2576160130346932
1.2936614951212533
1.3179514295489714
1.0
1.0
1.0
' •/
104 DATA ANALYSIS S PROCESSING
--- '•
The code may a bit hard to understand because of
the for loop but let's try to understand it. We
normalize the data as we did we before but this
time we used the f12' method and printed the data
values. Then we printed the sum of the values of
first three rows of the normalized data but they
didn't sum upto 1. Next as the L2 method states,
we printed the sum of the squared values in the
first three rows using for loop which turned out
to be exactly 1
Before the for loop, we created an sm_row
varaible in which we will add our squared values
in the rows but we stored a string at start. Then
we created the outer loop in which we will get the
index of the rows. Also we entered one more number
in the list because at the first iteration the
string in the sm_row will be printed and after
printing it we changed it's value to 0 and then we
create the innner in which we will perform
addition. In each iteration of the inner loop we
will add the square of each element in the rows
with += compound assignment operator. After all
the values are sumed up, we return to the outer
loop and print it and again revert the value to 0
to store teh sum for the next row until all the
values are printed
----------------------- [Sum holder variable (vessel)]
Z ----
[Sum of. )
2nd Run)-
i-{1.0 )«---------
1.0
1.0 Output
\______________ ✓
' •/ '
105-- DATA ANALYSIS S PROCESSING
--- '•
Binarization
In binarization we binarize our data i.e. reduce
differences to only two to leave crisp vales with
a threshold. For exaple if we set the threshold to
10, all the value in a data set under 10 will be
converted to 0 and above 10 will be converted into
1. Let's binarize our data with Binarizer class
and transformO method
In [21]: import pandas
from sklearn import preprocessing
dt = pandas.read_csv('trees.csv')
ar = dt.values
Nm = preprocessing.Normalizer(norm=111')
nm_ar = Nm.transform(ar)
Bin = preprocessing.Binarizer(threshold=0.1)
bin_ar = Bin.transform(nm_ar)
bin_ar[10:16]
0
' •/ '
106-- DATA ANALYSIS S PROCESSING
--- '•
Standardization
Standardization or Standard scaling is the method
of changing the distribution of data arttributes
to Gausiann distribution (Normal distribution). In
this mthe mean is changes to 0 and standard devia
tion is changed to 1. Let's standardize our data
using the StandardScater class and it's fit()
and transform() methods
In [14]: import pandas
import numpy
from sklearn import preprocessing
dt = pandas.read_csv('trees.csv')
ar = dt.values
# Standardizer
Std = preprocessing.StandardScaler().fit(ar)
std_ar = Std.transform(ar)
print(std_ar[0:3])
print('Mean:',round(numpy.mean(std_ar),2))
print('Std.Deviation:',round(numpy.std(std_ar),2))
Label encoding
In many cases our data has more labels (word)
than features (numeric) but using words (strings)
in processing limits many activities. For that
purpose we need to change those labels into
numeric notations or features like the following
example
Out[15]:
Questions Answers
0 A True
1 B True
2 C False
3 D True
4 E False
0ut[17]:
Questions Answers
0 A 1
1 B 1
2 C 0
3 D 1
4 E 0
' •/ '
108 DATA ANALYSIS S PROCESSING
--- '•
As you can see that we had the Questions as A-E
and Answers as True or False. But we encoded the
Answers label to be 0(False) or l(True)
If we want we can get the label for the value or
decode the 0 or 1 values using the
inverse_transform() function
In [18]: import pandas
from sklearn import preprocessing
dt = pandas.DataFrame({'Questions':['A','B','C','D','E'],
'Answers':[’True', 'True','False','True','False']})
Enc = preprocessing.LabelEncoder()
Enc.fit(dt['Answers'])
# Encoded LabeLs
dt['Answers'] = Enc.transform(dt['Answers'])
# Decoding LabeLs
print(Enc.inverse_transform([0,1]))
['False' 'True']
o
13 data visualization
J
X______ / □------------------
Python has excellent libraries for data
visualization. A combination of Pandas., numpy and
matplotlib can help in creating in nearly all types
of visualizations charts. We can use the visuals to
better understand our data
Plotting data
We use numpy library to create the required
numbers to be mapped for creating the chart and the
pyplot module of matplotlib which draws the
actual chart
y
Editing labels and colors
As we already know matplotlib uses MATLAB symbols
as formatted strings to customize the colors [refer
to page 32] and how to add labels to the plots
Univariate plots
Univariate plots are the type of plots or visuals
with different plots for each variable to
understand them individually. We will use the
trees.csv data, which we used in the previous
lesson. First of all let's plot histogram for each
variable using the hist() function of the pandas
library
70 65 63 72 81 83 66 75 80 79 76 69 74 85 86 71 64 78 77 82 87
'Girth (in)'
z—• /
116 DATA VISUALIZATION
-
Multivariate plots
As it sounds, through multivariate plots we can
understand the realtions btween different
attributes or variables in a data. One of the
multivariate plots is a correlation matrix. We can
plot correlation matrix for our data like so:
0 12
I
We used the figure() function of the matlplotlib
library, along with the matrix we also added a color
bar to indicate the value of colors using the
correlations data i.e. calculated using the corr()
function. We added the matrix using the
add_subplot() function and 111 as argument i.e. it's
position. Then we used the matshow() function and
passed our correlations and min and max values.
117 DATA VISUALIZATION
- •
s 20
Ju.
••
• •
15
§ • • • • * .s 7’
(3
10
L •
80
70
mil
25
9
• V •’
a a R
•
e• •
•• •
------- ;-------- -—
•
8
11.
a a £
"Girth (in)" "Height (ft)’ "Volume(ft/K3)"
Ju.
• e •••
• •
•••
t a ’
uiL
• V
• What is
Regression
• Regressor
model
• Linear regression
o J
v
14 J REGRESSION
J
□------------
What is regression?
As we already know about the regression algorithm
which is a case of supervised machine learning
where we feed input data(numeric) and the algorithm
learns the patterns in the data and predicts the
output data. The performance of the algorithm is
entirely based on it's learning so we need to do
our best to feed the best data to our model. So
letJs create a regressor with the scikit-learn-’s
readymade algorithms
Regressor Model
LetJs create a simple regressor model to predict
the weight of a person if the height is given as
input value. You can download the file from here.
Then jump to your jupyter notebook
and import the following modules to
start the model http://bit.ly/HaitWet
import pandas
from sklearn import Linear_model
import sklearn.metrics
import matplotlib.pyplot
121 REGRESSION
Out[2]:
Index Height(ln) Weight(lbs)
0 1 65.78 112.99
1 2 71.52 136.49
2 3 69.40 153.03
3 4 68.22 142.34
4 5 67.79 144.30
40
20
0
A 65 70
0ut[5]: LinearRegression()
In [6]: RegModel.predict([[60]])
Out[6]: array([[99.93286131]])
Out[2]: LinearRegression()
140
130
120
110
100
64 65 66 67 68 69 70 71
Out[6]: 11.529538265252379
Linear Regression
As learnt earlier, regression algorithms find
relationship between two variables and predict the
values based on that relation. In linear
regression, the algorithm finds the linear
relationship between two variables i.e. if a
independent varaible is changed the dependent
variable will be affected. For example consider
the following dataset, we have data of different
squares with itJs length of one side and itJs
area. And it is clear that if the side
(independent variable) is changed say increased
the area (dependent variable) will
change(increase) too because area is calculated
from the length of the side
127 REGRESSION
140
130
120
no
100
64 65 66 67 68 69 70 71
Multiple Linear Regression
Multiple Linear Regression or MLR is the type of
regression in which we predict a value using two
or more features like predicting the weight of a
person using the age and height values. Here, the
regression line is calculated using the following
formula:
h(xj = bo + bixu + ... + bpXiP+
yt= h(xi) + ei
http://bit.ly/MallCust
import pandas
from sktearn import tinear_modet
from sktearn import metrics
from matptotlib import pyptot
from sktearn.modet_setection -
-import train_test_sptit
129 REGRESSION
and also import the data set and inspect the first
five rows using the headO function
0 1 Male 19 15 39
1 2 Male 21 15 81
2 3 Female 20 16 6
3 4 Female 23 16 77
4 5 Female 31 17 40
0 1 1 19 15 39
1 2 1 21 15 81
2 3 0 20 16 6
3 4 0 23 16 77
4 5 0 31 17 40
z—•
130 REGRESSION
0ut[4]: LinearRegression()
Out[31]: 52
131 REGRESSION
Out[5]: array([[60.5504497 ],
[62.09356734]])
132 REGRESSION
• Decision Tree I 0 n
"I I
• Logistic In 0
regression
• Nolve Boyes
I
I
o J
ar A
15 CLASSIFICATION
_____ / □-------------- J
Decision tree
Let's create a classifier to classify a customer
into which falvour he/she likes if the age and
gender are provided as input. To do so we will use
the decision tree, an algorithm that can perform
both classification and regression tasks. It learns
the categories in a dataset and creates categories
using a decision tree and then predicts the
category of an input
We will use the scikit learns
DecisionTreeClassifier to create our
model. You can download the data set
from here -
Now jump onto your jupyter notebook
and import the packages and modules
needed in this project
import pandas
from sklearn.tree import -
-DecisionT reeCtassifier
from sklearn.model_selection import
train_test_split (optional)
dt = pandas.read_csv('flavour.csv')
dt.head()
0 6 Male Chocolate
1 6 Female Strawberry
2 7 Male Chocolate
3 8 Female Strawberry
4 11 Male Butterscotch
Out[3]
Age Gender Flavour
0 6 1 Chocolate
1 6 0 Strawberry
2 7 1 Chocolate
3 8 0 Strawberry
4 11 1 Butterscotch
136 CLASSIFICATION
0ut[4]: DecisionTreeClassifier()
In [8]: CModel.predict([[30,0]])
137 CLASSIFICATION
140 CLASSIFICATION
z gini = 0.0
Ages 18.0
gini = 0.5
samples = 2
samples = 4
value = [0. 2. 0. 0. 0. 0. 0]
value = [2. 0. 0, 0. 2. 0. 0]
class = Strawberry y
class = Chocolate j
Logistic Regression
Logistic regression is a type of model that
predicts the outcome of output values as Yes or no
as numeric values 1 or 0 respectively. We can use
these type of models to classify a day as rainy or
not, a person as healthy or sick, etc. But there
are different types of logistic regression used
for to different situations.
import pandas
from sklearn import Linear_model
from sklearn.modet_setection import -
- train_test_split
from sklearn.metrics import accuracy_score
Out[2]:
Unnamed: 0 Temperature Humidity% Rain
0 0 34 74.2 Yes
1 1 19 68.2 No
2 2 28 67.2 Yes
3 3 29 66.6 Yes
4 4 26 57.9 Yes
0ut[4]: LogisticRegression()
he(x) = g(0Tx) [0 he sS 1 ]
gis the logistic or sigmoid function which can be
found with the following formula:
where z is 0Tx
• We can visualize the sigmoid curve can be
understood by the following graph
Out[2]:
Temperature Humidity% Rainfall
0 34 74.2 Low
1 19 68.2 No Rain
2 28 67.2 Moderate
3 29 66.6 Moderate
4 26 57.9 Low
...
0ut[4]: LogisticRegression()
Accuracy: 0.435156900860872
Naive Bayes
Naive Bayes algorithm is based on the Bayes
theorem which we already learned in previous
lessons. We have three types of Naive Bayes
algorithms:
• Gaussian, is used when the data in labels is
drawn from a gaussian distribution
• Multinomial, is used when the data in labels is
drawn from a multinomial distribution
• Bernoulli, is used when we have to predict
binary features like 0 or 1
So let's use the naive bayes algorithm to create
to model to predict whether it will rain or not
with the dataset used in the Binomial logistic
regression. We will use the Gaussian Naive Bayes
algorithm for this model
Out[3]: GaussianNB()
Accuracy: 0.9275
o
X
16
_____ /
LOGICAL PROGRAMMING
__________________________________________________________ j
Creating Logics
We will use kanren package for logic programming
so open your anaconda prompt and install pip with
conda install pip
□ Anaconda Prompt (anaconda!)
Out[2]: ~log
Out[3]: (5J
0ut[4]: ('A',)
Out[5]: (2.4,)
Heirarchical logics
We can also create facts with heirarchies or
parent-child relationship. For that we need to use
the Relation class and create fact using factsC)
function
Out[l]: ('Milk',)
o J
A
17
_____ /
natural language processing
1________________________________________________________ J
Tokenization
Tokenization is the simplest form of breaking down
text data into smaller parts like words,
punctuations or sentences
value [money
was] [and
Refresh
Server Index:
Out[3]: ['The',
'product',
'was',
'good',
■ ■
'lightweightand',
'value',
'for',
'money',
■!']
Out[8]: ['The',
'product',
'was',
'good',
I I
J J
'lightweightand',
'value',
'for',
'money',
■!']
Stemming
The text data can contain different forms of
verbs like go, gone or went. For a unified
approach we need to bring back those into base
forms i.e. called stemming
it
has > ha
scratches > scratch
The
product
is
fairly
good
but
it
has > ha
scratches > scratch
Feature extraction
In the 12th lesson we learned about label
encoding i.e. changing labels or string attributes
in a data into features or numeric notations.
Doing so helps the machine learning algorithms to
perform various activities with them. The same can
be said for the text data, even if we tokenize
text data it is still a raw text which cannot be
processed by the algorithms
So we need to encode these tokens into numeric
values to be useful of the algorithms. We can do
so using the Bag of Word Model or feature
extraction i.e. a process which involves
extracting features(unique vocabulary) from the
text data and then it creates a model or matrix
with the weight of each feature in each sentence
in the text data. This matrix is the feature
vectors. So let's say we have the following text
Oranges are orange
But black current are purple
So let's extract the features first and for the
same we will use CountVectorizer from sklearn
Text:
Oranges are orange
But black current are purple
Features:
are, black, but, currants, orange, oranges, purple
Matrix:
[[i, a, e, 0, i, i, e],
[1, 1, 1, 1, 0, 0, 1]]
Features:
[are,] black, but, currants, [orange,] [oranges,] purple
Sent|ence 1; :
[Oranges] [are] [orange]
I i~~~
Matrix: \
[ 0, 0j
[1, 1. b
Out[3]: {'oranges': 5,
'are': 0,
'orange': 4,
'but': 2,
'black': 1,
'currants': 3,
'purple': 6}
import pandas
from sktearn.feature_extraction.text
import CountVectorizer, Tfidf-
-T ransformer Scan Me!
In [2]: dt.head()
Out[2]:
Reviews Rating
0ut[4]: DecisionTreeClassifier()
____________________________________________
• Reoding on
audio file
• Extracting
features
• Speech
recognizer
o
A
18
k ______ /
SPEECH RECOGNITION
T )
Extracting features
After importing the audio we need extract
features from the file. There are a lot of ways to
extract features from audio files like MFCC i.e.
Mel-Frequency Cepstral Coefficients. Simply you
need to know that MFCC is the Coefficients that
make up the Mel-frequency cepstral where
Mel-frequency is a scaling process which scales
the audio at human audible range and cepstrum is
the information of rate of change in spectral bands
171 SPEECH RECOGNITION
0 2 4 6 8 10 12
Speech Recognizer
Previously we imported audio files, visualized it
and also extracted it's feature but even though if
we want to create a speech recognizer from scratch
it will take a lot of time that's why we will use
the Speech Recognition package
Install it using the below code bit.ly/Recog aud
K
in your anaconda prompt : http://bit.ly/Recog_aud
SRec.recognize_google(aud)
o
r A
Al ALGORITHMS
k ____________________________)
As mentioned earlier we can use Al algorithms for
solving many problems i.e. called heuristic search.
Like we have some conditions needed to be met and
then find the answer i.e. Constraint Satisfaction
Problems. We can use search algorithms to solve
problems with constraints which have a lot of
possible anwers and finding them manually will take
a lot of time. The search algorithm will search
through every possible answer and return the best
answer
Satisfying constraints
Let's say we have a problem like -
2x + 3 = y
prb = constraint.Problem()
z—• /
176 Al ALGORITHMS
prb.addVariable('x',range(10))
prb.addVariable('y'>range(10))
{'x' ■y.; 9}
3,
{'x* ■y.; 7}
2,
{’X* ■y.; 5}
1,
{•x‘ 8, ■y': 3}
177 Al ALGORITHMS
•
2x + 3 = y
2(3) + 3 = (9)
6 + 3 = 9
9 = 9
Unique combinations
Previously we learned how to use the search
algorithm to find out unknown values. But let's
say we want a unique combinations of day and night
duty of lake, Mike and John where they share equal
work and holidays in a week (except Sunday). We
can use the AULDifferentConstraint0 and get the
unique combinations
In [2]: matrix
o
A
20
______ /
ARTIFICIAL NEURAL NETWORKS
T_______________________________________________________________________________ J
Have you ever wondered how do you remember
everything or reading this book now? It's pretty
obvious that our brain is behind everything but
what's inside it that make it happen? Speaking
biologically, our brain consists of
trillions of neurons or nerve cells
that create a neural network.
These neurons work together to
help us think, decide, etc. But
we are not going that deep here,
instead what if we have the
machines have the same ability as
our neural networks to learn, think,
decide and perform tasks like us. Well
that's known as Artificial Neural Networks or ANN.
We aren't going to learn how they work or things
behind the scene because it needs another dedicated
book about it but it falls under Al which we are
learning in this book so let's use it
0ut[4]: array([[1.]])
Layers of networks
Previously we created a network with one node or
neuron with single layer i.e. iy will evaluate
both the input and output but let's say we want to
add more neurons or layers. We can achieve so
using the newffC) function so let's import it and
create our input and output data
I
In [1]: from neurolab.net import newff
X = [[e,o],[i,0],[i,i]]
y = [[©],[i],[2]]
Out[4]: array([[0.99982076]])
21
___________ /
DEEP LEARNING
__________________________________________ J
dt = read_csv('height_and_weight.csv')
In [2]: dt.values[:5]
In [3]: dt = dt.values
X = dt[:, 1]
y = dt[:, 2]
0ut[6]: 2
Z—•
Epoch 1/10
180/180 [------------------------------------------------------- ] -
- val_mean_absolute_error: 139.4882
Epoch 2/10
180/180 [------------------------------------------------------- ] -
- val_mean_absolute_error: 121.8028
Epoch 3/10
180/180 [==============================] -
val_mean_absolute_error: 90.9186
Epoch 4/10
180/180 [------------------------------------------------------- ] -
al_mean_absolute_error: 64.9408
Epoch 5/10
180/180 [-=============================] -
al_mean_absolute_error: 51.0282
Epoch 6/10
180/180 [=————-------------- =-=----------- ] -
al_mean_absolute_error: 33.7814
Epoch 7/10
180/180 [-====-============-=-===-====-] -
al_mean_absolute_error: 12.2725
Epoch 8/10
180/180 [---------- =--------- =------------------------------- ] -
mean_absolute_error: 8.9072
Epoch 9/10
180/180 [------------------------------------------------------- ] -
mean_absolute_error: 8.8819
Epoch 10/10
180/180 [---------- =--------- =------------------------------- ] -
mean_absolute_error: 8.8243
0ut[8]: tensorflow.python.keras.callbacks.History
6 1D
No of Epochs
# Training
MAE = LossHistory()
Z—•
RegModel.fit(X, y,
epochs=10, batch_size=l,
verbose=l, validation_split=0.1,
callbacks=[MAE])
# Loss vaLues
MAE.losses
RegModel.summary()
Model:: "sequential"
Total params: 18
Trainable params: 18
Non-trainable params: 0
o
Till now we have learned how to replicate human
thinking with various methods but let's look at
the another end of the story i.e. visuals.
Training machines to view the world as we see i.e.
in 3-D and interpret on that information is called
computer vision
Processing images
You may think how can machines
view the world as we see? How is
that is even possible? So let's
move a bit back to the NLP lesson
where we learned to teach models
to work with human language. It
seemed a question to that the
computers which only works with
binary digits (0 and 1) can
understand human language and now that machine
has vision too. Based on the NLP lesson, what do
you think enables machines to have vision?
First off, how do you think you are able to see
images in your devices. Image files are stored as
.png, .jpg, .svg, etc. formats but how do they
store an image digitally? I want you to naviage to
any image on your desktop
monitor.png
* Dataset (X:)
Photos
Photos are easily enhanced, organized for you, and
ready to edit and share,
Other options
Notepad
Paint
v
R'Mrt* on
Tweaking images
So let's import images first and perform a little
bit of tweaking on it. To do so we need to install
the opencv library using anconda
100
200
300
400
500
600
0ut[4]: True
100
200
300
400
500
600
Face recognition
Using opencv we create a face recognition model.
You can choose any image you want. For
demonstration, I will use the following image:
In [4]: cv2.imwrite('Detected-Faces.png',img)
0ut[4]: True
• -------------
208 COMPUTER VISION
—
211 Al APPLICATION
•
. X .
X . .
212 Al APPLICATION
•
X . .
0 . .
X . .
then again it's your turn and you pass 5 this time
0 . .
. X .
X . .
213 Al APPLICATION
•
def possible_moves(self):
return [
enumerate(self.board) ]
def possible_moves(self):
return [
enumerate(self.board) if y == 0]
214 Al APPLICATION
-J
•
XXX 12 3
XXX 4 5 6
XXX 7 8 9
X . . 1 . .
X . . 4 . .
X . . 7 . .
. X . . 2 .
. X . . 5 .
. X . . 8 .
. . X . . 3
. . X . . 6
. . X . . 9
X . . 1 . .
. X . . 5 .
. . X . . 9
. . X . . 3
. X . . 5 .
X . . 7 . .
216 Al APPLICATION
. X .
. X .
. X .
| return
[1,4,7], [2,5,8], [3,6,9], [1,5,9], [3,5,7]]
([
217 Al APPLICATION
•
The game will end when all the boxes in the borad
are filled i.e. after 9 turns or if a winning
combination is made. Note that we are calling the
condition_for_Tose() method inside of another
method. Now after we have defined the rules and
all the conditions we need to print the board or
matrix too so let's define the showO function
def show(self):
# Printing the board
print(’\n'+'\n'.join([' ’. join( [ [' . ’, 'O', ’X1]
[self.board[3*j + i]]
for i in range(3)]) for j in range(3)]))
219 Al APPLICATION
•
220 Al APPLICATION
__________
0 . .
0 . .
. X .
0 . .
0 . .
. X .
0 . .
0 . .
X X .
0 . .
0 . .
X X 0
0 . .
0 X .
X X 0
0 . .
0 X .
X X 0
0 0 .
0 X .
X X 0
0 0 X
OXO
X X 0
0 0 X
o------
24°(---------------
(---- Al APPLICATION 2
-------------------------- y
import pandas
from sktearn.tree import \
DecisionT reeCLassifier
from sktearn.feature_extraction.text import \
CountVectorizer, TfidfTransformer
r*----------------------------------------------------
226 Al APPLICATION
Out[l]:
Title
0 It's Python
Description
Keywords Price
In [4]: wrds = []
for ele in com.getSolutions():
wrds.append(f"{ele['a']} "+f "{ele [ ’ b ' ]}")
wrds
for k in wrds:
keywrds[f"B{i+1}"].append(k)
In [6]: keywrds
In [7]: Dt = []
for bk in list(keywrds.keys()):
for ks in keywrds[bk]:
Dt.append([ks,bk])
Out[8]:
Keywords Book
0 programming python B1
1 programming beginners B1
2 programming for B1
3 programming python B1
4 python programming B1
enc = CountVectorizer()
enc.fit(inp)
e_inp = enc.transform(inp)
tf = TfidfTransformer()
tf.fit(e_inp)
X = tf.transform(e_inp)
Out[10]: DecisionTreeClassifier()
<Title>
description ...
... >
<Price>
9/ s
234 Al APPLICATION
Description:
{desc}
Price:{price}"""
print(result)
In [*]: search_book()
In [12]: search_book()
Tittle:
It's Python
Description:
A beginners guidebook to programming with Python. We
f statements, creating loops and how to iterate over
Price:$3.00
In [*]: search_book()
In [13]: search_book()
Tittle:
Data analysis with Python
Description:
Learn how to perform different types of analytics on
ncoding, Scaling., Standard deviations, interquartile
Price:$3.00
if Type == 'text':
query = input('Search: ')
elif Type == 'voice':
vois = speech_recognition.Recognizer()
with speech_recognition.Microphone() as a:
vois.adjust_for_ambient_noise(a)
print("Your Query:")
aud = vois.listen(source)
query = vois.recognize_google(aud)
if Type == 'text':
query = input('Search: ')
elif Type == 'voice':
vois = speech_recognition.Recognizer()
with speech_recognition.Microphone() as a:
vois.adjust_for_ambient_noise(a)
print("Your Query:")
aud = vois.listen(source)
query = vois.recognize_google(aud)
enc_q = enc.transform([query])
tf_q = tf.transform(enc_q)
bk = CModel.predict(tf_q)
Description:
{desc}
Price:{price}
• F*------------------------------------------
241 Al APPLICATION
-J
•
In [ ]: search_book(Type='voice')
■x
26 APPLICATION 4
• Captcho AV
solver
J
A
Al APPLICATION 4
i__________________________________________ >
http://bit.ly/caPtcha
Scan Me!
image = cv2.imread('test_image.jpg')
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
In [5]: bool((rects).all())
Out[5]: True
humn_img = []
humn_img = []
hog = cv2.HOGDescriptor()
hog.setSVMDetector(
cv2.HOGDescriptor_getDefaultPeopleDetector())
for img in images:
rects, weights = 0,0
Img = cv2.cvtColor(cv2.imread(img),
cv2.COLOR_BGR2GRAY)
return humn_img
Out[7]:
Labels Check
0 captcha\Captcha1\Caplmg1.jpg 1
1 captcha\Captcha1\Caplmg2.jpg
2 captcha\Captcha1\Caplmg3.jpg 3
3 captcha\Captcha1\Caplmg4.jpg
4 captcha\Captcha1\Caplmg5.jpg
5 captcha\Captcha1\Caplmg6.jpg 6
out[8]: [1, 3, 6]
Al
APPLICATION 5
• Computer vision
with deep learning
from scratch
o
z
Al APPLICATION 5
X. T___________________________________ J
https://www.microsoft.com/en-us/download/details.aspx?id=54765
k?
But the only downside of downloading from the
official site is that it is not organized into
train-test sets and also contains some errors.
If you donJt want to do the splliting manually you
can download it from here http://bit.Ly/Dog_Cats,
though it consists of 25,000 images
http://bit.ly/Dog_Cats
--------- 1 ziP I
252 Al APPLICATION
--- '•
If you download the dataset from the official
source, you will recieve a zip file. After
unzipping it you will see 3 things, license .docx
file, ReadMeFile.txt and Petimages folder with all
the images of the Cats and Dogs in seperate
folders
• You need to drop the Petimages folder into your
jupyter notebook home
• The dataset contains 25000 images with half of
the images of dogs and the other half of cats.
After that you need to remove the 666.jpg from the
Cats folder and 11702.jpg from the Dogs folder.
They will cause error because they are corrupted
files
• Then you need to create another folder like
Testimages and create Cat_test and Dog_test in it
* Quick access
□ Desktop
i Downloads
* Cat_test Dog_test
0 Pictures *
D Captcha2
□ Cat
D Music
Edges Corners
• Next is the
convolutional layer.
All the computations
of the features and
parameters in the
image are
calculated
here. The
neurons takes
the output of
the layer
i.e.
features
features, etc.
(from
ahead
pixeLs)
• F*------------------------------------------
256 Al APPLICATION
-J
•
CModel.compile(optimizer = 'adam',
loss = 'binary_crossentropy',
metrics = ['accuracy'])
In [5]: X = X_datagen.flow_from_directory(
"Petimages",
target_size = (64, 64),
batch_size = 32, class_mode = 'binary')
y = y_datagen.flow_from_directory(
'Testimages', target_size = (64, 64),
batch_size = 32, class_mode = 'binary')
261 Al APPLICATION
• -J
Epoch 1/25
625/625 [==============================] - 67s
0.6976
Epoch 2/25
625/625 [==============================] - 67s
0.7508
Epoch 3/25
625/625 [==============================] - 67s
0.7560
Epoch 4/25
61/625 [ = > ] - ETA:
Epoch 1/25
after a
625/625 [============================== ] - 67s 107ms
white
0.6976
Epoch 2/25
625/625 [============================== ] - 67s 107ms
0.7508
Epoch 3/25
625/625 [============================== ] - 67s 107ms
0.7560
Epoch 4/25
218/625 [=========> ] - ETA: 37s
9/ \
262 Al APPLICATION
- J—
S Performance £ # X
CPU 34% q / 0/ 100
GPU 10% *
RAM 37%
FPS —
60 SECONDS 0
Performance H X
CPU 39% QQ0/ 100
GPU 9% *
RAM 36%
FPS -
60 SECONDS 0
Epoch 1/25
625/625 [==============================] - 67s 107ms/step - loss: 0.5553 - accu
racy: 0.7175 - val_loss: 0.5793 - val_accuracy: 0.6976
Epoch 2/25
625/625 [==============================] - 67s 107ms/step - loss: 0.5372 - accu
racy: 0.7281 - val_loss: 0.5152 - val_accuracy: 0.7508
Epoch 3/25
625/625 [==============================] - 67s 107ms/step - loss: 0.5263 - accu
racy: 0.7387 - val_loss: 0.5028 - val_accuracy: 0.7560
Epoch 4/25
625/625 [==============================] - 67s 107ms/step - loss: 0.5176 - accu
racy: 0.7395 - val_loss: 0.5108 - val_accuracy: 0.7410
Epoch 5/25
625/625 [==============================] - 67s 106ms/step - loss: 0.5091 - accu
racy: 0.7453 - val_loss: 0.4910 - val_accuracy: 0.7628
Epoch 6/25
625/625 [==============================] - 67s 107ms/step - loss: 0.5049 - accu
racy: 0.7466 - val_loss: 0.5041 - val_accuracy: 0.7572
Epoch 7/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4925 - accu
racy: 0.7548 - val_loss: 0.4846 - val_accuracy: 0.7628
Epoch 8/25
625/625 [==============================] - 67s 108ms/step - loss: 0.4889 - accu
racy: 0.7611 - val_loss: 0.4869 - val_accuracy: 0.7676
Epoch 9/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4860 - accu
racy: 0.7606 - val_loss: 0.5165 - val_accuracy: 0.7506
Epoch 10/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4798 - accu
Epoch 11/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4794 - accu
racy: 0.7636 - val_loss: 0.4827 - val_accuracy: 0.7696
Epoch 12/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4709 - accu
racy: 0.7701 - val_loss: 0.4784 - val_accuracy: 0.7686
Epoch 13/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4677 - accu
racy: 0.7713 - val_loss: 0.5009 - val_accuracy: 0.7630
Epoch 14/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4648 - accu
racy: 0.7733 - val_loss: 0.4746 - val_accuracy: 0.7684
Epoch 15/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4576 - accu
racy: 0.7768 - val_loss: 0.4922 - val_accuracy: 0.7710
Epoch 16/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4528 - accu
Epoch 17/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4477 - accu
racy: 0.7861 - val_loss: 0.4746 - val_accuracy: 0.7680
Epoch 18/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4454 - accu
racy: 0.7846 - val_loss: 0.5438 - val_accuracy: 0.7480
Epoch 19/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4422 - accu
racy: 0.7877 - val_loss: 0.5549 - val_accuracy: 0.7518
Epoch 20/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4372 - accu
racy: 0.7895 - val_loss: 0.4832 - val_accuracy: 0.7684
• F*--------------------------------------------------------------------------------------
264 Al APPLICATION
Epoch 21/25
625/625 [==============================] - 68s 109ms/step - loss: 0.4358 - accu
racy: 0.7901 - val_loss: 0.6326 - val_accuracy: 0.7062
Epoch 22/25
625/625 [==============================] - 69s lllms/step - loss: 0.4342 - accu
racy: 0.7943 - val_loss: 0.4914 - val_accuracy: 0.7788
Epoch 23/25
625/625 [==============================] - 70s 112ms/step - loss: 0.4290 - accu
racy: 0.7976 - val_loss: 0.4847 - val_accuracy: 0.7762
Epoch 24/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4284 - accu
racy: 0.7960 - val_loss: 0.5695 - val_accuracy: 0.7428
Epoch 25/25
625/625 [==============================] - 67s 107ms/step - loss: 0.4225 - accu
racy: 0.7998 - val_loss: 0.5150 - val_accuracy: 0.7774
266 Al APPLICATION
• -J
pred = CModel.predict(img)
if pred[0][0] == 1:
pyplot.imshow(
image.imread('TestImage.jpeg'))
pyplot.axis(’off')
pyplot.title('Dog')
else:
pyplot.imshow(
image.imread('TestImage.jpeg'))
pyplot.axis(’off')
pyplot.title('Cat')
Dog
# Quick access
Q Desl *
Downloads ✓
[Q Docum *
O Pictures * Cat Dog
rn Captcha^
□ Cat
rn d
n Music
• Human or artificial
intelligence
• Datasets
o
A
28 INTELLIGENCE?
i__________________________________________ )
datasets y