Python
Python
PYTHON PROGRAMMING
s t r 1 = input ( ” Enter a S t r i n g : ” )
print ( s t r 1 )
# T h i s i s P y t h o n comment
”””
This i s paragraph s y n t a x .
T h i s can be a l s o u s e d f o r m u l t i −l i n e comment .
”””
# V a r i a b l e s and A s s i g n m e n t s
v a r 1 = 10
print ( var 1 )
type ( var 1 )
# p r in t ( type ( var 1 ) )
# B y t e s Data T y p e s
# C r e a t e s b y t e s t y p e o f data which i s immutable
var 1 = b” Farid ”
print ( var 1 )
# T y p e s o f B u i l t −i n Data S t r u c t u r e s i n P y t h o n :
# L i s t , Tuple , Sets , D i c t i o n a r y .
# A l i s t comprises a sequence of objects , u s u a l l y represented using square brackets
# w i t h commas b e t w e e n t h e i t e m s i n t h e s e q u e n c e .
m y l i s t = [ ] # c r e a t e empty l i s t
BASIC PYTHON PROGRAMMING 5
print ( my list )
m y l i s t = [ 1 , 2 , 3 , ’ example ’ , 3 . 1 3 2 ] # c r e a t i n g l i s t with data
print ( my list )
# Adding Elements
m y l i s t = [1 , 2 , 3]
print ( my list )
# The append ( ) a d d s a l l t h e e l e m e n t s p a s s e d t o i t a s a s i n g l e e l e m e n t .
m y l i s t . a p p e n d ( [ 5 5 5 , 1 2 ] ) # add a s a s i n g l e e l e m e n t
print ( my list )
# The e x t e n d ( ) a d d s t h e e l e m e n t s one−by−one i n t o t h e l i s t .
m y l i s t . e x t e n d ( [ 2 3 4 , ’ m o r e e x a m p l e ’ ] ) # add a s d i f f e r e n t e l e m e n t s
print ( my list )
# The i n s e r t ( ) a d d s t h e e l e m e n t p a s s e d t o t h e i n d e x v a l u e
# and i n c r e a s e t h e s i z e o f t h e l i s t t o o .
m y l i s t . i n s e r t ( 1 , ’ i n s e r t e x a m p l e ’ ) # add e l e m e n t i
print ( my list )
# Deleting Elements
m y l i s t = [ 1 , 2 , 3 , ’ example ’ , 3 . 1 3 2 , 10 , 30]
# The d e l k e y w o r d w h i c h i s b u i l t −i n i n t o P y t h o n
# but t h i s does not r e t u r n a n y t h i n g back t o us .
del my list [5] # d e l e t e element at index 5
print ( my list )
# To remove an e l e m e n t by i t s v a l u e , you u s e t h e remove ( ) .
m y l i s t . remove ( ’ e x a m p l e ’ ) # remove e l e m e n t w i t h v a l u e
print ( my list )
# I f we want t h e e l e m e n t back , we can u s e t h e pop ( ) f u n c t i o n w h i c h t a k e s t h e i n d e x v a l u e .
a = m y l i s t . pop ( 1 ) # pop e l e m e n t f r o m l i s t
p r i n t ( ’ Popped E l e m e n t : ’ , a , ’ L i s t r e m a i n i n g : ’ , m y l i s t )
m y l i s t . c l e a r ( ) # empty t h e l i s t
print ( my list )
# Accessing Elements
m y l i s t = [ 1 , 2 , 3 , ’ example ’ , 3 . 1 3 2 , 10 , 30]
f o r e l e m e n t i n m y l i s t : # a c c e s s e l e m e n t s one by one
print ( element )
print ( my list ) # access a l l elements
print ( my list [ 3 ] ) # access index 3 element
p r i n t ( m y l i s t [ 0 : 2 ] ) # a c c e s s e l e m e n t s f r o m 0 t o 1 and e x c l u d e 2
print ( my list [:: −1]) # access elements in reverse
# Other Functions
m y l i s t = [1 , 2 , 3 , 10 , 30 , 10]
print ( len ( my list ) ) # find length of l i s t
print ( my list . index (30)) # f in d index of element th at occurs f i r s t
print ( my list . count (10)) # f in d count of the element
p r i n t ( s o r t e d ( m y l i s t ) ) # p r i n t s o r t e d l i s t but not change o r i g i n a l
m y l i s t . s o r t ( r e v e r s e =True ) # s o r t o r i g i n a l l i s t
print ( my list )
# Copy c o n t e n t o f l i s t 1 t o l i s t 2
l i s t 1 = [10 , 20 , 30 , 40 , 50]
l i s t 2 = l i s t 1 . copy ( )
print ( l i s t 2 )
6 PYTHON PROGRAMMING
# Concatenation
list 3 = list 1 + list 2
print ( l i s t 3 )
# A t u p l e i s a P y t h o n c o l l e c t i o n t h a t i s e x t r e m e l y s i m i l a r t o a l i s t , w i t h some s u b t l e d i f f e r e n c e s .
# For s t a r t e r s , t u p l e s a r e i n d i c a t e d u s i n g p a r e n t h e s e s i n s t e a d o f s q u a r e b r a c k e t s :
my tuple = (1 , 2 , 3)
print ( my tuple )
m y t u p l e = m y t u p l e + ( 4 , 5 , 6 ) # add e l e m e n t s
print ( my tuple )
# Other Functions
my tuple = (1 , 2 , 3 , [ ’ java ’ , ’ python ’ ] )
print ( my tuple )
my tuple [ 3 ] [ 0 ] = ’ j u l i a ’
print ( my tuple )
print ( my tuple . count ( 2 ) )
print ( my tuple . index ( [ ’ j u l i a ’ , ’ python ’ ] ) )
# Creating a set
m y s e t = 1 , 2 , 3 , 4 , 5 , 5 , 5
print ( my set )
# Adding e l e m e n t s t o s e t
m y s e t . add ( 7 )
print ( my set )
# Operations in s e t s
m y s e t = 1 , 2 , 3 , 4
m y s e t 2 = 3 , 4 , 5 , 6
# The u n i o n ( ) f u n c t i o n c o m b i n e s t h e d a t a p r e s e n t i n b o t h s e t s .
p r i n t ( m y s e t . u n i o n ( m y s e t 2 ) , ’−−−’ , m y s e t m y s e t 2 )
# The i n t e r s e c t i o n ( ) f u n c t i o n f i n d s t h e d a t a p r e s e n t i n b o t h s e t s o n l y .
p r i n t ( m y s e t . i n t e r s e c t i o n ( m y s e t 2 ) , ’−−−’ , m y s e t & m y s e t 2 )
# The d i f f e r e n c e ( ) f u n c t i o n d e l e t e s t h e d a t a p r e s e n t i n b o t h
# and o u t p u t s d a t a p r e s e n t o n l y i n t h e s e t p a s s e d .
p r i n t ( m y s e t . d i f f e r e n c e ( m y s e t 2 ) , ’−−−’ , m y s e t − m y s e t 2 )
# The s y m m e t r i c d i f f e r e n c e ( ) d o e s t h e same a s t h e d i f f e r e n c e ( ) f u n c t i o n
# but o u t p u t s t h e data which i s remaining i n both s e t s .
p r i n t ( m y s e t . s y m m e t r i c d i f f e r e n c e ( m y s e t 2 ) , ’−−−’ , m y s e t ˆ m y s e t 2 )
my set . c l e a r ( )
print ( my set )
# D i c t i o n a r i e s a r e u s e d t o s t o r e key−v a l u e p a i r s .
# I t h a s k e y s and v a l u e s t h a t a r e a s s o c i a t e d w i t h e a c h o t h e r .
m y d i c t = # e m p t y d i c t i o n a r y
print ( my dict )
my dict = 1: ’ Python ’ , 2: ’ Java ’ # d i c t i o n a r y with e l e m e n t s
BASIC PYTHON PROGRAMMING 7
print ( my dict )
# D e l e t i n g key , v a l u e p a i r s
# To d e l e t e t h e v a l u e s , you u s e t h e pop ( ) f u n c t i o n
# which r e t u r n s t h e v a l u e t h a t has been d e l e t e d .
# To r e t r i e v e t h e key−v a l u e p a i r , we can u s e t h e p o p i t e m ( )
# f u n c t i o n w h i c h r e t u r n s a t u p l e o f t h e k e y and v a l u e .
# To c l e a r t h e e n t i r e d i c t i o n a r y , you u s e t h e c l e a r ( ) f u n c t i o n .
m y d i c t = ’ F i r s t ’ : ’ P y t h o n ’ , ’ Second ’ : ’ J a v a ’ , ’ T h i r d ’ : ’ J u l i a ’
a = m y d i c t . pop ( ’ T h i r d ’ ) # pop e l e m e n t
p r i n t ( ’ Value : ’ , a )
print ( ’ Dictionary : ’ , my dict )
b = m y d i c t . p o p i t e m ( ) # pop t h e key−v a l u e p a i r
p r i n t ( ’ Key , v a l u e p a i r : ’ , b )
print ( ’ Dictionary ’ , my dict )
m y d i c t . c l e a r ( ) # empty d i c t i o n a r y
print ( ’ Dictionary ’ , my dict )
# M u l t i −d i m e n t i o n a l D i c t i o n a r y
f r u i t s = ’ Fruit name ’ :
’ Apple ’ :
’ C o l o r ’ : ’ Red ’ ,
’ Shape ’ : ’ Round ’
,
’ Orange ’ :
’ C o l o r ’ : ’ Orange ’ ,
’ Shape ’ : ’ Round ’
f r u i t s [ ’ Fruit name ’ ]
f r u i t s [ ’ F r u i t n a m e ’ ] [ ’ Apple ’ ]
# P y t h o n C o n t r o l Flow
# For−Loops
sum = 0
f o r i i n range ( 1 0 ) :
sum = sum + i
8 PYTHON PROGRAMMING
p r i n t ( sum )
a l t e r n a t i v e s u m = 0+1+2+3+4+5+6+7+8+9
p r i n t ( a l t e r n a t i v e s u m ==sum )
f o r i i n range ( 1 0 ) :
i f i % 2 == 0 : # % −− m o d u l u s o p e r a t o r −− r e t u r n s t h e r e m a i n d e r a f t e r d i v i s i o n
p r i n t ( ” i s e v e n ” . format ( i ) )
else :
p r i n t ( ” i s odd ” . format ( i ) )
# Example o f i f −e l i f −e l s e c o n d i t i o n
var 1 = 3
i f v a r 1 % 2 == 0 :
p r i n t ( ” V a r i a b l e i s d i v i s i b l e by 2 . ” )
e l i f v a r 1 % 3 == 0 :
p r i n t ( ” V a r i a b l e i s d i v i s i b l e by 3 . ” )
else :
p r i n t ( ” V a r i a b l e i s n e i t h e r d i v i s i b l e by 2 n o r by 3 . ” )
# Syntax of f u n c t i o n in Python
def function name ( parameters ) :
statement ( s )
# Define A Function
def h e l l o ( ) :
p r i n t ( ” H e l l o World ” )
return
hello ()
# Creating a class
c l a s s MyClass :
v a r 1 = 10
x = MyClass
print ( x . var 1 )
# Create a c l a s s
c l a s s SuperMan :
d e f power ( s e l f ) :
p r i n t ( ” SuperMan c a n f l y . ” )
x = SuperMan ( )
x . power ( )
# The s e l f p a r a m e t e r i s a r e f e r e n c e t o t h e c u r r e n t i n s t a n c e o f t h e c l a s s ,
# and i s u s e d t o a c c e s s v a r i a b l e s t h a t b e l o n g s t o t h e c l a s s .
NUMPY LIBRARY 9
The NumPy library endows Python with a host of scientific computing capabilities.
Chief among these is the Array object, which provides a multidimensional way to
organise values of the same type. NumPy arrays allow slicing and indexing similar to
lists. Most importantly, NumPy has a formidable number of mathematical operations
that can be used to transform arrays and perform computations between arrays.
# Indexing / S l i c i n g examples
p r i n t (A[ 0 , : ] ) # i n d e x t h e f i r s t ”row” and a l l c o l u m n s
p r i n t (A[ 1 , 2 ] ) # i n d e x t h e s e c o n d row , t h i r d column e n t r y
p r i n t (A [ : , 1 ] ) # i n d e x e n t i r e s e c o n d column
# A r i t h m e t i c Examples
C = A ∗ 2 # m u l t i p l i e s e v e r y e l e m n t o f A by two
D = A ∗ B # elementwise m u l t i p l i c a t i o n rather than matrix m u l t i p l i c a t i o n
E = np . t r a n s p o s e ( B )
F = np . matmul (A, E ) # p e r f o r m s m a t r i x m u l t i p l i c a t i o n −− c o u l d a l s o u s e np . d o t ( )
G = np . matmul (A, x ) # p e r f o r m s m a t r i x −v e c t o r m u l t i p l i c a t i o n −− a g a i n c o u l d a l s o u s e np . d o t ( )
p r i n t ( ” \n M a t r i x E ( t h e t r a n s p o s e o f B ) : \n ” )
print (E)
p r i n t ( ” \n M a t r i x F ( r e s u l t o f m a t r i x m u l t i p l i c a t i o n A x E ) : \n ” )
print (F)
p r i n t ( ” \n M a t r i x G ( r e s u l t o f m a t r i x −v e c t o r m u l t i p l i c a t i o n A∗x ) : \n ” )
p r i n t (G)
# B r o a d c a s t i n g Examples
H = A ∗ x # ” b r o a d c a s t s ” x f o r e l e m e n t −w i s e m u l t i p l i c a t i o n w i t h t h e rows o f A
p r i n t (H)
J = B + x # b r o a d c a s t s f o r a d d i t i o n , a g a i n a c r o s s rows
print ( J )
# max o p e r a t i o n e x a m p l e s
X = np . a r r a y ( [ [ 3 , 9 , 4 ] , [ 1 0 , 2 , 7 ] , [ 5 , 1 1 , 8 ] ] )
a l l m a x = np . max (X) # g e t s t h e maximum v a l u e o f m a t r i x X
column max = np . max (X, a x i s = 0 ) # g e t s t h e maximum i n e a c h column −− r e t u r n s a rank −1 a r r a y [ 1 0 , 1 1 , 8 ]
row max = np . max (X, a x i s = 1 ) # g e t s t h e maximum i n e a c h row −− r e t u r n s a rank −1 a r r a y [ 9 , 1 0 , 1 1 ]
# I n a d d i t i o n t o max , can s i m i l a r l y do min . Numpy a l s o h a s argmax t o r e t u r n i n d i c e s o f maximal v a l u e s
c o l u m n a r g m a x = np . argmax (X, a x i s = 0 ) # n o t e t h a t t h e ” i n d e x ” h e r e i s a c t u a l l y t h e row t h e maximum o c c u r s f o r
p r i n t ( ” M a t r i x X : \n ” )
p r i n t (X)
p r i n t ( ” \n Maximum v a l u e i n X : \n ” )
print ( all max )
p r i n t ( ” \n Column−w i s e max o f X : \n ” )
10 PYTHON PROGRAMMING
p r i n t ( column max )
p r i n t ( ” \n I n d i c e s o f column max : \n ” )
p r i n t ( column argmax )
p r i n t ( ” \n Row−w i s e max o f X : \n ” )
p r i n t ( row max )
# Sum o p e r a t i o n e x a m p l e s
# T h e s e work s i m i l a r l y t o t h e max o p e r a t i o n s −− u s e t h e a x i s a r g u m e n t t o d e n o t e i f summing o v e r rows o r c o l u m
t o t a l s u m = np . sum (X)
column sum = np . sum (X, a x i s = 0 )
row sum = np . sum (X, a x i s = 1 )
p r i n t ( ” M a t r i x X : \n ” )
p r i n t (X)
p r i n t ( ” \n Sum o v e r a l l e l e m e n t s o f X : \n ” )
print ( total sum )
p r i n t ( ” \n Column−w i s e sum o f X : \n ” )
p r i n t ( column sum )
p r i n t ( ” \n Row−w i s e sum o f X : \n ” )
p r i n t ( row sum )
# Matrix reshaping
X = np . a r a n g e ( 1 6 ) # makes a rank −1 a r r a y o f i n t e g e r s f r o m 0 t o 15
X s q u a r e = np . r e s h a p e (X, ( 4 , 4 ) ) # r e s h a p e X i n t o a 4 x 4 m a t r i x
X r a n k 3 = np . r e s h a p e (X, ( 2 , 2 , 4 ) ) # r e s h a p e X t o be 2 x 2 x 4 −−a rank −3 a r r a y
# c o n s i d e r a s two rank −2 a r r a y s w i t h 2 rows and 4 c o l u m n s
p r i n t ( ” Rank−1 a r r a y X : \n ” )
p r i n t (X)
p r i n t ( ” \n R e s h a p e d i n t o a s q u a r e m a t r i x : \n ” )
print ( X square )
p r i n t ( ” \n R e s h a p e d i n t o a r a n k −3 a r r a y w i t h d i m e n s i o n s 2 x 2 x 4 : \n ” )
print ( X rank 3 )
Matplotlib library is used for plotting, specifically within the Pyplot module. Aptly
named, the plot function is used to plot 2-D data.
Listing A.3: Exploring Titanic Dataset with pandas in Python.
import numpy a s np
import m a t p l o t l i b . p y p l o t a s p l t
# We ’ l l s t a r t w i t h a p a r a b o l a
# Compute t h e p a r a b o l a ’ s x and y coordinates
x = np . a r a n g e ( −5 , 5 , 0 . 1 )
y = np . s q u a r e ( x )
# Use m a t p l o t l i b f o r t h e p l o t
p l t . plot (x , y , ’b ’ ) # specify the color blue for the l i n e
p l t . x l a b e l ( ’X−A x i s V a l u e s ’ )
p l t . y l a b e l ( ’Y−A x i s V a l u e s ’ )
plt . t i t l e ( ’ F i r s t Plot : A Parabola ’)
p l t . show ( ) # r e q u i r e d t o a c t u a l l y display the plot
X = np . i d e n t i t y ( 1 0 )
EXPLORING DATASET WITH PANDAS IN PYTHON 11
# Now p l o t a random m a t r i x , w i t h a d i f f e r e n t c o l o r m a p
A = np . random . r a n d n ( 1 0 , 1 0 )
r a n d o m m a t r i x i m a g e = p l t . imshow (A)
p l t . show ( )
import p a n d a s a s pd
# Following code c o n v e r t s t h e uploaded f i l e i n t o a pandas dataframe .
# Make s u r e t h e f i l e n a m e m a t c h e s t h e name o f t h e u p l o a d e d f i l e .
import i o
t i t a n i c d f = pd . r e a d c s v ( i o . B y t e s I O ( u p l o a d e d [ ” T i t a n i c −t r a i n . c s v ” ] ) )
# D a t a s e t i s now s t o r e d i n a Pandas D a t a f r a m e
titanic df
# When d i s p l a y i n g a DataFrame , t h e f i r s t and l a s t 5 rows w i l l be shown by d e f a u l t .
t i t a n i c d f . head ( )
# D i s p l a y i n g t h e f i r s t 5 rows o f a p a n d a s DataFrame
# Want t o s e e t h e f i r s t 8 rows o f a p a n d a s DataFrame .
# t i t a n i c d f . head ( 8 )
titanic df . tail ()
# D i s p l a y i n g t h e l a s t 5 rows o f a p a n d a s DataFrame
# D i s p l a y i n g l i s t o f column names
t i t a n i c d f . columns
# D i s p l a y s h a p e o f DataFrame
t i t a n i c d f . shape
t i t a n i c d f . info ()
# The method i n f o ( ) p r o v i d e s t e c h n i c a l i n f o r m a t i o n a b o u t a DataFrame .
12 PYTHON PROGRAMMING
t i t a n i c d f . i s n u l l ( ) . sum ( )
t i t a n i c d f [ ”Name” ]
t y p e ( t i t a n i c d f [ ”Name” ] )
# Each column i n a DataFrame i s a S e r i e s .
# D i s p l a y column names w i t h ’ a ’
titanic df . filter ( like = ’a ’ )
# # S o r t i n g a f e a t u r e i n a DatFrame
t i t a n i c d f . f i l t e r ( l i k e = ’ F a r e ’ ) . s o r t v a l u e s ( by = ’ F a r e ’ , a s c e n d i n g = T r u e )
# Descending oder
# t i t a n i c d f . f i l t e r ( l i k e = ’ Fare ’ ) . s o r t v a l u e s ( by = ’ Fare ’ , a s c e n d i n g = F a l s e )
# S o r t i n g i n s t a n c e s i n a DatFrame
t i t a n i c d f . s o r t v a l u e s ( by = ’ F a r e ’ , a s c e n d i n g = T r u e )
t i t a n i c d f . loc [ : , : ]
t i t a n i c d f . l o c [ 1 0 : 2 0 , [ ” P c l a s s ” , ” C a b i n ” , ” Embarked ” ] ]
# D r o p i n g rows and c o l u m n s
t i t a n i c d f . drop ( [ 0 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] , a x i s = 0)
# a x i s = 0 , i n d i c a t e s t o d r o p rows
t i t a n i c d f . drop ( [ ” T i c k e t ” , ” Cabin ” ] , a x i s = 1)
# a x i s = 1 , i n d i c a t e s t o drop columns
titanic df
EXPLORATORY DATA ANALYSIS (EDA) IN PYTHON 13
# C o n v e r t i n g u p l o a d e d f i l e i n t o DataFrame
import p a n d a s a s pd
import i o
t i t a n i c d f = pd . r e a d c s v ( i o . B y t e s I O ( u p l o a d e d [ ” T i t a n i c −t r a i n . c s v ” ] ) )
display ( t i t a n i c t r a i n d f )
t i t a n i c d f [ ” Sex ” ] . v a l u e c o u n t s ( )
# Data V i s u l a i z a t i o n
# h t t p s : / / m a t p l o t l i b . org / s t a b l e / t u t o r i a l s / c o l o r s / colormaps . html
import m a t p l o t l i b . p y p l o t a s p l t
# Help
plt . pie ?
# C r e a t e a p i e c h a r t p r e s e n t i n g t h e male / f e m a l e p r o p o r t i o n .
plt . figure ( figsize = (6 ,8))
p l t . p i e ( t i t a n i c d f [ ” Sex ” ] . v a l u e c o u n t s ( ) ,
a u t o p c t =” %0.2 f%%” , l a b e l s = [ ” Male ” , ” Female ” ] ,
explode =(0 , 0 . 1 ) )
p l t . legend ( )
p l t . show ( )
p r i n t ( ” S u r v i v e d = ” , ( t i t a n i c d f [ ” S u r v i v e d ” ] = = 1 ) . sum ( ) )
p r i n t ( ” Not S u r v i v e d = ” , ( t i t a n i c d f [ ” S u r v i v e d ” ] = = 0 ) . sum ( ) )
t i t a n i c d f [ ” Embarked ” ] . v a l u e c o u n t s ( )
14 PYTHON PROGRAMMING
# C r e a t i n g a l i n e graph
plt . figure ( fig s ize =(20 ,9))
p l t . p l o t ( t i t a n i c d f . loc [100:200 ,[ ’ Fare ’ ] ] )
p l t . t i t l e ( ” Age L i n e P l o t ” , f o n t s i z e = 1 2 )
p l t . x l a b e l ( ” P a s s e n g e r s ( I n s t n a c e from 100 t o 2 0 0 ) ” )
p l t . y l a b e l ( ” Passenger Fare ” )
p l t . show ( )
sns . s e t s t y l e ( ” whitegrid ” )
plt . figure ( figsize = (12 ,10))
s n s . s c a t t e r p l o t ( x= ’ Age ’ , y= ’ F a r e ’ , d a t a = t i t a n i c d f , hue = ’ Sex ’ , p a l e t t e = ’ p l a s m a ’ )
# C r e a t e a h i s t o g r a m w i t h t h e Fare p a y e d
p l t . s t y l e . use ( ” ggplot ” )
plt . figure ( figsize = (10 ,8))
s n s . d i s t p l o t ( t i t a n i c d f [ ” F a r e ” ] , c o l o r =” b l u e ” )
# s n s . d i s t p l o t ( t i t a n i c t r a i n d f . Fare )
p l t . show ( )
# Creating boxplot
p l t . s t y l e . use ( ’ ggplot ’ )
plt . figure ( figsize = (11 ,8))
sns . boxplot ( data = t i t a n i c d f )
p l t . s t y l e . use ( ’ ggplot ’ )
plt . figure ( figsize = (11 ,8))
sns . boxplot ( data= t i t a n i c d f , o r i e n t = ’h ’ , p a l e t t e = ’ v i r i d i s ’ )
# Finding c o r r e l a t i o n
t i t a n i c d f . corr ()
# C r e a t i n g heatmap
plt . figure ( figsize = (13 ,9))
sn s . heatmap ( t i t a n i c d f . c o r r ( ) , annot = True )
# Gender P l o t
s n s . f a c t o r p l o t ( ’ Sex ’ , d a t a = t i t a n i c d f , k i n d = ’ c o u n t ’ , p a l e t t e = ’ v i r i d i s ’ )
# F i n d t h e g e n d e r r a t i o n among t h e c l a s s e s
s n s . f a c t o r p l o t ( ’ P c l a s s ’ , d a t a = t i t a n i c d f , hue = ’ Sex ’ , k i n d = ’ c o u n t ’ )
# C h e c k i n g o f t h e c l a s s had any e f f e c t i n t h e s u r v i v a l r a t e
KMEANS CLUSTERING WITH SCIKIT-LEARN 15
s n s . f a c t o r p l o t ( ’ S u r v i v e d ’ , d a t a = t i t a n i c d f , k i n d = ’ c o u n t ’ , hue = ’ P c l a s s ’ )
# Creating FacetGrid
a s f i g = s n s . F a c e t G r i d ( t i t a n i c d f , hue = ’ P c l a s s ’ , a s p e c t = 5 )
a s f i g . map ( s n s . k d e p l o t , ’ Age ’ , s h a d e = T r u e )
o l d e s t = t i t a n i c d f [ ’ Age ’ ] . max ( )
a s f i g . s e t ( xlim =(0 , o l d e s t ) )
a s f i g . add legend ()
# Creating f a c t o r p l o t
s n s . f a c t o r p l o t ( ’ P c l a s s ’ , ’ S u r v i v e d ’ , d a t a = t i t a n i c d f , hue = ’ Sex ’ )
# Creating lmplot
s n s . l m p l o t ( ’ Age ’ , ’ S u r v i v e d ’ , d a t a = t i t a n i c d f , hue = ’ Embarked ’ )
s n s . l m p l o t ( ’ Age ’ , ’ S u r v i v e d ’ , d a t a = t i t a n i c d f , hue = ’ P c l a s s ’ )
# S c a t t e r p l o t Matrix
s n s . s e t t h e m e ( s t y l e =” t i c k s ” )
# d f = sns . l o a d d a t a s e t (” penguins ”)
s n s . p a i r p l o t ( t i t a n i c d f , hue =” S u r v i v e d ” )
# O b s e r v a t i o n : Most o f p a s s e n g e r s t r a v e l l i n g i n 3 r d C l a s s
p l t . s t y l e . use ( ” ggplot ” )
s n s . c a t p l o t ( x = ’ P c l a s s ’ , k i n d = ” c o u n t ” , hue = ’ Sex ’ , d a t a = t i t a n i c d f , p a l e t t e = ’ c o o l ’ )
p l t . show ( )
# O b s e r v a t i o n : Most o f p a s s e n g e r s t r a v e l l i n g i n 3 r d C l a s s
p l t . s t y l e . use ( ” ggplot ” )
s n s . c a t p l o t ( x = ’ S u r v i v e d ’ , k i n d = ” c o u n t ” , hue = ’ P c l a s s ’ , d a t a = t i t a n i c d f , p a l e t t e = ’ w i n t e r ’ )
p l t . show ( )
sns . s e t s t y l e ( ” whitegrid ” )
plt . figure ( figsize = (12 ,9))
s n s . b o x p l o t ( x = ” P c l a s s ” , y = ” Age ” , d a t a = t i t a n i c d f )
# h t t p s : / / s c i k i t −l e a r n . o r g / s t a b l e / m o d u l e s / c l u s t e r i n g . h t m l
# h t t p s : / / s c i k i t −l e a r n . o r g / s t a b l e / m o d u l e s / g e n e r a t e d / s k l e a r n . c l u s t e r . KMeans . h t m l
# h t t p s : / / www . a n a l y t i c s v i d h y a . com / b l o g / 2 0 2 1 / 0 1 / i n −d e p t h −i n t u i t i o n −o f−k−means−c l u s t e r i n g −a l g o r i t h m −i n −machine−
from g o o g l e . c o l a b import f i l e s
uploaded = f i l e s . upload ( )
import p a n d a s a s pd
import i o
m a l l c u s t o m e r d f = pd . r e a d c s v ( i o . B y t e s I O ( u p l o a d e d [ ” M a l l C u s t o m e r s . c s v ” ] ) )
16 PYTHON PROGRAMMING
mall customer df
# EDA − E x p l o r a t o r y Data A n a l y s i s
m a l l c u s t o m e r d f [ ’ Genre ’ ] . v a l u e c o u n t s ( )
import s e a b o r n a s s n s
s n s . c o u n t p l o t ( d a t a = m a l l c u s t o m e r d f , x = ’ Genre ’ , p a l e t t e = ’ RdPu ’ )
# F r e q u e n c y o f e a c h Age
import m a t p l o t l i b . p y p l o t a s p l t
plt . figure ( figsize = (20 ,10))
s n s . c o u n t p l o t ( d a t a = m a l l c u s t o m e r d f , x = ’ Age ’ , p a l e t t e = ’ v i r i d i s ’ )
# F r e q u e n c y o f e a c h A n n u a l Income
plt . figure ( figsize = (20 ,10))
s n s . c o u n t p l o t ( d a t a = m a l l c u s t o m e r d f , x = ’ Annual Income ( k$ ) ’ , p a l e t t e = ’magma ’ )
# F r e q u e n c y o f e a c h A n n u a l Income
plt . figure ( figsize = (20 ,10))
s n s . c o u n t p l o t ( d a t a = m a l l c u s t o m e r d f , x = ’ S p e n d i n g S c o r e (1 −100) ’ , p a l e t t e = ’ v i r i d i s ’ )
kmeans . c l u s t e r c e n t e r s
y c l u s t e r s = kmeans . f i t p r e d i c t (X)
y clusters . astype
import numpy a s np
LINEAR REGRESSION IN PYTHON WITH SCIKIT-LEARN 17
x a r r a y = np . a r r a y (X)
# Visualization
plt . figure ( fig s ize =(15 ,8))
sns . s c a t t e r p l o t ( x = x array [ y clusters == 0, 0] , y = x array [ y clusters == 0, 1] , label = ’ Cluster1 ’ , s = 6
sns . s c a t t e r p l o t ( x = x array [ y clusters == 1, 0] , y = x array [ y clusters == 1, 1] , label = ’ Cluster2 ’ , s = 6
sns . s c a t t e r p l o t ( x = x array [ y clusters == 2, 0] , y = x array [ y clusters == 2, 1] , label = ’ Cluster3 ’ , s = 6
sns . s c a t t e r p l o t ( x = x array [ y clusters == 3, 0] , y = x array [ y clusters == 3, 1] , label = ’ Cluster4 ’ , s = 6
sns . s c a t t e r p l o t ( x = x array [ y clusters == 4, 0] , y = x array [ y clusters == 4, 1] , label = ’ Cluster5 ’ , s = 6
s n s . s c a t t e r p l o t ( kmeans . c l u s t e r c e n t e r s [ : , 0 ] , kmeans . c l u s t e r c e n t e r s [ : , 1 ] , m a r k e r = ’ x ’ , l a b e l = ’ C e n t r o i
p l t . show ( )
# Data M a n i p u l a t i o n
b o s t o n d f . i s n u l l ( ) . sum ( )
# Muti−C o l l i n e a r i t y i s C o r r e l a t i o n b / w f e a t u r e s .
# V a l u e o f c o r r e l a t i o n m u s t n o t be >= 0 . 8 5 o r −0.85
p l t . s c a t t e r ( b o s t o n d f [ ’LSTAT ’ ] , b o s t o n d f [ ’MEDV’ ] )
p l t . s c a t t e r ( b o s t o n d f [ ’RM’ ] , b o s t o n d f [ ’MEDV’ ] )
18 PYTHON PROGRAMMING
# Data P r e p r o c e s s i n g
# S c a l i n g − P r ep r o c e s s i n g to c o n v e r t d i f f e r e n t range of data i n t o ( 0 , 1 ) range
from s k l e a r n . p r e p r o c e s s i n g import MinMaxScaler
# s c a l i n g m u l t i p l e range of data i n t o ( 0 , 1 ) range
s c a l e r = MinMaxScaler ( f e a t u r e r a n g e = ( 0 , 1 ) )
X s c a l e d = s c a l e r . f i t t r a n s f o r m (X)
# C r o s s V a l i d a t i o n S p l i t t i n g d a t a i n t o t r a i n i n g and t e s t i n g
from s k l e a r n . m o d e l s e l e c t i o n import t r a i n t e s t s p l i t
X train , X test , y t r a i n , y t e s t = t r a i n t e s t s p l i t ( X scaled , y , t e s t s i z e = 0.3 , random state = 3)
# Linear Regression
from s k l e a r n . l i n e a r m o d e l import L i n e a r R e g r e s s i o n
l i n r e g = L i n e a r R e g r e s s i o n ( n o r m a l i z e = True )
# f i t ( ) means t r a i n i n g o f LR model
l i n r e g . f i t ( X train , y t r a i n )
# Accuracy of T e s t i n g
l i n r e g . score ( X test , y t e s t )
# r 2 S c o r e , mean s q u a r e d e r r o r , mean a b s o l u t e e r r o r
from s k l e a r n . m e t r i c s import m e a n a b s o l u t e e r r o r , m e a n s q u a r e d e r r o r , r 2 s c o r e
p r i n t ( ” Mean A b s o l u t e E r r o r : ” , m e a n a b s o l u t e e r r o r ( y t e s t , y h a t ) )
p r i n t ( ” Mean S q u a r e d E r r o r : ” , m e a n s q u a r e d e r r o r ( y t e s t , y h a t ) )
print ( ” r2 Score : ” , r 2 s c o r e ( y t e s t , y hat ) )
import numpy a s np
p r i n t ( ” Root Mean S q u a r e d E r r o r : ” , np . s q r t ( m e a n s q u a r e d e r r o r ( y t e s t , y h a t ) ) )
# P r e d i c t i n g new v a l u e
X = [[0.00632 , 18.0 , 7.31 , 0.438 , 6.575 , 75.2 , 4.0900 , 286.0 , 21.3 , 396.90 , 4.98]]
X s c a l e d n e w = s c a l e r . t r a n s f o r m (X)
l i n r e g . p r e d i c t ( X scaled new )
from g o o g l e . c o l a b import f i l e s
uploaded = f i l e s . upload ( )
t i t a n i c d f = pd . r e a d c s v ( i o . B y t e s I O ( u p l o a d e d [ ” T i t a n i c −t r a i n . c s v ” ] ) )
display ( titanic df )
sns . s e t s t y l e ( ” whitegrid ” )
plt . figure ( figsize = (12 ,9))
s n s . b o x p l o t ( x = ” P c l a s s ” , y = ” Age ” , d a t a = t i t a n i c d f )
t i t a n i c d f [ t i t a n i c d f [ ’ P c l a s s ’ ] == 1 ] [ ’ Age ’ ] . median ( )
t i t a n i c d f [ t i t a n i c d f [ ’ P c l a s s ’ ] == 2 ] [ ’ Age ’ ] . median ( )
t i t a n i c d f [ t i t a n i c d f [ ’ P c l a s s ’ ] == 3 ] [ ’ Age ’ ] . median ( )
# Simple Imputer
def impute age ( c o l s ) :
Age = c o l s [ 0 ]
Pclass = cols [1]
i f pd . i s n u l l ( Age ) :
i f P c l a s s == 1 :
r e t u r n 37
e l i f P c l a s s == 2 :
r e t u r n 29
else :
r e t u r n 24
else :
r e t u r n Age
t i t a n i c d f [ ’ Age ’ ] = t i t a n i c d f [ [ ’ Age ’ , ’ P c l a s s ’ ] ] . a p p l y ( i m p u t e a g e , a x i s = 1 )
t i t a n i c d f . i s n u l l ( ) . sum ( )
t i t a n i c d f . i s n u l l ( ) . sum ( )
t i t a n i c d f . info ()
# s p l i t X and y i n t o t r a i n i n g and t e s t i n g s e t s
from s k l e a r n . m o d e l s e l e c t i o n import t r a i n t e s t s p l i t
X t r a i n , X t e s t , y t r a i n , y t e s t = t r a i n t e s t s p l i t (X, y , t e s t s i z e = 0 . 3 0 , r a n d o m s t a t e = 0 )
# i n s t a n t i a t e t h e model ( u s i n g t h e d e f a u l t p a r a m e t e r s )
logreg = LogisticRegression ()
# f i t t h e model w i t h d a t a
logreg . f i t ( X train , y t r a i n )
# Accuracy of T e s t i n g
logreg . score ( X test , y t e s t )
# Confusion Matrix
from s k l e a r n . m e t r i c s import c o n f u s i o n m a t r i x , c l a s s i f i c a t i o n r e p o r t
predictions = logreg . predict ( X test )
confusion matrix ( y test , predictions )
s n s . h e a t m a p ( c o n f u s i o n m a t r i x ( y t e s t , p r e d i c t i o n s ) , a n n o t = True , f m t = ’ 0 . 0 f ’ )
l o g r e g . p r e d i c t ( X Jack )
l o g r e g . p r e d i c t ( X Rose )
# Model E v a l u a t i o n u s i n g C o n f u s i o n M a t r i x
y pred = logreg . p re d i c t ( X test )
# import the metrics class
from s k l e a r n import m e t r i c s
cnf matrix = metrics . confusion matrix ( y test , y pred )
cnf matrix
# V i s u a l i z i n g C o n f u s i o n M a t r i x u s i n g Heatmap
c l a s s n a m e s = [ 0 , 1 ] # name o f c l a s s e s
f i g , ax = p l t . s u b p l o t s ( )
t i c k m a r k s = np . a r a n g e ( l e n ( c l a s s n a m e s ) )
p l t . xticks ( tick marks , class names )
p l t . yticks ( tick marks , class names )
# c r e a t e heatmap
s n s . h e a t m a p ( pd . DataFrame ( c n f m a t r i x ) , a n n o t = True , cmap=” YlGnBu ” , f m t = ’ g ’ )
ax . x a x i s . s e t l a b e l p o s i t i o n ( ” t o p ” )
plt . tight layout ()
p l t . t i t l e ( ’ Confusion matrix ’ , y =1.1)
p l t . ylabel ( ’ Actual l a b e l ’ )
plt . xlabel ( ’ Predicted label ’ )
# ROC Curve
y pred proba = logreg . predict proba ( X test ) [ : : , 1 ]
fpr , tpr , = metrics . roc curve ( y test , y pred proba )
auc = m e t r i c s . r o c a u c s c o r e ( y t e s t , y p r e d p r o b a )
DECISION TREE AND RANDOM FOREST IN PYTHON 21
p l t . p l o t ( f p r , t p r , l a b e l =” d a t a 1 , a u c =”+ s t r ( a u c ) )
p l t . l e g e n d ( l o c =4)
p l t . show ( )
from g o o g l e . c o l a b import f i l e s
uploaded = f i l e s . upload ( )
i r i s d f = pd . r e a d c s v ( i o . B y t e s I O ( u p l o a d e d [ ” I r i s . c s v ” ] ) )
display ( i r i s d f )
i r i s d f . i s n u l l ( ) . sum ( )
X = i r i s d f . drop ( [ ” Id ” , ” Species ” ] , a x i s = 1)
y = i r i s d f [ ” Species ” ]
from s k l e a r n . m o d e l s e l e c t i o n import t r a i n t e s t s p l i t
X t r a i n , X t e s t , y t r a i n , y t e s t = t r a i n t e s t s p l i t (X, y , t e s t s i z e = 0 . 3 , r a n d o m s t a t e = 3 )
from s k l e a r n . t r e e import D e c i s i o n T r e e C l a s s i f i e r
c l f t r e e = DecisionTreeClassifier ( c r i t e r i o n = ’ entropy ’ )
c l f t r e e . f i t ( X train , y t r a i n )
# A c c u r a c y on T e s t d a t a
c l f t r e e . score ( X test , y t e s t )
# A c c u r a c y on T r a i n i n g d a t a
c l f t r e e . score ( X train , y t r a i n )
# Confusion Matrix
from s k l e a r n . m e t r i c s import c o n f u s i o n m a t r i x , c l a s s i f i c a t i o n r e p o r t
y hat = c l f t r e e . predict ( X test )
s n s . h e a t m a p ( c o n f u s i o n m a t r i x ( y t e s t , y h a t ) , a n n o t = True , f m t = ’ 0 . 0 f ’ )
# P l o t t i n g Decision Tree
from s k l e a r n . e x t e r n a l s . s i x import S t r i n g I O
from I P y t h o n . d i s p l a y import Image
from s k l e a r n . t r e e import e x p o r t g r a p h v i z
22 PYTHON PROGRAMMING
import p y d o t p l u s
Image ( g r a p h . c r e a t e p n g ( ) )
# Random F o r e s t
from s k l e a r n . e n s e m b l e import R a n d o m F o r e s t C l a s s i f i e r
c l f r f = R a n d o m F o r e s t C l a s s i f i e r ( b o o t s t r a p = True , m a x d e p t h = 1 0 , n e s t i m a t o r s = 1 5 0 , r a n d o m s t a t e = 0 , c r i t e
c l f r f . f i t ( X train , y t r a i n )
c l f r f . score ( X test , y t e s t )
# Confusion Matrix
from s k l e a r n . m e t r i c s import c o n f u s i o n m a t r i x , c l a s s i f i c a t i o n r e p o r t
predictions = c l f r f . predict ( X test )
s n s . h e a t m a p ( c o n f u s i o n m a t r i x ( y t e s t , p r e d i c t i o n s ) , a n n o t = True , f m t = ’ 0 . 0 f ’ )
from g o o g l e . c o l a b import f i l e s
uploaded = f i l e s . upload ( )
import p a n d a s a s pd
import i o
w e a t h e r d f = pd . r e a d c s v ( i o . B y t e s I O ( u p l o a d e d [ ” w e a t h e r . n u m e r i c . c s v ” ] ) )
display ( weather df )
weather df . shape
weather df . info ()
X = w e a t h e r d f . drop ( [ ” Play ” ] , a x i s = 1)
y = weather df [ ” Play ” ]
from s k l e a r n . m o d e l s e l e c t i o n import t r a i n t e s t s p l i t
X t r a i n , X t e s t , y t r a i n , y t e s t = t r a i n t e s t s p l i t (X, y , t e s t s i z e = 0 . 3 , r a n d o m s t a t e = 4 2 )
# B u i l d i n g Decision Tree
from s k l e a r n . t r e e import D e c i s i o n T r e e C l a s s i f i e r
HANDLING MISSING DATA WITH PYTHON 23
DT = D e c i s i o n T r e e C l a s s i f i e r ( c r i t e r i o n = ’ e n t r o p y ’ )
DT . f i t ( X t r a i n , y t r a i n )
# Confusion Matrix
from s k l e a r n . m e t r i c s import c o n f u s i o n m a t r i x , c l a s s i f i c a t i o n r e p o r t
y h a t = DT . p r e d i c t ( X t e s t )
import s e a b o r n a s s n s
s n s . h e a t m a p ( c o n f u s i o n m a t r i x ( y t e s t , y h a t ) , a n n o t = True , f m t = ’ 0 . 0 f ’ )
# P l o t t i n g Decision Tree
from s k l e a r n . e x t e r n a l s . s i x import S t r i n g I O
from I P y t h o n . d i s p l a y import Image
from s k l e a r n . t r e e import e x p o r t g r a p h v i z
import p y d o t p l u s
dot data = StringIO ()
e x p o r t g r a p h v i z ( DT , o u t f i l e = d o t d a t a , f e a t u r e n a m e s = l i s t (X . c o l u m n s ) , f i l l e d = True , r o u n d e d = T r u e )
graph = pydotplus . g r a p h f r o m d o t d a t a ( d o t d a t a . g e t v a l u e ( ) )
Image ( g r a p h . c r e a t e p n g ( ) )
# h t t p s : / / m l j a r . com / b l o g / v i s u a l i z e −d e c i s i o n −t r e e /
from g o o g l e . c o l a b import f i l e s
uploaded = f i l e s . upload ( )
d i a b e t e s d f = pd . r e a d c s v ( i o . B y t e s I O ( u p l o a d e d [ ” d i a b e t e s . c s v ” ] ) )
display ( diabetes df )
d a t a = d i a b e t e s d f . d r o p ( [ ’ Outcome ’ ] , a x i s = 1 )
24 PYTHON PROGRAMMING
# R e p l a c i n g 0 w i t h nan v a l u e s
d a t a . r e p l a c e ( 0 , np . nan , i n p l a c e = T r u e )
d a t a . i s n u l l ( ) . sum ( ) . s o r t v a l u e s ( a s c e n d i n g = F a l s e )
np . round ( d a t a [ ’ I n s u l i n ’ ] . mean ( ) )
from s k l e a r n . i m p u t e import S i m p l e I m p u t e r
# s t r a t e g y = ’ median ’ , ’ mean ’
i m p u t e = S i m p l e I m p u t e r ( s t r a t e g y = ’ median ’ )
d a t a a r r a y = impute . f i t t r a n s f o r m ( data )
data array
d i a b e t e s d f 1 = pd . DataFrame ( d a t a a r r a y , c o l u m n s = d a t a . c o l u m n s )
diabetes df1
from g o o g l e . c o l a b import f i l e s
uploaded = f i l e s . upload ( )
d i a b e t e s d f = pd . r e a d c s v ( i o . B y t e s I O ( u p l o a d e d [ ” d i a b e t e s . c s v ” ] ) )
display ( diabetes df )
d i a b e t e s d f . i s n u l l ( ) . sum ( ) . s o r t v a l u e s ( a s c e n d i n g = F a l s e )
s n s . c a t p l o t ( x = ’ Outcome ’ , k i n d = ’ c o u n t ’ , d a t a = d i a b e t e s d f , p a l e t t e = ’ v i r i d i s ’ )
d i a b e t e s d f [ ’ Outcome ’ ] . v a l u e c o u n t s ( )
from s k l e a r n . u t i l s import r e s a m p l e
d f 0 = d i a b e t e s d f [ d i a b e t e s d f [ ’ Outcome ’ ] == 0 ]
d f 1 = d i a b e t e s d f [ d i a b e t e s d f [ ’ Outcome ’ ] == 1 ]
# Apply sampling
d f 1 o v e r s a m p l i n g = r e s a m p l e ( d f 1 , n s a m p l e s = 5 0 0 , r e p l a c e = True , r a n d o m s t a t e = 1 2 3 )
d i a b e t e s d f b a l a n c e d = pd . c o n c a t ( [ d f 0 , d f 1 o v e r s a m p l i n g ] )
s n s . c a t p l o t ( x = ’ Outcome ’ , k i n d = ’ c o u n t ’ , d a t a = d i a b e t e s d f b a l a n c e d , p a l e t t e = ’ v i r i d i s ’ )
ARTIFICIAL NEURAL NETWORK CLASSIFICATION WITH PYTHON 25
X = d i a b e t e s d f b a l a n c e d . d r o p ( [ ’ Outcome ’ ] , a x i s = 1 )
y = d i a b e t e s d f b a l a n c e d [ ’ Outcome ’ ]
from s k l e a r n . m o d e l s e l e c t i o n import t r a i n t e s t s p l i t
X t r a i n , X t e s t , y t r a i n , y t e s t = t r a i n t e s t s p l i t (X, y , t e s t s i z e = 0 . 3 , r a n d o m s t a t e = 2 )
# I n p u t Layer
model . add ( Dense ( u n i t s = 6 4 , a c t i v a t i o n = ’ r e l u ’ , i n p u t s h a p e = [ l e n (X . k e y s ( ) ) ] ) )
model . add ( D r o p o u t ( 0 . 2 ) )
# Hidden L a y e r − I
model . add ( Dense ( u n i t s = 1 2 8 , a c t i v a t i o n = ’ r e l u ’ ) )
model . add ( D r o p o u t ( 0 . 2 ) )
# Hidden L a y e r − I I
model . add ( Dense ( u n i t s = 1 2 8 , a c t i v a t i o n = ’ r e l u ’ ) )
model . add ( D r o p o u t ( 0 . 2 ) )
# Output Layer
model . add ( Dense ( u n i t s = 1 , a c t i v a t i o n = ’ s i g m o i d ’ ) )
# Optimizers
# Learning r a t e ’ alpha ’ = (0.00001 − 0 . 1 )
o p t i m i z e r s = Adam ( l e a r n i n g r a t e = 0 . 0 0 1 )
r e t u r n model
model = b u i l d m o d e l ( )
model . summary ( )
from k e r a s . u t i l s . v i s u t i l s import p l o t m o d e l
p l o t m o d e l ( model , t o f i l e = ’ model . png ’ , s h o w s h a p e s = True , s h o w l a y e r n a m e s = T r u e )
h i s t o r y = model . f i t ( X t r a i n , y t r a i n , e p o c h s = 5 0 0 , b a t c h s i z e = 2 5 , v a l i d a t i o n s p l i t = 0 . 2 )
pd . DataFrame ( h i s t o r y . h i s t o r y ) [ [ ’ a c c u r a c y ’ , ’ v a l a c c u r a c y ’ ] ] . p l o t ( f i g s i z e = ( 8 , 6 ) )
model . e v a l u a t e ( X t e s t , y t e s t )
p r e d i c t = model . p r e d i c t ( X t e s t )
26 PYTHON PROGRAMMING
y h a t = np . round ( p r e d i c t )
from s k l e a r n . m e t r i c s import c o n f u s i o n m a t r i x
confusion matrix ( y test , yhat )
# Deep L e a r n i n g P a c k a g e s
from k e r a s . m o d e l s import S e q u e n t i a l
from k e r a s . l a y e r s import Conv2D , MaxPooling2D , F l a t t e n , Dense
from k e r a s . d a t a s e t s . m n i s t import l o a d d a t a
( train data , train labels ) , ( test data , test labels ) = load data ()
# t r a i n data w ith i n d e x p o s i t i o n as 0
train data [0]
# 600 i s i n d e x p o s i t i o n 0 − 59999
img = t r a i n d a t a [ 6 0 0 ]
p l t . imshow ( img , cmap = ’ g r a y ’ )
p l t . t i t l e ( ” L a b e l : %i ” %t r a i n l a b e l s [ 6 0 0 ] )
p l t . show ( )
# Image Shape T r a n s f o r m a t i o n
i m a g e h e i g h t = 28
i m a g e w i d t h = 28
# Gray S c a l e Image w i t h num o f C h a n n e l s = 1 ( Rank ) , 3 ( Rank ) RGB
num channels = 1
t r a i n d i g i t s . shape
t e s t d i g i t s . shape
CONVOLUTIONAL NEURAL NETWORK WITH PYTHON 27
t r a i n d i g i t s [0]
# Rescalling / Normalization
# s c a l i n g o f image p i x e l
t r a i n d i g i t s = t r a i n d i g i t s . a s t y p e ( ’ f l o a t 3 2 ’ ) / 255
t e s t d i g i t s = t e s t d i g i t s . a s t y p e ( ’ f l o a t 3 2 ’ ) / 255
t r a i n l a b e l s [ 0 : 10]
from k e r a s . u t i l s import t o c a t e g o r i c a l
n u m c l a s s e s = 10
t r a i n l a b e l s c l a s s = t o c a t e g o r i c a l ( t r a i n l a b e l s , num classes )
# CNN Model
def build model ( ) :
model = S e q u e n t i a l ( )
# C o n v o l u t i o n a l Layer − I
# p a d d i n g = ’ same ’ u s e d f o r z e r o p a d d i n g
model . add ( Conv2D ( f i l t e r s = 3 2 , k e r n e l s i z e = ( 3 , 3 ) , s t r i d e s = ( 1 , 1 ) , p a d d i n g = ’ same ’ , a c t i v a t i o n = ’ r e l u ’ ,
i n p u t s h a p e = ( image height , image width , num channels ) ) )
# MaxPooling
model . add ( MaxPooling2D ( p o o l s i z e = ( 2 , 2 ) ) )
# F latt e n Matrix
model . add ( F l a t t e n ( ) )
# F u l l y Connected Layer
model . add ( Dense ( u n i t s = 1 2 8 , a c t i v a t i o n = ’ r e l u ’ ) )
# Output Layer
model . add ( Dense ( u n i t s = 1 0 , a c t i v a t i o n = ’ s o f t m a x ’ ) )
o p t i m i z e r s = Adam ( l e a r n i n g r a t e = 0 . 0 0 0 1 )
model . c o m p i l e ( o p t i m i z e r = o p t i m i z e r s , l o s s = ’ c a t e g o r i c a l c r o s s e n t r o p y ’ , m e t r i c s = [ ’ a c c u r a c y ’ ] )
r e t u r n model
model = b u i l d m o d e l ( )
model . summary ( )
28 PYTHON PROGRAMMING
from k e r a s import c a l l b a c k s
f i l e p a t h = ” / c o n t e n t / d r i v e / MyDrive / B e s t M o d e l D i g i t . h d f 5 ”
c h e c k p o i n t = c a l l b a c k s . M o d e l C h e c k p o i n t ( f i l e p a t h , m o n i t o r = ’ v a l l o s s ’ , s a v e b e s t o n l y = True , mode = ’ min ’ , v
import d a t e t i m e
import k e r a s
import o s
%l o a d e x t t e n s o r b o a r d
%t e n s o r b o a r d −−l o g d i r ” / c o n t e n t / d r i v e / MyDrive / l o g s c n n ”
# C l a s s i f i c a t i o n Report
model . l o a d w e i g h t s ( ” / c o n t e n t / d r i v e / MyDrive / B e s t M o d e l D i g i t . h d f 5 ” )
p r e d i c t i o n s = model . p r e d i c t ( t e s t d i g i t s )
y h a t = np . argmax ( p r e d i c t i o n s , a x i s = 1 )
from s k l e a r n . m e t r i c s import c o n f u s i o n m a t r i x
confusion matrix ( t e s t l a b e l s , yhat )
t e s t l a b e l s c l a s s = t o c a t e g o r i c a l ( t e s t l a b e l s , num classes )
model . e v a l u a t e ( t e s t d i g i t s , t e s t l a b e l s c l a s s )
from s k l e a r n . m e t r i c s import c l a s s i f i c a t i o n r e p o r t
print ( c l a s s i f i c a t i o n r e p o r t ( t e s t l a b e l s , yhat ) )