0% found this document useful (0 votes)
62 views31 pages

Python Data Analytics: With Pandas, NumPy, and Matplotlib, 3rd Edition Fabio Nelli Download

The document provides information about the book 'Python Data Analytics: With Pandas, NumPy, and Matplotlib, 3rd Edition' by Fabio Nelli, including details on its content, structure, and various chapters covering data analysis processes, Python programming, and libraries like NumPy and pandas. It also includes links to download the book and other related resources. The book aims to equip readers with the necessary skills for data analysis using Python.

Uploaded by

ubbuqafc409
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views31 pages

Python Data Analytics: With Pandas, NumPy, and Matplotlib, 3rd Edition Fabio Nelli Download

The document provides information about the book 'Python Data Analytics: With Pandas, NumPy, and Matplotlib, 3rd Edition' by Fabio Nelli, including details on its content, structure, and various chapters covering data analysis processes, Python programming, and libraries like NumPy and pandas. It also includes links to download the book and other related resources. The book aims to equip readers with the necessary skills for data analysis using Python.

Uploaded by

ubbuqafc409
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Python Data Analytics: With Pandas, NumPy, and

Matplotlib, 3rd Edition Fabio Nelli download pdf

https://ebookmass.com/product/python-data-analytics-with-pandas-numpy-
and-matplotlib-3rd-edition-fabio-nelli-2/

Visit ebookmass.com today to download the complete set of


ebook or textbook!
We have selected some products that you may be interested in
Click the link to download now or visit ebookmass.com
for more options!.

Python Data Analytics: With Pandas, NumPy, and Matplotlib,


3rd Edition Fabio Nelli

https://ebookmass.com/product/python-data-analytics-with-pandas-numpy-
and-matplotlib-3rd-edition-fabio-nelli/

Machine Learning with Python for Everyone (Addison Wesley


Data & Analytics Series) 1st Edition, (Ebook PDF)

https://ebookmass.com/product/machine-learning-with-python-for-
everyone-addison-wesley-data-analytics-series-1st-edition-ebook-pdf/

Advanced Data Analytics Using Python : With Architectural


Patterns, Text and Image Classification, and Optimization
Techniques 2nd Edition Sayan Mukhopadhyay
https://ebookmass.com/product/advanced-data-analytics-using-python-
with-architectural-patterns-text-and-image-classification-and-
optimization-techniques-2nd-edition-sayan-mukhopadhyay/

Data-Driven SEO with Python: Solve SEO Challenges with


Data Science Using Python 1st Edition Andreas Voniatis

https://ebookmass.com/product/data-driven-seo-with-python-solve-seo-
challenges-with-data-science-using-python-1st-edition-andreas-
voniatis/
Data Mining for Business Analytics: Concepts, Techniques
and Applications in Python eBook

https://ebookmass.com/product/data-mining-for-business-analytics-
concepts-techniques-and-applications-in-python-ebook/

Data Quality: Empowering Businesses with Analytics and AI


Prashanth Southekal

https://ebookmass.com/product/data-quality-empowering-businesses-with-
analytics-and-ai-prashanth-southekal/

ISE Data Analytics for Accounting 3rd Edition Vernon


Richardson Professor

https://ebookmass.com/product/ise-data-analytics-for-accounting-3rd-
edition-vernon-richardson-professor/

Data Science from Scratch: First Principles with Python


2nd Edition

https://ebookmass.com/product/data-science-from-scratch-first-
principles-with-python-2nd-edition/

Health Analytics with Python: A Comprehensive Guide for


2024 Van Der Post

https://ebookmass.com/product/health-analytics-with-python-a-
comprehensive-guide-for-2024-van-der-post/
Python Data
Analytics
With Pandas, NumPy, and Matplotlib

Third Edition

Fabio Nelli
Python Data Analytics
With Pandas, NumPy, and Matplotlib

Third Edition

Fabio Nelli
Python Data Analytics: With Pandas, NumPy, and Matplotlib
Fabio Nelli
Rome, Italy

ISBN-13 (pbk): 978-1-4842-9531-1 ISBN-13 (electronic): 978-1-4842-9532-8


https://doi.org/10.1007/978-1-4842-9532-8

Copyright © 2023 by Fabio Nelli


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
Copy Editor: Kezia Endsley
Cover image by Tyler B on Unsplash (www.unsplash.com)
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 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 [email protected]; for reprint,
paperback, or audio rights, please e-mail [email protected].
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 http://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 https://www.apress.com/
gp/services/source-code.
Paper in this product is recyclable
“Science leads us forward in knowledge, but only analysis makes us more aware”

This book is dedicated to all those who are constantly looking for awareness
Table of Contents

About the Author���������������������������������������������������������������������������������������������������xvii


About the Technical Reviewer��������������������������������������������������������������������������������xix
Preface�������������������������������������������������������������������������������������������������������������������xxi


■Chapter 1: An Introduction to Data Analysis��������������������������������������������������������� 1
Data Analysis�������������������������������������������������������������������������������������������������������������������� 1
Knowledge Domains of the Data Analyst������������������������������������������������������������������������� 2
Computer Science���������������������������������������������������������������������������������������������������������������������������������� 2
Mathematics and Statistics�������������������������������������������������������������������������������������������������������������������� 3
Machine Learning and Artificial Intelligence������������������������������������������������������������������������������������������ 3
Professional Fields of Application����������������������������������������������������������������������������������������������������������� 3

Understanding the Nature of the Data������������������������������������������������������������������������������ 4


When the Data Become Information������������������������������������������������������������������������������������������������������� 4
When the Information Becomes Knowledge������������������������������������������������������������������������������������������� 4
Types of Data������������������������������������������������������������������������������������������������������������������������������������������ 4

The Data Analysis Process����������������������������������������������������������������������������������������������� 4


Problem Definition���������������������������������������������������������������������������������������������������������������������������������� 5
Data Extraction��������������������������������������������������������������������������������������������������������������������������������������� 6
Data Preparation������������������������������������������������������������������������������������������������������������������������������������� 6
Data Exploration/Visualization���������������������������������������������������������������������������������������������������������������� 7
Predictive Modeling�������������������������������������������������������������������������������������������������������������������������������� 7
Model Validation������������������������������������������������������������������������������������������������������������������������������������� 8
Deployment�������������������������������������������������������������������������������������������������������������������������������������������� 8

v
■ Table of Contents

Quantitative and Qualitative Data Analysis����������������������������������������������������������������������� 9


Open Data������������������������������������������������������������������������������������������������������������������������� 9
Python and Data Analysis����������������������������������������������������������������������������������������������� 12
Conclusions�������������������������������������������������������������������������������������������������������������������� 13

■Chapter 2: Introduction to the Python World������������������������������������������������������ 15
Python—The Programming Language��������������������������������������������������������������������������� 15
The Interpreter and the Execution Phases of the Code������������������������������������������������������������������������ 16
Installing Python������������������������������������������������������������������������������������������������������������� 18
Python Distributions����������������������������������������������������������������������������������������������������������������������������� 19
Using Python����������������������������������������������������������������������������������������������������������������������������������������� 23
Writing Python Code����������������������������������������������������������������������������������������������������������������������������� 26
IPython�������������������������������������������������������������������������������������������������������������������������������������������������� 30

PyPI—The Python Package Index���������������������������������������������������������������������������������� 36


The IDEs for Python������������������������������������������������������������������������������������������������������������������������������ 37

SciPy������������������������������������������������������������������������������������������������������������������������������ 42
NumPy�������������������������������������������������������������������������������������������������������������������������������������������������� 42
Pandas�������������������������������������������������������������������������������������������������������������������������������������������������� 43
matplotlib��������������������������������������������������������������������������������������������������������������������������������������������� 43

Conclusions�������������������������������������������������������������������������������������������������������������������� 43

■Chapter 3: The NumPy Library����������������������������������������������������������������������������� 45
NumPy: A Little History��������������������������������������������������������������������������������������������������� 45
The NumPy Installation�������������������������������������������������������������������������������������������������� 46
ndarray: The Heart of the Library����������������������������������������������������������������������������������� 47
Create an Array������������������������������������������������������������������������������������������������������������������������������������� 48
Types of Data���������������������������������������������������������������������������������������������������������������������������������������� 49
The dtype Option���������������������������������������������������������������������������������������������������������������������������������� 50
Intrinsic Creation of an Array���������������������������������������������������������������������������������������������������������������� 50

Basic Operations������������������������������������������������������������������������������������������������������������ 51
Arithmetic Operators���������������������������������������������������������������������������������������������������������������������������� 52
The Matrix Product������������������������������������������������������������������������������������������������������������������������������� 53

vi
■ Table of Contents

Increment and Decrement Operators��������������������������������������������������������������������������������������������������� 54


Universal Functions (ufunc)������������������������������������������������������������������������������������������������������������������ 54
Aggregate Functions���������������������������������������������������������������������������������������������������������������������������� 55

Indexing, Slicing, and Iterating��������������������������������������������������������������������������������������� 55


Indexing������������������������������������������������������������������������������������������������������������������������������������������������ 55
Slicing��������������������������������������������������������������������������������������������������������������������������������������������������� 57
Iterating an Array���������������������������������������������������������������������������������������������������������������������������������� 59

Conditions and Boolean Arrays�������������������������������������������������������������������������������������� 60


Shape Manipulation������������������������������������������������������������������������������������������������������� 61
Array Manipulation��������������������������������������������������������������������������������������������������������� 62
Joining Arrays��������������������������������������������������������������������������������������������������������������������������������������� 62
Splitting Arrays������������������������������������������������������������������������������������������������������������������������������������� 63

General Concepts����������������������������������������������������������������������������������������������������������� 64
Copies or Views of Objects������������������������������������������������������������������������������������������������������������������� 64
Vectorization����������������������������������������������������������������������������������������������������������������������������������������� 65
Broadcasting���������������������������������������������������������������������������������������������������������������������������������������� 66

Structured Arrays����������������������������������������������������������������������������������������������������������� 68
Reading and Writing Array Data on Files������������������������������������������������������������������������ 70
Loading and Saving Data in Binary Files���������������������������������������������������������������������������������������������� 70
Reading Files with Tabular Data����������������������������������������������������������������������������������������������������������� 70

Conclusions�������������������������������������������������������������������������������������������������������������������� 72

■Chapter 4: The pandas Library—An Introduction������������������������������������������������ 73
pandas: The Python Data Analysis Library��������������������������������������������������������������������� 73
Installation of pandas����������������������������������������������������������������������������������������������������� 74
Installation from Anaconda������������������������������������������������������������������������������������������������������������������� 74
Installation from PyPI���������������������������������������������������������������������������������������������������������������������������� 78

Getting Started with pandas������������������������������������������������������������������������������������������� 78


Introduction to pandas Data Structures������������������������������������������������������������������������� 79

vii
■ Table of Contents

The Series��������������������������������������������������������������������������������������������������������������������������������������������� 80
The Dataframe�������������������������������������������������������������������������������������������������������������������������������������� 87
The Index Objects��������������������������������������������������������������������������������������������������������������������������������� 94

Other Functionalities on Indexes������������������������������������������������������������������������������������ 96


Reindexing�������������������������������������������������������������������������������������������������������������������������������������������� 96
Dropping����������������������������������������������������������������������������������������������������������������������������������������������� 98
Arithmetic and Data Alignment������������������������������������������������������������������������������������������������������������� 99

Operations Between Data Structures��������������������������������������������������������������������������� 100


Flexible Arithmetic Methods��������������������������������������������������������������������������������������������������������������� 100
Operations Between Dataframes and Series�������������������������������������������������������������������������������������� 101

Function Application and Mapping������������������������������������������������������������������������������� 102


Functions by Element������������������������������������������������������������������������������������������������������������������������� 102
Functions by Row or Column�������������������������������������������������������������������������������������������������������������� 102
Statistics Functions���������������������������������������������������������������������������������������������������������������������������� 103

Sorting and Ranking����������������������������������������������������������������������������������������������������� 104


Correlation and Covariance������������������������������������������������������������������������������������������ 107
“Not a Number” Data��������������������������������������������������������������������������������������������������� 108
Assigning a NaN Value������������������������������������������������������������������������������������������������������������������������ 108
Filtering Out NaN Values��������������������������������������������������������������������������������������������������������������������� 109
Filling in NaN Occurrences����������������������������������������������������������������������������������������������������������������� 110

Hierarchical Indexing and Leveling������������������������������������������������������������������������������ 110


Reordering and Sorting Levels����������������������������������������������������������������������������������������������������������� 112
Summary Statistics with groupby Instead of with Level�������������������������������������������������������������������� 113

Conclusions������������������������������������������������������������������������������������������������������������������ 114

■Chapter 5: pandas: Reading and Writing Data��������������������������������������������������� 115
I/O API Tools������������������������������������������������������������������������������������������������������������������ 115
CSV and Textual Files��������������������������������������������������������������������������������������������������� 116
Reading Data in CSV or Text Files��������������������������������������������������������������������������������� 116
Using Regexp to Parse TXT Files�������������������������������������������������������������������������������������������������������� 119
Reading TXT Files Into Parts��������������������������������������������������������������������������������������������������������������� 121
Writing Data in CSV���������������������������������������������������������������������������������������������������������������������������� 121
viii
■ Table of Contents

Reading and Writing HTML Files���������������������������������������������������������������������������������� 123


Writing Data in HTML�������������������������������������������������������������������������������������������������������������������������� 124
Reading Data from an HTML File�������������������������������������������������������������������������������������������������������� 126

Reading Data from XML����������������������������������������������������������������������������������������������� 127


Reading and Writing Data on Microsoft Excel Files������������������������������������������������������ 129
JSON Data�������������������������������������������������������������������������������������������������������������������� 131
The HDF5 Format��������������������������������������������������������������������������������������������������������� 135
Pickle—Python Object Serialization����������������������������������������������������������������������������� 136
Serialize a Python Object with cPickle����������������������������������������������������������������������������������������������� 136
Pickling with pandas�������������������������������������������������������������������������������������������������������������������������� 137

Interacting with Databases������������������������������������������������������������������������������������������ 137


Loading and Writing Data with SQLite3���������������������������������������������������������������������������������������������� 138
Loading and Writing Data with PostgreSQL in a Docker Container���������������������������������������������������� 140

Reading and Writing Data with a NoSQL Database: MongoDB������������������������������������� 146


Conclusions������������������������������������������������������������������������������������������������������������������ 148

■Chapter 6: pandas in Depth: Data Manipulation������������������������������������������������ 149
Data Preparation���������������������������������������������������������������������������������������������������������� 149
Merging���������������������������������������������������������������������������������������������������������������������������������������������� 150

Concatenating�������������������������������������������������������������������������������������������������������������� 154
Combining������������������������������������������������������������������������������������������������������������������������������������������ 156
Pivoting����������������������������������������������������������������������������������������������������������������������������������������������� 157
Removing�������������������������������������������������������������������������������������������������������������������������������������������� 160

Data Transformation����������������������������������������������������������������������������������������������������� 161


Removing Duplicates�������������������������������������������������������������������������������������������������������������������������� 161
Mapping���������������������������������������������������������������������������������������������������������������������������������������������� 162

Discretization and Binning������������������������������������������������������������������������������������������� 166


Detecting and Filtering Outliers���������������������������������������������������������������������������������������������������������� 168

Permutation������������������������������������������������������������������������������������������������������������������ 169
Random Sampling������������������������������������������������������������������������������������������������������������������������������ 170

ix
■ Table of Contents

String Manipulation������������������������������������������������������������������������������������������������������ 170


Built-in Methods for String Manipulation������������������������������������������������������������������������������������������� 170
Regular Expressions��������������������������������������������������������������������������������������������������������������������������� 172

Data Aggregation���������������������������������������������������������������������������������������������������������� 173


GroupBy���������������������������������������������������������������������������������������������������������������������������������������������� 174
A Practical Example���������������������������������������������������������������������������������������������������������������������������� 175
Hierarchical Grouping������������������������������������������������������������������������������������������������������������������������� 176
Group Iteration������������������������������������������������������������������������������������������������������������� 176
Chain of Transformations�������������������������������������������������������������������������������������������������������������������� 177
Functions on Groups��������������������������������������������������������������������������������������������������������������������������� 178

Advanced Data Aggregation����������������������������������������������������������������������������������������� 179


Conclusions������������������������������������������������������������������������������������������������������������������ 181

■Chapter 7: Data Visualization with matplotlib and Seaborn������������������������������ 183
The matplotlib Library�������������������������������������������������������������������������������������������������� 183
Installation�������������������������������������������������������������������������������������������������������������������� 184
The matplotlib Architecture������������������������������������������������������������������������������������������ 185
Backend Layer������������������������������������������������������������������������������������������������������������������������������������ 186
Artist Layer����������������������������������������������������������������������������������������������������������������������������������������� 186
Scripting Layer (pyplot)���������������������������������������������������������������������������������������������������������������������� 188
pylab and pyplot��������������������������������������������������������������������������������������������������������������������������������� 188

pyplot��������������������������������������������������������������������������������������������������������������������������� 189
The Plotting Window��������������������������������������������������������������������������������������������������������������������������� 189

Data Visualization with Jupyter Notebook�������������������������������������������������������������������� 191


Set the Properties of the Plot�������������������������������������������������������������������������������������������������������������� 192
matplotlib and NumPy������������������������������������������������������������������������������������������������������������������������ 194

Using kwargs���������������������������������������������������������������������������������������������������������������� 196


Working with Multiple Figures and Axes�������������������������������������������������������������������������������������������� 196

Adding Elements to the Chart��������������������������������������������������������������������������������������� 198


Adding Text����������������������������������������������������������������������������������������������������������������������������������������� 198
Adding a Grid�������������������������������������������������������������������������������������������������������������������������������������� 202
Adding a Legend��������������������������������������������������������������������������������������������������������������������������������� 203
x
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
■ Table of Contents

Saving Your Charts������������������������������������������������������������������������������������������������������� 206


Saving the Code���������������������������������������������������������������������������������������������������������������������������������� 206
Saving Your Notebook as an HTML File or as Other File Formats������������������������������������������������������� 207
Saving Your Chart Directly as an Image���������������������������������������������������������������������������������������������� 208

Handling Date Values��������������������������������������������������������������������������������������������������� 208


Chart Typology�������������������������������������������������������������������������������������������������������������� 211
Line Charts������������������������������������������������������������������������������������������������������������������� 211
Line Charts with pandas��������������������������������������������������������������������������������������������������������������������� 217

Histograms������������������������������������������������������������������������������������������������������������������� 218
Bar Charts�������������������������������������������������������������������������������������������������������������������� 219
Horizontal Bar Charts�������������������������������������������������������������������������������������������������������������������������� 222
Multiserial Bar Charts������������������������������������������������������������������������������������������������������������������������� 223
Multiseries Bar Charts with a pandas Dataframe������������������������������������������������������������������������������� 225
Multiseries Stacked Bar Charts���������������������������������������������������������������������������������������������������������� 227
Stacked Bar Charts with a pandas Dataframe������������������������������������������������������������������������������������ 229
Other Bar Chart Representations�������������������������������������������������������������������������������������������������������� 230

Pie Charts��������������������������������������������������������������������������������������������������������������������� 231


Pie Charts with a pandas Dataframe�������������������������������������������������������������������������������������������������� 234

Advanced Charts���������������������������������������������������������������������������������������������������������� 235


Contour Plots�������������������������������������������������������������������������������������������������������������������������������������� 235
Polar Charts���������������������������������������������������������������������������������������������������������������������������������������� 236

The mplot3d Toolkit������������������������������������������������������������������������������������������������������ 237


3D Surfaces���������������������������������������������������������������������������������������������������������������������������������������� 238
Scatter Plots in 3D������������������������������������������������������������������������������������������������������������������������������ 239
Bar Charts in 3D��������������������������������������������������������������������������������������������������������������������������������� 240

Multipanel Plots������������������������������������������������������������������������������������������������������������ 241


Display Subplots Within Other Subplots��������������������������������������������������������������������������������������������� 241
Grids of Subplots�������������������������������������������������������������������������������������������������������������������������������� 243

The Seaborn Library����������������������������������������������������������������������������������������������������� 245


Conclusions������������������������������������������������������������������������������������������������������������������ 257

xi
■ Table of Contents


■Chapter 8: Machine Learning with scikit-learn������������������������������������������������� 259
The scikit-learn Library������������������������������������������������������������������������������������������������ 259
Machine Learning��������������������������������������������������������������������������������������������������������� 259
Supervised and Unsupervised Learning��������������������������������������������������������������������������������������������� 259
Training Set and Testing Set��������������������������������������������������������������������������������������������������������������� 260

Supervised Learning with scikit-learn������������������������������������������������������������������������� 260


The Iris Flower Dataset������������������������������������������������������������������������������������������������ 261
The PCA Decomposition��������������������������������������������������������������������������������������������������������������������� 264

K-Nearest Neighbors Classifier������������������������������������������������������������������������������������ 267


Diabetes Dataset���������������������������������������������������������������������������������������������������������� 271
Linear Regression: The Least Square Regression�������������������������������������������������������� 272
Support Vector Machines (SVMs)��������������������������������������������������������������������������������� 276
Support Vector Classification (SVC)���������������������������������������������������������������������������������������������������� 277
Nonlinear SVC������������������������������������������������������������������������������������������������������������������������������������� 281
Plotting Different SVM Classifiers Using the Iris Dataset�������������������������������������������������������������������� 283
Support Vector Regression (SVR)�������������������������������������������������������������������������������������������������������� 285

Conclusions������������������������������������������������������������������������������������������������������������������ 287

■Chapter 9: Deep Learning with TensorFlow������������������������������������������������������� 289
Artificial Intelligence, Machine Learning, and Deep Learning�������������������������������������� 289
Artificial Intelligence��������������������������������������������������������������������������������������������������������������������������� 289
Machine Learning Is a Branch of Artificial Intelligence���������������������������������������������������������������������� 290
Deep Learning Is a Branch of Machine Learning�������������������������������������������������������������������������������� 290
The Relationship Between Artificial Intelligence, Machine Learning, and Deep Learning������������������ 290

Deep Learning�������������������������������������������������������������������������������������������������������������� 291


Neural Networks and GPUs����������������������������������������������������������������������������������������������������������������� 291
Data Availability: Open Data Source, Internet of Things, and Big Data����������������������������������������������� 292
Python������������������������������������������������������������������������������������������������������������������������������������������������� 292
Deep Learning Python Frameworks��������������������������������������������������������������������������������������������������� 292

Artificial Neural Networks�������������������������������������������������������������������������������������������� 293

xii
■ Table of Contents

How Artificial Neural Networks Are Structured���������������������������������������������������������������������������������� 293


Single Layer Perceptron (SLP)������������������������������������������������������������������������������������������������������������ 294
Multilayer Perceptron (MLP)��������������������������������������������������������������������������������������������������������������� 296
Correspondence Between Artificial and Biological Neural Networks������������������������������������������������� 297

TensorFlow������������������������������������������������������������������������������������������������������������������� 298
TensorFlow: Google’s Framework������������������������������������������������������������������������������������������������������� 298
TensorFlow: Data Flow Graph������������������������������������������������������������������������������������������������������������� 298

Start Programming with TensorFlow���������������������������������������������������������������������������� 299


TensorFlow 2.x vs TensorFlow 1.x������������������������������������������������������������������������������������������������������ 299
Installing TensorFlow�������������������������������������������������������������������������������������������������������������������������� 300
Programming with the Jupyter Notebook������������������������������������������������������������������������������������������� 300
Tensors����������������������������������������������������������������������������������������������������������������������������������������������� 300
Loading Data Into a Tensor from a pandas Dataframe����������������������������������������������������������������������� 303
Loading Data in a Tensor from a CSV File������������������������������������������������������������������������������������������� 304
Operation on Tensors�������������������������������������������������������������������������������������������������������������������������� 306

Developing a Deep Learning Model with TensorFlow��������������������������������������������������� 307


Model Building������������������������������������������������������������������������������������������������������������� 307
Model Compiling���������������������������������������������������������������������������������������������������������� 308
Model Training and Testing������������������������������������������������������������������������������������������� 309
Prediction Making�������������������������������������������������������������������������������������������������������� 309
Practical Examples with TensorFlow 2.x���������������������������������������������������������������������� 310
Single Layer Perceptron with TensorFlow������������������������������������������������������������������������������������������ 310
Multilayer Perceptron (with One Hidden Layer) with TensorFlow������������������������������������������������������� 317
Multilayer Perceptron (with Two Hidden Layers) with TensorFlow����������������������������������������������������� 319

Conclusions������������������������������������������������������������������������������������������������������������������ 321

■Chapter 10: An Example—Meteorological Data������������������������������������������������ 323
A Hypothesis to Be Tested: The Influence of the Proximity of the Sea������������������������� 323
The System in the Study: The Adriatic Sea and the Po Valley������������������������������������������������������������� 323

Finding the Data Source����������������������������������������������������������������������������������������������� 327


Data Analysis on Jupyter Notebook������������������������������������������������������������������������������ 328

xiii
■ Table of Contents

Analysis of Processed Meteorological Data����������������������������������������������������������������� 332


The RoseWind�������������������������������������������������������������������������������������������������������������� 343
Calculating the Mean Distribution of the Wind Speed������������������������������������������������������������������������ 347

Conclusions������������������������������������������������������������������������������������������������������������������ 348

■Chapter 11: Embedding the JavaScript D3 Library in the IPython Notebook���� 349
The Open Data Source for Demographics�������������������������������������������������������������������� 349
The JavaScript D3 Library�������������������������������������������������������������������������������������������� 352
Drawing a Clustered Bar Chart������������������������������������������������������������������������������������� 355
The Choropleth Maps��������������������������������������������������������������������������������������������������� 358
The Choropleth Map of the U.S. Population in 2022����������������������������������������������������� 362
Conclusions������������������������������������������������������������������������������������������������������������������ 366

■Chapter 12: Recognizing Handwritten Digits���������������������������������������������������� 367
Handwriting Recognition���������������������������������������������������������������������������������������������� 367
Recognizing Handwritten Digits with scikit-learn�������������������������������������������������������� 367
The Digits Dataset�������������������������������������������������������������������������������������������������������� 368
Learning and Predicting����������������������������������������������������������������������������������������������� 370
Recognizing Handwritten Digits with TensorFlow�������������������������������������������������������� 372
Learning and Predicting with an SLP��������������������������������������������������������������������������� 376
Learning and Predicting with an MLP�������������������������������������������������������������������������� 381
Conclusions������������������������������������������������������������������������������������������������������������������ 384

■Chapter 13: Textual Data Analysis with NLTK���������������������������������������������������� 385
Text Analysis Techniques���������������������������������������������������������������������������������������������� 385
The Natural Language Toolkit (NLTK)�������������������������������������������������������������������������������������������������� 386
Import the NLTK Library and the NLTK Downloader Tool��������������������������������������������������������������������� 386
Search for a Word with NLTK�������������������������������������������������������������������������������������������������������������� 389
Analyze the Frequency of Words�������������������������������������������������������������������������������������������������������� 390
Select Words from Text����������������������������������������������������������������������������������������������������������������������� 392
Bigrams and Collocations������������������������������������������������������������������������������������������������������������������� 393
Preprocessing Steps��������������������������������������������������������������������������������������������������������������������������� 394

xiv
■ Table of Contents

Use Text on the Network��������������������������������������������������������������������������������������������������������������������� 397


Extract the Text from the HTML Pages������������������������������������������������������������������������������������������������ 398
Sentiment Analysis����������������������������������������������������������������������������������������������������������������������������� 399

Conclusions������������������������������������������������������������������������������������������������������������������ 401

■Chapter 14: Image Analysis and Computer Vision with OpenCV����������������������� 403
Image Analysis and Computer Vision��������������������������������������������������������������������������� 403
OpenCV and Python������������������������������������������������������������������������������������������������������ 404
OpenCV and Deep Learning������������������������������������������������������������������������������������������ 404
Installing OpenCV��������������������������������������������������������������������������������������������������������� 404
First Approaches to Image Processing and Analysis���������������������������������������������������� 404
Before Starting����������������������������������������������������������������������������������������������������������������������������������� 404
Load and Display an Image���������������������������������������������������������������������������������������������������������������� 405
Work with Images������������������������������������������������������������������������������������������������������������������������������� 406
Save the New Image��������������������������������������������������������������������������������������������������������������������������� 407
Elementary Operations on Images������������������������������������������������������������������������������������������������������ 407
Image Blending����������������������������������������������������������������������������������������������������������������������������������� 411

Image Analysis������������������������������������������������������������������������������������������������������������� 412


Edge Detection and Image Gradient Analysis��������������������������������������������������������������� 413
Edge Detection����������������������������������������������������������������������������������������������������������������������������������� 413
The Image Gradient Theory���������������������������������������������������������������������������������������������������������������� 413
A Practical Example of Edge Detection with the Image Gradient Analysis����������������������������������������� 415
A Deep Learning Example: Face Detection������������������������������������������������������������������� 420
Conclusions������������������������������������������������������������������������������������������������������������������ 422

■Appendix A: Writing Mathematical Expressions with LaTeX����������������������������� 423


■Appendix B: Open Data Sources������������������������������������������������������������������������ 435

Index��������������������������������������������������������������������������������������������������������������������� 437

xv
About the Author

Fabio Nelli is a data scientist and Python consultant who designs and develops Python applications for
data analysis and visualization. He also has experience in the scientific world, having performed various
data analysis roles in pharmaceutical chemistry for private research companies and universities. He has
been a computer consultant for many years at IBM, EDS, and Hewlett-Packard, along with several banks
and insurance companies. He holds a master’s degree in organic chemistry and a bachelor’s degree in
information technologies and automation systems, with many years of experience in life sciences (as a tech
specialist at Beckman Coulter, Tecan, and SCIEX).
For further info and other examples, visit his page at www.meccanismocomplesso.org and the GitHub
page at https://github.com/meccanismocomplesso.

xvii
About the Technical Reviewer

Akshay R. Kulkarni is an artificial intelligence (AI) and machine learning


(ML) evangelist and thought leader. He has consulted with several Fortune
500 and global enterprises to drive AI and data science–led strategic
transformations. He is a Google developer, an author, and a regular
speaker at major AI and data science conferences, including the O’Reilly
Strata Data & AI Conference and the Great International Developer
Summit (GIDS). He has been a visiting faculty member at some of the
top graduate institutes in India. In 2019, he was featured as one of India’s
“top 40 under 40” data scientists. In his spare time, Akshay enjoys reading,
writing, coding, and helping aspiring data scientists. He lives in Bangalore
with his family.

xix
Preface

About five years have passed since the last edition of this book. In drafting this third edition, I made some
necessary changes, both to the text and to the code. First, all the Python code has been ported to 3.8 and
greater, and all references to Python 2.x versions have been dropped. Some chapters required a total
rewrite because the content was no longer compatible. I'm referring to TensorFlow 3.x which, compared
to TensorFlow 2.x (covered in the previous edition), has completely revamped its entire reference system.
In five years, the deep learning modules and code developed with version 2.x have proven completely
unusable. Keras and all its modules have been incorporated into the TensorFlow library, replacing all the
classes, functions, and modules that performed similar functions. The construction of neural network
models, their learning phases, and the functions they use have all completely changed. In this edition,
therefore, you have the opportunity to learn the methods of TensorFlow 3.x and to acquire familiarity with
the concepts and new paradigms in the new version.
Regarding data visualization, I decided to add information about the Seaborn library to the matplotlib
chapter. Seaborn, although still in version 0.x, is proving to be a very useful matplotlib extension for data
analysis, thanks to its statistical display of plots and its compatibility with pandas dataframes. I hope that,
with this completely updated third edition, I can further entice you to study and deepen your data analysis
with Python. This book will be a valuable learning tool for you now, and serve as a dependable reference in
the future.
—Fabio Nelli

xxi
CHAPTER 1

An Introduction to Data Analysis

In this chapter, you’ll take your first steps in the world of data analysis, learning in detail the concepts and
processes that make up this discipline. The concepts discussed in this chapter are helpful background
for the following chapters, where these concepts and procedures are applied in the form of Python code,
through the use of several libraries that are discussed in later chapters.

Data Analysis
In a world increasingly centralized around information technology, huge amounts of data are produced
and stored each day. Often these data come from automatic detection systems, sensors, and scientific
instrumentation, or you produce them daily and subconsciously every time you make a withdrawal from the
bank or purchase something, when you record various blogs, or even when you post on social networks.
But what are the data? The data actually are not information, at least in terms of their form. In the
formless stream of bytes, at first glance it is difficult to understand their essence, if they are not strictly
numbers, words, or times. This information is actually the result of processing, which, taking into account a
certain dataset, extracts conclusions that can be used in various ways. This process of extracting information
from raw data is called data analysis.
The purpose of data analysis is to extract information that is not easily deducible but, when understood,
enables you to carry out studies on the mechanisms of the systems that produced the data. This in turn
allows you to forecast possible responses of these systems and their evolution in time.
Starting from a simple methodical approach to data protection, data analysis has become a real
discipline, leading to the development of real methodologies that generate models. The model is in fact
a translation of the system to a mathematical form. Once there is a mathematical or logical form that can
describe system responses under different levels of precision, you can predict its development or response
to certain inputs. Thus, the aim of data analysis is not the model, but the quality of its predictive power.
The predictive power of a model depends not only on the quality of the modeling techniques but also
on the ability to choose a good dataset upon which to build the entire analysis process. So the search for
data, their extraction, and their subsequent preparation, while representing preliminary activities of an
analysis, also belong to data analysis itself, because of their importance in the success of the results.
So far I have spoken of data, their handling, and their processing through calculation procedures. In
parallel to all the stages of data analysis processing, various methods of data visualization have also been
developed. In fact, to understand the data, both individually and in terms of the role they play in the dataset,
there is no better system than to develop the techniques of graphical representation. These techniques are
capable of transforming information, sometimes implicitly hidden, into figures, which help you more easily
understand the meaning of the data. Over the years, many display modes have been developed for different
modes of data display, called charts.

© Fabio Nelli 2023 1


F. Nelli, Python Data Analytics, https://doi.org/10.1007/978-1-4842-9532-8_1
Chapter 1 ■ An Introduction to Data Analysis

At the end of the data analysis process, you have a model and a set of graphical displays and you can
predict the responses of the system under study; after that, you move to the test phase. The model is tested
using another set of data for which you know the system response. These data do not define the predictive
model. Depending on the ability of the model to replicate real, observed responses, you get an error
calculation and knowledge of the validity of the model and its operating limits.
These results can be compared to any other models to understand if the newly created one is
more efficient than the existing ones. Once you have assessed that, you can move to the last phase of
data analysis—deployment. This phase consists of implementing the results produced by the analysis,
namely, implementing the decisions to be made based on the predictions generated by the model and its
associated risks.
Data analysis is well suited to many professional activities. So, knowledge of it and how it can be put
into practice is relevant. It allows you to test hypotheses and understand the systems you’ve analyzed
more deeply.

Knowledge Domains of the Data Analyst


Data analysis is basically a discipline suitable to the study of problems that occur in several fields of
applications. Moreover, data analysis includes many tools and methodologies and requires knowledge of
computing, mathematical, and statistical concepts.
A good data analyst must be able to move and act in many disciplinary areas. Many of these disciplines
are the basis of the data analysis methods, and proficiency in them is almost necessary. Knowledge of other
disciplines is necessary, depending on the area of application and the particular data analysis project. More
generally, sufficient experience in these areas can help you better understand the issues and the type of data
you need.
Often, regarding major problems of data analysis, it is necessary to have an interdisciplinary team of
experts who can contribute in the best possible way to their respective fields of competence. Regarding
smaller problems, a good analyst must be able to recognize problems that arise during data analysis,
determine which disciplines and skills are necessary to solve these problems, study these disciplines, and
maybe even ask the most knowledgeable people in the sector. In short, the analyst must be able to search not
only for data, but also for information on how to treat that data.

Computer Science
Knowledge of computer science is a basic requirement for any data analyst. In fact, only when you have
good knowledge of and experience in computer science can you efficiently manage the necessary tools for
data analysis. In fact, every step concerning data analysis involves using calculation software (such as IDL,
MATLAB, etc.) and programming languages (such as C ++, Java, and Python).
The large amount of data available today, thanks to information technology, requires specific skills in
order to be managed as efficiently as possible. Indeed, data research and extraction require knowledge of
these various formats. The data are structured and stored in files or database tables with particular formats.
XML, JSON, or simply XLS or CSV files, are now the common formats for storing and collecting data, and
many applications allow you to read and manage the data stored in them. When it comes to extracting data
contained in a database, things are not so immediate, but you need to know the SQL Query language or use
software specially developed for the extraction of data from a given database.
Moreover, for some specific types of data research, the data are not available in an explicit format, but
are present in text files (documents and log files) or web pages, or shown as charts, measures, number of
visitors, or HTML tables. This requires specific technical expertise to parse and eventually extract these data
(called web scraping).

2
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Exploring the Variety of Random
Documents with Different Content
back
back
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookmasss.com

You might also like