Table Of ContentAdvanced Data
Analytics Using Python
With Architectural Patterns, Text
and Image Classification, and
Optimization Techniques
—
Second Edition
—
Sayan Mukhopadhyay
Pratip Samanta
Advanced Data
Analytics Using
Python
With Architectural Patterns,
Text and Image Classification,
and Optimization Techniques
Second Edition
Sayan Mukhopadhyay
Pratip Samanta
Advanced Data Analytics Using Python: With Architectural Patterns, Text
and Image Classification, and Optimization Techniques
Sayan Mukhopadhyay Pratip Samanta
Kolkata, West Bengal, India Kolkota, West Bengal, India
ISBN-13 (pbk): 978-1-4842-8004-1 ISBN-13 (electronic): 978-1-4842-8005-8
https://doi.org/10.1007/978-1-4842-8005-8
Copyright © 2023 by Sayan Mukhopadhyay, Pratip Samanta
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin Suresh John
Development Editor: James Markham
Coordinating Editor: Mark Powers
Copyeditor: Kim Wimpsett
Cover designed by eStudioCalamar
Cover image by David Clode on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY
10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com,
or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance
Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for
reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub (github.com/apress). For more detailed information, please visit
www.apress.com/source- code.
Printed on acid-free paper
The reason for the success of this book is that it has original
research, so I dedicate it to the person from whom I learned
how to do research: Dr. Debnath Pal, IISc.
—Sayan Mukhopadhyay
Table of Contents
About the Authors ��������������������������������������������������������������������������������xi
About the Technical Reviewer �����������������������������������������������������������xiii
Acknowledgments ������������������������������������������������������������������������������xv
Introduction ��������������������������������������������������������������������������������������xvii
Chapter 1: A Birds Eye View to AI System ��������������������������������������������1
OOP in Python �������������������������������������������������������������������������������������������������������1
Calling Other Languages in Python ���������������������������������������������������������������������13
Exposing the Python Model as a Microservice ���������������������������������������������������14
High-Performance API and Concurrent Programming ����������������������������������������17
Choosing the Right Database ������������������������������������������������������������������������������21
Summary�������������������������������������������������������������������������������������������������������������22
Chapter 2: ETL with Python ����������������������������������������������������������������23
MySQL �����������������������������������������������������������������������������������������������������������������24
How to Install MySQLdb? �������������������������������������������������������������������������������24
Database Connection �������������������������������������������������������������������������������������25
INSERT Operation ������������������������������������������������������������������������������������������26
READ Operation ���������������������������������������������������������������������������������������������26
DELETE Operation ������������������������������������������������������������������������������������������27
UPDATE Operation �����������������������������������������������������������������������������������������28
COMMIT Operation �����������������������������������������������������������������������������������������28
ROLL-BACK Operation������������������������������������������������������������������������������������28
v
Table of ConTenTs
Normal Forms �����������������������������������������������������������������������������������������������������31
First Normal Form �����������������������������������������������������������������������������������������31
Second Normal Form �������������������������������������������������������������������������������������32
Third Normal Form ����������������������������������������������������������������������������������������33
Elasticsearch �������������������������������������������������������������������������������������������������������35
Connection Layer API �������������������������������������������������������������������������������������38
Neo4j Python Driver ��������������������������������������������������������������������������������������������39
neo4j-rest-client �������������������������������������������������������������������������������������������������39
In-Memory Database ������������������������������������������������������������������������������������������40
MongoDB (Python Edition) ����������������������������������������������������������������������������������40
Import Data into the Collection ����������������������������������������������������������������������41
Create a Connection Using pymongo �������������������������������������������������������������42
Access Database Objects ������������������������������������������������������������������������������42
Insert Data �����������������������������������������������������������������������������������������������������43
Update Data ���������������������������������������������������������������������������������������������������43
Remove Data �������������������������������������������������������������������������������������������������43
Cloud Databases �������������������������������������������������������������������������������������������������43
Pandas ����������������������������������������������������������������������������������������������������������������44
ETL with Python (Unstructured Data) ������������������������������������������������������������������45
Email Parsing �������������������������������������������������������������������������������������������������45
Topical Crawling ��������������������������������������������������������������������������������������������48
Summary�������������������������������������������������������������������������������������������������������������52
Chapter 3: Feature Engineering and Supervised Learning �����������������53
Dimensionality Reduction with Python ���������������������������������������������������������������54
Correlation Analysis ���������������������������������������������������������������������������������������55
Principal Component Analysis �����������������������������������������������������������������������57
Mutual Information ����������������������������������������������������������������������������������������60
Classifications with Python ���������������������������������������������������������������������������������64
vi
Table of ConTenTs
Semi-Supervised Learning ���������������������������������������������������������������������������������65
Decision Tree �������������������������������������������������������������������������������������������������������66
Which Attribute Comes First? ������������������������������������������������������������������������66
Random Forest Classifier ������������������������������������������������������������������������������68
Naïve Bayes Classifier �����������������������������������������������������������������������������������������68
Support Vector Machine ��������������������������������������������������������������������������������������69
Nearest Neighbor Classifier ��������������������������������������������������������������������������������71
Sentiment Analysis ���������������������������������������������������������������������������������������������71
Image Recognition ����������������������������������������������������������������������������������������������73
Regression with Python ���������������������������������������������������������������������������������74
Least Square Estimation ��������������������������������������������������������������������������������75
Logistic Regression ���������������������������������������������������������������������������������������76
Classification and Regression �����������������������������������������������������������������������������76
Intentionally Bias the Model to Over-Fit or Under-Fit ������������������������������������������77
Dealing with Categorical Data �����������������������������������������������������������������������������78
Summary�������������������������������������������������������������������������������������������������������������79
Chapter 4: Unsupervised Learning: Clustering �����������������������������������81
K-Means Clustering ��������������������������������������������������������������������������������������������82
Choosing K: The Elbow Method ���������������������������������������������������������������������������86
Silhouette Analysis ����������������������������������������������������������������������������������������������86
Distance or Similarity Measure ���������������������������������������������������������������������������88
Properties ������������������������������������������������������������������������������������������������������89
General and Euclidean Distance ��������������������������������������������������������������������89
Squared Euclidean Distance ��������������������������������������������������������������������������91
Distance Between String-Edit Distance���������������������������������������������������������91
Similarity in the Context of a Document �������������������������������������������������������������93
Types of Similarity �����������������������������������������������������������������������������������������94
vii
Table of ConTenTs
Example of K-Means in Images ��������������������������������������������������������������������������95
Preparing the Cluster �������������������������������������������������������������������������������������97
Thresholding �������������������������������������������������������������������������������������������������������99
Time to Cluster ��������������������������������������������������������������������������������������������100
Revealing the Current Cluster ����������������������������������������������������������������������102
Hierarchical Clustering ��������������������������������������������������������������������������������������104
Bottom-Up Approach �����������������������������������������������������������������������������������104
Distance Between Clusters �������������������������������������������������������������������������105
Top-Down Approach ������������������������������������������������������������������������������������107
Graph Theoretical Approach ������������������������������������������������������������������������111
How Do You Know If the Clustering Result Is Good?������������������������������������112
Summary�����������������������������������������������������������������������������������������������������������113
Chapter 5: Deep Learning and Neural Networks �������������������������������115
Backpropagation �����������������������������������������������������������������������������������������������116
Backpropagation Approach �������������������������������������������������������������������������116
Other Algorithms �����������������������������������������������������������������������������������������������119
TensorFlow ��������������������������������������������������������������������������������������������������������120
Network Architecture and Regularization Techniques ���������������������������������124
Updatable Model and Transfer Learning ������������������������������������������������������124
Recurrent Neural Network ��������������������������������������������������������������������������������133
LSTM �����������������������������������������������������������������������������������������������������������134
Reinforcement Learning������������������������������������������������������������������������������������138
TD0 ��������������������������������������������������������������������������������������������������������������139
TDλ ��������������������������������������������������������������������������������������������������������������145
Example of Dialectic Learning ���������������������������������������������������������������������146
Convolution Neural Networks ���������������������������������������������������������������������������156
Summary�����������������������������������������������������������������������������������������������������������159
viii
Table of ConTenTs
Chapter 6: Time Series ���������������������������������������������������������������������161
Classification of Variation ����������������������������������������������������������������������������������161
Analyzing a Series Containing a Trend ��������������������������������������������������������������161
Curve Fitting ������������������������������������������������������������������������������������������������163
Removing Trends from a Time Series ����������������������������������������������������������163
Analyzing a Series Containing Seasonality �������������������������������������������������������164
Removing Seasonality from a Time Series ��������������������������������������������������������165
By Filtering ��������������������������������������������������������������������������������������������������165
By Differencing ��������������������������������������������������������������������������������������������166
Transformation ��������������������������������������������������������������������������������������������������166
To Stabilize the Variance �����������������������������������������������������������������������������167
To Make the Seasonal Effect Additive ���������������������������������������������������������167
To Make the Data Distribution Normal ���������������������������������������������������������167
Stationary Time Series ��������������������������������������������������������������������������������������168
Stationary Process ��������������������������������������������������������������������������������������168
Autocorrelation and the Correlogram ����������������������������������������������������������169
Estimating Autocovariance and Autocorrelation Functions �������������������������170
Time-Series Analysis with Python ���������������������������������������������������������������������171
Useful Methods ��������������������������������������������������������������������������������������������171
Autoregressive Processes ���������������������������������������������������������������������������173
Estimating Parameters of an AR Process ����������������������������������������������������175
Mixed ARMA Models �����������������������������������������������������������������������������������������177
Integrated ARMA Models �����������������������������������������������������������������������������������179
The Fourier Transform ���������������������������������������������������������������������������������������181
An Exceptional Scenario �����������������������������������������������������������������������������������182
Missing Data �����������������������������������������������������������������������������������������������������183
Summary�����������������������������������������������������������������������������������������������������������184
ix
Table of ConTenTs
Chapter 7: Analytics at Scale �����������������������������������������������������������185
Hadoop ��������������������������������������������������������������������������������������������������������������185
MapReduce Programming ���������������������������������������������������������������������������185
Partitioning Function �����������������������������������������������������������������������������������186
Combiner Function ��������������������������������������������������������������������������������������187
HDFS File System ����������������������������������������������������������������������������������������197
MapReduce Design Pattern �������������������������������������������������������������������������197
A Notes on Functional Programming ����������������������������������������������������������������203
Spark �����������������������������������������������������������������������������������������������������������������204
PySpark �������������������������������������������������������������������������������������������������������������207
Updatable Machine Learning and Spark Memory Model ����������������������������������209
Analytics in the Cloud ���������������������������������������������������������������������������������������221
Internet of Things ����������������������������������������������������������������������������������������������231
Essential Architectural Patterns for Data Scientists ������������������������������������231
Scenario 1: Hot Potato Anti-Pattern �������������������������������������������������������������232
Scenario 2: Proxy and Layering Patterns �����������������������������������������������������236
Thank You ����������������������������������������������������������������������������������������������������������241
Index �������������������������������������������������������������������������������������������������243
x