Table Of ContentPractical Machine
Learning with
Python
A Problem-Solver’s Guide to Building
Real-World Intelligent Systems
—
Dipanjan Sarkar
Raghav Bali
Tushar Sharma
Practical Machine
Learning with Python
A Problem-Solver’s Guide to Building
Real-World Intelligent Systems
Dipanjan Sarkar
Raghav Bali
Tushar Sharma
Practical Machine Learning with Python
Dipanjan Sarkar Raghav Bali
Bangalore, Karnataka, India Bangalore, Karnataka, India
Tushar Sharma
Bangalore, Karnataka, India
ISBN-13 (pbk): 978-1-4842-3206-4 ISBN-13 (electronic): 978-1-4842-3207-1
https://doi.org/10.1007/978-1-4842-3207-1
Library of Congress Control Number: 2017963290
Copyright © 2018 by Dipanjan Sarkar, Raghav Bali and Tushar Sharma
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.
Cover image by Freepik (www.freepik.com)
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Celestin Suresh John
Development Editor: Matthew Moodie
Technical Reviewer: Jojo Moolayil
Coordinating Editor: Sanchita Mandal
Copy Editor: Kezia Endsley
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
Tis book is dedicated to my parents, partner, friends, family, and well-wishers.
—Dipanjan Sarkar
To all my inspirations, who would never read this!
—Raghav Bali
Dedicated to my family and friends.
—Tushar Sharma
Contents
About the Authors ��������������������������������������������������������������������������������������������������xvii
About the Technical Reviewer ��������������������������������������������������������������������������������xix
Acknowledgments ��������������������������������������������������������������������������������������������������xxi
Foreword ��������������������������������������������������������������������������������������������������������������xxiii
Introduction �����������������������������������������������������������������������������������������������������������xxv
■ Part I: Understanding Machine Learning �������������������������������������������� 1
■ Chapter 1: Machine Learning Basics ��������������������������������������������������������������������� 3
The Need for Machine Learning ��������������������������������������������������������������������������������������� 4
Making Data-Driven Decisions ��������������������������������������������������������������������������������������������������������������� 4
Effciency and Scale ������������������������������������������������������������������������������������������������������������������������������� 5
T raditional Programming Paradigm �������������������������������������������������������������������������������������������������������� 5
Why Machine Learning? ������������������������������������������������������������������������������������������������������������������������� 6
Understanding Machine Learning ������������������������������������������������������������������������������������ 8
Why Make Machines Learn?������������������������������������������������������������������������������������������������������������������� 8
Formal Defnition ������������������������������������������������������������������������������������������������������������������������������������ 9
A Multi-Disciplinary Field ��������������������������������������������������������������������������������������������������������������������� 13
Computer Science ���������������������������������������������������������������������������������������������������������� 14
Theoretical Computer Science�������������������������������������������������������������������������������������������������������������� 15
P ractical Computer Science ����������������������������������������������������������������������������������������������������������������� 15
Important Concepts ������������������������������������������������������������������������������������������������������������������������������ 15
D ata Science ������������������������������������������������������������������������������������������������������������������ 16
v
■ Contents
Mathematics ������������������������������������������������������������������������������������������������������������������ 18
Important Concepts ������������������������������������������������������������������������������������������������������������������������������ 19
Statistics ������������������������������������������������������������������������������������������������������������������������ 24
Data Mining �������������������������������������������������������������������������������������������������������������������� 25
Artifcial Intelligence ������������������������������������������������������������������������������������������������������ 25
Natural Language Processing ���������������������������������������������������������������������������������������� 26
D eep Learning ���������������������������������������������������������������������������������������������������������������� 28
Important Concepts ������������������������������������������������������������������������������������������������������������������������������ 31
Machine Learning Methods �������������������������������������������������������������������������������������������� 34
S upervised Learning ������������������������������������������������������������������������������������������������������ 35
Classifcation ���������������������������������������������������������������������������������������������������������������������������������������� 36
Regression �������������������������������������������������������������������������������������������������������������������������������������������� 37
U nsupervised Learning �������������������������������������������������������������������������������������������������� 38
C lustering ��������������������������������������������������������������������������������������������������������������������������������������������� 39
Dimensionality Reduction ��������������������������������������������������������������������������������������������������������������������� 40
Anomaly Detection�������������������������������������������������������������������������������������������������������������������������������� 41
Association Rule-Mining ����������������������������������������������������������������������������������������������������������������������� 41
Semi-Supervised Learning ��������������������������������������������������������������������������������������������� 42
R einforcement Learning ������������������������������������������������������������������������������������������������� 42
Batch Learning ��������������������������������������������������������������������������������������������������������������� 43
Online Learning �������������������������������������������������������������������������������������������������������������� 44
Instance Based Learning ������������������������������������������������������������������������������������������������ 44
Model Based Learning ���������������������������������������������������������������������������������������������������� 45
The CRISP-DM Process Model ���������������������������������������������������������������������������������������� 45
Business Understanding ����������������������������������������������������������������������������������������������������������������������� 46
Data Understanding ������������������������������������������������������������������������������������������������������������������������������ 48
Data Preparation ����������������������������������������������������������������������������������������������������������������������������������� 50
Modeling ����������������������������������������������������������������������������������������������������������������������������������������������� 51
Evaluation ��������������������������������������������������������������������������������������������������������������������������������������������� 52
Deployment������������������������������������������������������������������������������������������������������������������������������������������� 52
vi
■ Contents
Building Machine Intelligence ���������������������������������������������������������������������������������������� 52
Machine Learning Pipelines ����������������������������������������������������������������������������������������������������������������� 52
S upervised Machine Learning Pipeline ������������������������������������������������������������������������������������������������ 54
Unsupervised Machine Learning Pipeline �������������������������������������������������������������������������������������������� 55
Real-World Case Study: Predicting Student Grant Recommendations ��������������������������� 55
Objective ����������������������������������������������������������������������������������������������������������������������������������������������� 56
Data Retrieval ��������������������������������������������������������������������������������������������������������������������������������������� 56
Data Preparation ����������������������������������������������������������������������������������������������������������������������������������� 57
Modeling ����������������������������������������������������������������������������������������������������������������������������������������������� 60
Model Evaluation ���������������������������������������������������������������������������������������������������������������������������������� 61
Model Deployment �������������������������������������������������������������������������������������������������������������������������������� 61
Prediction in Action ������������������������������������������������������������������������������������������������������������������������������� 62
Challenges in Machine Learning ������������������������������������������������������������������������������������ 64
Real-World Applications of Machine Learning ��������������������������������������������������������������� 64
Summary ������������������������������������������������������������������������������������������������������������������������ 65
■ Chapter 2: The Python Machine Learning Ecosystem ����������������������������������������� 67
Python: An Introduction �������������������������������������������������������������������������������������������������� 67
Strengths ���������������������������������������������������������������������������������������������������������������������������������������������� 68
Pitfalls ��������������������������������������������������������������������������������������������������������������������������������������������������� 68
Setting Up a Python Environment ��������������������������������������������������������������������������������������������������������� 69
Why Python for Data Science? ������������������������������������������������������������������������������������������������������������� 71
Introducing the Python Machine Learning Ecosystem ��������������������������������������������������� 72
J upyter Notebooks �������������������������������������������������������������������������������������������������������������������������������� 72
NumPy �������������������������������������������������������������������������������������������������������������������������������������������������� 75
Pandas �������������������������������������������������������������������������������������������������������������������������������������������������� 84
Scikit-learn ������������������������������������������������������������������������������������������������������������������������������������������� 96
Neural Networks and Deep Learning �������������������������������������������������������������������������������������������������� 102
Text Analytics and Natural Language Processing ������������������������������������������������������������������������������� 112
Statsmodels ���������������������������������������������������������������������������������������������������������������������������������������� 116
Summary ���������������������������������������������������������������������������������������������������������������������� 118
vii
■ Contents
■ Part II: The Machine Learning Pipeline ������������������������������������������� 119
■ Chapter 3: Processing, Wrangling, and Visualizing Data ����������������������������������� 121
D ata Collection ������������������������������������������������������������������������������������������������������������� 122
C SV ����������������������������������������������������������������������������������������������������������������������������������������������������� 122
J SON ��������������������������������������������������������������������������������������������������������������������������������������������������� 124
XML ����������������������������������������������������������������������������������������������������������������������������������������������������� 128
H TML and Scraping ���������������������������������������������������������������������������������������������������������������������������� 131
S QL ����������������������������������������������������������������������������������������������������������������������������������������������������� 136
D ata Description ����������������������������������������������������������������������������������������������������������� 137
Numeric ���������������������������������������������������������������������������������������������������������������������������������������������� 137
Text ����������������������������������������������������������������������������������������������������������������������������������������������������� 137
Categorical ����������������������������������������������������������������������������������������������������������������������������������������� 137
D ata Wrangling ������������������������������������������������������������������������������������������������������������� 138
U nderstanding Data ���������������������������������������������������������������������������������������������������������������������������� 138
F iltering Data �������������������������������������������������������������������������������������������������������������������������������������� 141
Typecasting ����������������������������������������������������������������������������������������������������������������������������������������� 144
Transformations ���������������������������������������������������������������������������������������������������������������������������������� 144
Imputing Missing Values ��������������������������������������������������������������������������������������������������������������������� 145
H andling Duplicates ���������������������������������������������������������������������������������������������������������������������������� 147
Handling Categorical Data ������������������������������������������������������������������������������������������������������������������ 147
N ormalizing Values ����������������������������������������������������������������������������������������������������������������������������� 148
String Manipulations �������������������������������������������������������������������������������������������������������������������������� 149
Data Summarization ����������������������������������������������������������������������������������������������������� 149
Data Visualization ��������������������������������������������������������������������������������������������������������� 151
Visualizing with Pandas ���������������������������������������������������������������������������������������������������������������������� 152
Visualizing with Matplotlib������������������������������������������������������������������������������������������������������������������ 161
P ython Visualization Ecosystem ��������������������������������������������������������������������������������������������������������� 176
Summary ���������������������������������������������������������������������������������������������������������������������� 176
viii
■ Contents
■ Chapter 4: Feature Engineering and Selection �������������������������������������������������� 177
F eatures: Understand Your Data Better ������������������������������������������������������������������������ 178
D ata and Datasets ������������������������������������������������������������������������������������������������������������������������������ 178
Features ���������������������������������������������������������������������������������������������������������������������������������������������� 179
Models ������������������������������������������������������������������������������������������������������������������������������������������������ 179
Revisiting the Machine Learning Pipeline �������������������������������������������������������������������� 179
F eature Extraction and Engineering ����������������������������������������������������������������������������� 181
W hat Is Feature Engineering?������������������������������������������������������������������������������������������������������������� 181
Why Feature Engineering? ������������������������������������������������������������������������������������������������������������������ 183
How Do You Engineer Features? ��������������������������������������������������������������������������������������������������������� 184
Feature Engineering on Numeric Data ������������������������������������������������������������������������� 185
Raw Measures ������������������������������������������������������������������������������������������������������������������������������������ 185
Binarization ����������������������������������������������������������������������������������������������������������������������������������������� 187
Rounding �������������������������������������������������������������������������������������������������������������������������������������������� 188
I nteractions ����������������������������������������������������������������������������������������������������������������������������������������� 189
B inning ����������������������������������������������������������������������������������������������������������������������������������������������� 191
Statistical Transformations ����������������������������������������������������������������������������������������������������������������� 197
Feature Engineering on Categorical Data ��������������������������������������������������������������������� 200
Transforming Nominal Features ��������������������������������������������������������������������������������������������������������� 201
T ransforming Ordinal Features ����������������������������������������������������������������������������������������������������������� 202
Encoding Categorical Features ����������������������������������������������������������������������������������������������������������� 203
Feature Engineering on Text Data �������������������������������������������������������������������������������� 209
Text Pre-Processing ���������������������������������������������������������������������������������������������������������������������������� 210
B ag of Words Model ���������������������������������������������������������������������������������������������������������������������������� 211
Bag of N-Grams Model ����������������������������������������������������������������������������������������������������������������������� 212
TF-IDF Model �������������������������������������������������������������������������������������������������������������������������������������� 213
Document Similarity ��������������������������������������������������������������������������������������������������������������������������� 214
T opic Models ��������������������������������������������������������������������������������������������������������������������������������������� 216
Word Embeddings ������������������������������������������������������������������������������������������������������������������������������� 217
ix
■ Contents
Feature Engineering on Temporal Data ������������������������������������������������������������������������ 220
Date-Based Features �������������������������������������������������������������������������������������������������������������������������� 221
T ime-Based Features ������������������������������������������������������������������������������������������������������������������������� 222
F eature Engineering on Image Data ����������������������������������������������������������������������������� 224
I mage Metadata Features ������������������������������������������������������������������������������������������������������������������� 225
Raw Image and Channel Pixels ���������������������������������������������������������������������������������������������������������� 225
Grayscale Image Pixels ����������������������������������������������������������������������������������������������������������������������� 227
B inning Image Intensity Distribution �������������������������������������������������������������������������������������������������� 227
Image Aggregation Statistics �������������������������������������������������������������������������������������������������������������� 228
E dge Detection ����������������������������������������������������������������������������������������������������������������������������������� 229
Object Detection ��������������������������������������������������������������������������������������������������������������������������������� 230
Localized Feature Extraction �������������������������������������������������������������������������������������������������������������� 231
Visual Bag of Words Model ����������������������������������������������������������������������������������������������������������������� 233
Automated Feature Engineering with Deep Learning ������������������������������������������������������������������������� 236
Feature Scaling ������������������������������������������������������������������������������������������������������������ 239
Standardized Scaling �������������������������������������������������������������������������������������������������������������������������� 240
M in-Max Scaling ��������������������������������������������������������������������������������������������������������������������������������� 240
R obust Scaling ������������������������������������������������������������������������������������������������������������������������������������ 241
Feature Selection ��������������������������������������������������������������������������������������������������������� 242
Threshold-Based Methods ������������������������������������������������������������������������������������������������������������������ 243
S tatistical Methods ����������������������������������������������������������������������������������������������������������������������������� 244
Recursive Feature Elimination ������������������������������������������������������������������������������������������������������������ 247
M odel-Based Selection ����������������������������������������������������������������������������������������������������������������������� 248
Dimensionality Reduction ��������������������������������������������������������������������������������������������� 249
F eature Extraction with Principal Component Analysis ���������������������������������������������������������������������� 250
Summary ���������������������������������������������������������������������������������������������������������������������� 252
■ Chapter 5: Building, Tuning, and Deploying Models ������������������������������������������ 255
B uilding Models ������������������������������������������������������������������������������������������������������������ 256
M odel Types ���������������������������������������������������������������������������������������������������������������������������������������� 257
L earning a Model �������������������������������������������������������������������������������������������������������������������������������� 260
M odel Building Examples ������������������������������������������������������������������������������������������������������������������� 263
x