• No results found

Data Analytics

N/A
N/A
Protected

Academic year: 2021

Share "Data Analytics"

Copied!
350
0
0

Loading.... (view fulltext now)

Full text

(1)

Nelli

Shelve in: Programming Languages/General User level: Intermediate

SOURCE CODE ONLINE

RELATED

BOOKS FOR PROFESSIONALS BY PROFESSIONALS

®

Python Data Analytics

Python Data Analytics will help you tackle the world of data acquisition and analysis

using the power of the Python language. At the heart of this book lies the coverage of pandas, an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

Author Fabio Nelli expertly shows the strength of the Python programming language when applied to processing, managing and retrieving information. Inside, you will see how intuitive and flexible it is to discover and communicate meaningful patterns of data using Python scripts, reporting systems, and data export. This book examines how to go about obtaining, processing, storing, managing and analyzing data using the Python programming language.

You will use Python and other open source tools to wrangle data and tease out interesting and important trends in that data that will allow you to predict future patterns. Whether you are dealing with sales data, investment data (stocks, bonds, etc.), medical data, web page usage, or any other type of data set, Python can be used to interpret, analyze, and glean information from a pile of numbers and statistics.

This book is an invaluable reference with its examples of storing and accessing data in a database; it walks you through the process of report generation; it provides three real world case studies or examples that you can take with you for your everyday analysis needs.

• How to set up your environment and use automation scripts • Where and how to get your data

• How to analyze your data with Python’s data analysis library, called pandas • How to carry out visualization with matplotlib

• How to analyze text data using Python’s natural language processing

9 781484 209592 5 4 9 9 9 ISBN 978-1-4842-0959-2

(2)

Python Data Analytics

Data Analysis and Science Using

Pandas, matplotlib, and the

Python Programming Language

(3)

Python Data Analytics

Copyright © 2015 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. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

ISBN-13 (pbk): 978-1-4842-0959-2 ISBN-13 (electronic): 978-1-4842-0958-5

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: Welmoed Spahr Lead Editor: Steve Anglin

Technical Reviewer: Shubham Singh Tomar

Editorial Board: Steve Anglin, Louise Corrigan, Morgan Ertel, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Steve Weiss

Coordinating Editor: Mark Powers Copy Editor: Brendan Frost Compositor: SPi Global Indexer: SPi Global Artist: SPi Global

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 [email protected], 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], or visit www.apress.com.

Apress and friends of ED books 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 Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.

Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com/9781484209592. For detailed information about how to locate your book’s source

(4)

Contents at a Glance

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

xvii

About the Technical Reviewer ��������������������������������������������������������������������������������

xix

Acknowledgments ��������������������������������������������������������������������������������������������������

xxi

Chapter 1: An Introduction to Data Analysis ���������������������������������������������������������

1

Chapter 2: Introduction to the Python’s World ����������������������������������������������������

13

Chapter 3: The NumPy Library �����������������������������������������������������������������������������

35

Chapter 4: The pandas Library—An Introduction ������������������������������������������������

63

Chapter 5: pandas: Reading and Writing Data ���������������������������������������������������

103

Chapter 6: pandas in Depth: Data Manipulation ������������������������������������������������

131

Chapter 7: Data Visualization with matplotlib ���������������������������������������������������

167

Chapter 8: Machine Learning with scikit-learn �������������������������������������������������

237

Chapter 9: An Example—Meteorological Data ��������������������������������������������������

265

Chapter 10: Embedding the JavaScript D3 Library in IPython Notebook ���������

289

Chapter 11: Recognizing Handwritten Digits�����������������������������������������������������

311

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

317

Appendix B: Open Data Sources ������������������������������������������������������������������������

327

Index ���������������������������������������������������������������������������������������������������������������������

331

(5)

Contents

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

xvii

About the Technical Reviewer ��������������������������������������������������������������������������������

xix

Acknowledgments ��������������������������������������������������������������������������������������������������

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 �����������������������������������������������������������������������������������������������

5

Problem Definition ����������������������������������������������������������������������������������������������������������������������������������

5

Data Extraction ���������������������������������������������������������������������������������������������������������������������������������������

6

Data Preparation �������������������������������������������������������������������������������������������������������������������������������������

7

Data Exploration/Visualization ����������������������������������������������������������������������������������������������������������������

7

Predictive Modeling ��������������������������������������������������������������������������������������������������������������������������������

8

Model Validation �������������������������������������������������������������������������������������������������������������������������������������

8

Deployment���������������������������������������������������������������������������������������������������������������������������������������������

8

(6)

■ Contents

Open Data ����������������������������������������������������������������������������������������������������������������������

10

Python and Data Analysis �����������������������������������������������������������������������������������������������

11

Conclusions ��������������������������������������������������������������������������������������������������������������������

12

Chapter 2: Introduction to the Python’s World ����������������������������������������������������

13

Python—The Programming Language ���������������������������������������������������������������������������

13

Python—The Interpreter ������������������������������������������������������������������������������������������������

14

Cython���������������������������������������������������������������������������������������������������������������������������������������������������

15

Jython ���������������������������������������������������������������������������������������������������������������������������������������������������

15

PyPy ������������������������������������������������������������������������������������������������������������������������������������������������������

15

Python 2 and Python 3 ���������������������������������������������������������������������������������������������������

15

Installing Python �������������������������������������������������������������������������������������������������������������

16

Python Distributions �������������������������������������������������������������������������������������������������������

16

Anaconda ����������������������������������������������������������������������������������������������������������������������������������������������

16

Enthought Canopy ��������������������������������������������������������������������������������������������������������������������������������

17

Python(x,y) ��������������������������������������������������������������������������������������������������������������������������������������������

18

Using Python ������������������������������������������������������������������������������������������������������������������

18

Python Shell ������������������������������������������������������������������������������������������������������������������������������������������

18

Run an Entire Program Code ����������������������������������������������������������������������������������������������������������������

18

Implement the Code Using an IDE ��������������������������������������������������������������������������������������������������������

19

Interact with Python �����������������������������������������������������������������������������������������������������������������������������

19

Writing Python Code �������������������������������������������������������������������������������������������������������

19

Make Calculations ��������������������������������������������������������������������������������������������������������������������������������

20

Import New Libraries and Functions ����������������������������������������������������������������������������������������������������

20

Functional Programming (Only for Python 3�4) �������������������������������������������������������������������������������������

22

Indentation ��������������������������������������������������������������������������������������������������������������������������������������������

24

IPython ���������������������������������������������������������������������������������������������������������������������������

24

IPython Shell �����������������������������������������������������������������������������������������������������������������������������������������

24

(7)

■ Contents

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

28

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

28

IDLE (Integrated DeveLopment Environment) ���������������������������������������������������������������������������������������

29

Spyder ��������������������������������������������������������������������������������������������������������������������������������������������������

29

Eclipse (pyDev) �������������������������������������������������������������������������������������������������������������������������������������

30

Sublime�������������������������������������������������������������������������������������������������������������������������������������������������

30

Liclipse �������������������������������������������������������������������������������������������������������������������������������������������������

31

NinjaIDE ������������������������������������������������������������������������������������������������������������������������������������������������

32

Komodo IDE ������������������������������������������������������������������������������������������������������������������������������������������

32

SciPy ������������������������������������������������������������������������������������������������������������������������������

32

NumPy ��������������������������������������������������������������������������������������������������������������������������������������������������

33

Pandas ��������������������������������������������������������������������������������������������������������������������������������������������������

33

matplotlib ���������������������������������������������������������������������������������������������������������������������������������������������

34

Conclusions ��������������������������������������������������������������������������������������������������������������������

34

Chapter 3: The NumPy Library �����������������������������������������������������������������������������

35

NumPy: A Little History ���������������������������������������������������������������������������������������������������

35

The NumPy Installation ��������������������������������������������������������������������������������������������������

35

Ndarray: The Heart of the Library �����������������������������������������������������������������������������������

36

Create an Array �������������������������������������������������������������������������������������������������������������������������������������

37

Types of Data ����������������������������������������������������������������������������������������������������������������������������������������

38

The dtype Option ����������������������������������������������������������������������������������������������������������������������������������

39

Intrinsic Creation of an Array ����������������������������������������������������������������������������������������������������������������

39

Basic Operations ������������������������������������������������������������������������������������������������������������

40

Arithmetic Operators ����������������������������������������������������������������������������������������������������������������������������

41

The Matrix Product �������������������������������������������������������������������������������������������������������������������������������

42

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

43

Universal Functions (ufunc) ������������������������������������������������������������������������������������������������������������������

44

(8)

■ Contents

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

45

Indexing ������������������������������������������������������������������������������������������������������������������������������������������������

45

Slicing ���������������������������������������������������������������������������������������������������������������������������������������������������

46

Iterating an Array ����������������������������������������������������������������������������������������������������������������������������������

48

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

50

Shape Manipulation �������������������������������������������������������������������������������������������������������

50

Array Manipulation ���������������������������������������������������������������������������������������������������������

51

Joining Arrays ���������������������������������������������������������������������������������������������������������������������������������������

51

Splitting Arrays �������������������������������������������������������������������������������������������������������������������������������������

52

General Concepts �����������������������������������������������������������������������������������������������������������

54

Copies or Views of Objects �������������������������������������������������������������������������������������������������������������������

54

Vectorization �����������������������������������������������������������������������������������������������������������������������������������������

55

Broadcasting ����������������������������������������������������������������������������������������������������������������������������������������

55

Structured Arrays �����������������������������������������������������������������������������������������������������������

58

Reading and Writing Array Data on Files ������������������������������������������������������������������������

59

Loading and Saving Data in Binary Files ����������������������������������������������������������������������������������������������

59

Reading File with Tabular Data �������������������������������������������������������������������������������������������������������������

60

Conclusions ��������������������������������������������������������������������������������������������������������������������

61

Chapter 4: The pandas Library—An Introduction ������������������������������������������������

63

pandas: The Python Data Analysis Library ���������������������������������������������������������������������

63

Installation ����������������������������������������������������������������������������������������������������������������������

64

Installation from Anaconda �������������������������������������������������������������������������������������������������������������������

64

Installation from PyPI ����������������������������������������������������������������������������������������������������������������������������

65

Installation on Linux �����������������������������������������������������������������������������������������������������������������������������

65

Installation from Source �����������������������������������������������������������������������������������������������������������������������

66

A Module Repository for Windows ��������������������������������������������������������������������������������������������������������

66

Test Your pandas Installation ������������������������������������������������������������������������������������������

66

(9)

■ Contents

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

67

The Series ���������������������������������������������������������������������������������������������������������������������������������������������

68

The DataFrame �������������������������������������������������������������������������������������������������������������������������������������

75

The Index Objects ���������������������������������������������������������������������������������������������������������������������������������

81

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

83

Reindexing ��������������������������������������������������������������������������������������������������������������������������������������������

83

Dropping �����������������������������������������������������������������������������������������������������������������������������������������������

85

Arithmetic and Data Alignment �������������������������������������������������������������������������������������������������������������

86

Operations between Data Structures �����������������������������������������������������������������������������

87

Flexible Arithmetic Methods �����������������������������������������������������������������������������������������������������������������

88

Operations between DataFrame and Series �����������������������������������������������������������������������������������������

88

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

89

Functions by Element ���������������������������������������������������������������������������������������������������������������������������

89

Functions by Row or Column ����������������������������������������������������������������������������������������������������������������

90

Statistics Functions ������������������������������������������������������������������������������������������������������������������������������

91

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

91

Correlation and Covariance ��������������������������������������������������������������������������������������������

94

“Not a Number” Data �����������������������������������������������������������������������������������������������������

95

Assigning a NaN Value ��������������������������������������������������������������������������������������������������������������������������

96

Filtering Out NaN Values �����������������������������������������������������������������������������������������������������������������������

96

Filling in NaN Occurrences �������������������������������������������������������������������������������������������������������������������

97

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

97

Reordering and Sorting Levels �����������������������������������������������������������������������������������������������������������

100

Summary Statistic by Level ����������������������������������������������������������������������������������������������������������������

100

(10)

■ Contents

Chapter 5: pandas: Reading and Writing Data ���������������������������������������������������

103

I/O API Tools ������������������������������������������������������������������������������������������������������������������

103

CSV and Textual Files ���������������������������������������������������������������������������������������������������

104

Reading Data in CSV or Text Files ���������������������������������������������������������������������������������

104

Using RegExp for Parsing TXT Files ����������������������������������������������������������������������������������������������������

106

Reading TXT Files into Parts or Partially ���������������������������������������������������������������������������������������������

108

Writing Data in CSV�����������������������������������������������������������������������������������������������������������������������������

109

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

111

Writing Data in HTML ��������������������������������������������������������������������������������������������������������������������������

111

Reading Data from an HTML File ��������������������������������������������������������������������������������������������������������

113

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

114

Reading and Writing Data on Microsoft Excel Files ������������������������������������������������������

116

JSON Data ��������������������������������������������������������������������������������������������������������������������

118

The Format HDF5 ���������������������������������������������������������������������������������������������������������

121

Pickle—Python Object Serialization �����������������������������������������������������������������������������

122

Serialize a Python Object with cPickle �����������������������������������������������������������������������������������������������

122

Pickling with pandas �������������������������������������������������������������������������������������������������������������������������

� 123

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

124

Loading and Writing Data with SQLite3 ����������������������������������������������������������������������������������������������

124

Loading and Writing Data with PostgreSQL ����������������������������������������������������������������������������������������

126

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

128

Conclusions ������������������������������������������������������������������������������������������������������������������

130

Chapter 6: pandas in Depth: Data Manipulation ������������������������������������������������

131

Data Preparation ����������������������������������������������������������������������������������������������������������

131

Merging ����������������������������������������������������������������������������������������������������������������������������������������������

132

Concatenating ��������������������������������������������������������������������������������������������������������������

136

Combining ������������������������������������������������������������������������������������������������������������������������������������������

139

Pivoting �����������������������������������������������������������������������������������������������������������������������������������������������

140

Removing ��������������������������������������������������������������������������������������������������������������������������������������������

142

References

Related documents

Data from patients with severe mental illness from eight outpatient teams in the Netherlands were used to perform latent class analysis. From the total caseload, 395 patients agreed

[87] demonstrated the use of time-resolved fluorescence measurements to study the enhanced FRET efficiency and increased fluorescent lifetime of immobi- lized quantum dots on a

The variable angle valley gutter system provides secure and watertight installations for both glass and polycarbonate glazing and is able to accommodate a wide range of

● From the Start/Finish at the River House Barn, head south and cross Fig Ave into the River Campground and head EAST and connect with Main Loop trail.. ● The main loop trail will

However, there were no significant differ- ences between intervention and control groups in the changes in scores on body image, self-esteem, or gender equitable norms, and there

In conclusion, for the studied Taiwanese population of diabetic patients undergoing hemodialysis, increased mortality rates are associated with higher average FPG levels at 1 and

PowerScore test takers who analyze the answer choices will realize that they can skip the first three answer choices because all three expressions are squared, meaning that all

Corporate governance includes concerns for information technology governance because without effective information management, those charged with corporate responsibilities would