Table Of Content®
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
Johansson
Numerical Python
RELATED
Numerical Python by Robert Johansson shows you how to leverage the numerical
and mathematical modules in Python and its Standard Library as well as popular
open source numerical Python packages like NumPy, FiPy, matplotlib and more to
numerically compute solutions and mathematically model applications in a number
of areas like big data, cloud computing, financial engineering, business management
and more.
After reading and using this book, you’ll have some case study examples of
applications that can be found in areas like business management, big data/cloud
computing, financial engineering (i.e., options trading investment alternatives), and
even games.
Up until very recently, Python was mostly regarded as just a web scripting
language. Well, computational scientists and engineers have recently discovered
the flexibility and power of Python to do more. Big data analytics and cloud
computing programmers are seeing Python’s immense use. Financial engineers are
also now employing Python in their work. Python seems to be evolving as a language
that can even rival C++, Fortran, and Pascal/Delphi for numerical and mathematical
computations.
(cid:127) How to plot and graph with matplotlib
(cid:127) How to construct vectors and matrices with NumPy
(cid:127) How to solve linear/non-linear equations with NumPy
(cid:127) How to solve equations with finite element methods using FiPy
(cid:127) How to handle image processing with Pillow, matplotlib and OpenCV
(cid:127) How to do numerical computations, interpolations and optimizations using SciPy
(cid:127) How to handle file I/O, data encoding/compression, and text/date/time processing
Shelve in ISBN 978-1-4842-0554-9
55999
Programming Languages/General
User level:
Intermediate
SOURCE CODE ONLINE 9781484205549
www.apress.com
www.it-ebooks.info
Numerical Python
A Practical Techniques Approach
for Industry
Robert Johansson
www.it-ebooks.info
Numerical Python: A Practical Techniques Approach for Industry
Robert Johansson
Urayasu, Chiba, Japan
ISBN-13 (pbk): 978-1-4842-0554-9 ISBN-13 (electronic): 978-1-4842-0553-2
DOI 10.1007/978-1-4842-0553-2
Library of Congress Control Number: 2015952828
Copyright © 2015 by Robert Johansson
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.
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: Stefan Turalski
Editorial Board: Steve Anglin, Louise Corrigan, 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: Karen Jameson
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 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 rights@apress.com, 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 material referenced by the author in this text is available to readers
at www.apress.com/9781484205549. For additional information about how to locate and download your book’s
source code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the
Supplementary Material section for each chapter.
Printed on acid-free paper
www.it-ebooks.info
To Mika and Erika.
www.it-ebooks.info
Contents at a Glance
About the Author ���������������������������������������������������������������������������������������������������xvii
About the Technical Reviewer ��������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
■ Chapter 1: Introduction to Computing with Python �����������������������������������������������1
■ Chapter 2: Vectors, Matrices, and Multidimensional Arrays �������������������������������25
■ Chapter 3: Symbolic Computing ��������������������������������������������������������������������������63
■ Chapter 4: Plotting and Visualization ������������������������������������������������������������������89
■ Chapter 5: Equation Solving ������������������������������������������������������������������������������125
■ Chapter 6: Optimization �������������������������������������������������������������������������������������147
■ Chapter 7: Interpolation ������������������������������������������������������������������������������������169
■ Chapter 8: Integration ���������������������������������������������������������������������������������������187
■ Chapter 9: Ordinary Differential Equations ��������������������������������������������������������207
■ Chapter 10: Sparse Matrices and Graphs ����������������������������������������������������������235
■ Chapter 11: Partial Differential Equations ���������������������������������������������������������255
■ Chapter 12: Data Processing and Analysis ��������������������������������������������������������285
■ Chapter 13: Statistics ����������������������������������������������������������������������������������������313
■ Chapter 14: Statistical Modeling �����������������������������������������������������������������������333
■ Chapter 15: Machine Learning ��������������������������������������������������������������������������363
■ Chapter 16: Bayesian Statistics�������������������������������������������������������������������������383
v
www.it-ebooks.info
■ Contents at a GlanCe
■ Chapter 17: Signal Processing ��������������������������������������������������������������������������405
■ Chapter 18: Data Input and Output ��������������������������������������������������������������������425
■ Chapter 19: Code Optimization ��������������������������������������������������������������������������453
■ Appendix A: Installation ������������������������������������������������������������������������������������471
Index ���������������������������������������������������������������������������������������������������������������������481
vi
www.it-ebooks.info
Contents
About the Author ���������������������������������������������������������������������������������������������������xvii
About the Technical Reviewer ��������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
■ Chapter 1: Introduction to Computing with Python �����������������������������������������������1
Environments for Computing with Python ������������������������������������������������������������������������4
Python ������������������������������������������������������������������������������������������������������������������������������4
Interpreter �����������������������������������������������������������������������������������������������������������������������������������������������5
IPython Console ����������������������������������������������������������������������������������������������������������������5
Input and Output Caching �����������������������������������������������������������������������������������������������������������������������6
Autocompletion and Object Introspection�����������������������������������������������������������������������������������������������7
Documentation ���������������������������������������������������������������������������������������������������������������������������������������7
Interaction with the System Shell �����������������������������������������������������������������������������������������������������������8
IPython Extensions ���������������������������������������������������������������������������������������������������������������������������������9
The IPython Qt Console �������������������������������������������������������������������������������������������������������������������������13
IPython Notebook �����������������������������������������������������������������������������������������������������������14
Cell Types ����������������������������������������������������������������������������������������������������������������������������������������������16
Editing Cells ������������������������������������������������������������������������������������������������������������������������������������������17
Markdown Cells ������������������������������������������������������������������������������������������������������������������������������������18
nbconvert ����������������������������������������������������������������������������������������������������������������������������������������������19
Spyder: An Integrated Development Environment ����������������������������������������������������������21
Source Code Editor �������������������������������������������������������������������������������������������������������������������������������22
Consoles in Spyder �������������������������������������������������������������������������������������������������������������������������������23
Object Inspector ����������������������������������������������������������������������������������������������������������������������������������23
vii
www.it-ebooks.info
■ Contents
Summary ������������������������������������������������������������������������������������������������������������������������24
Further Reading ��������������������������������������������������������������������������������������������������������������24
References ���������������������������������������������������������������������������������������������������������������������24
■ Chapter 2: Vectors, Matrices, and Multidimensional Arrays �������������������������������25
Importing NumPy ������������������������������������������������������������������������������������������������������������26
The NumPy Array Object �������������������������������������������������������������������������������������������������26
Data Types ��������������������������������������������������������������������������������������������������������������������������������������������27
Order of Array Data in Memory �������������������������������������������������������������������������������������������������������������29
Creating Arrays ���������������������������������������������������������������������������������������������������������������30
Arrays Created from Lists and Other Array-like Objects �����������������������������������������������������������������������31
Arrays Filled with Constant Values �������������������������������������������������������������������������������������������������������32
Arrays Filled with Incremental Sequences �������������������������������������������������������������������������������������������33
Arrays Filled with Logarithmic Sequences �������������������������������������������������������������������������������������������33
Mesh-grid Arrays ����������������������������������������������������������������������������������������������������������������������������������33
Creating Uninitialized Arrays ����������������������������������������������������������������������������������������������������������������34
Creating Arrays with Properties of Other Arrays �����������������������������������������������������������������������������������34
Creating Matrix Arrays ��������������������������������������������������������������������������������������������������������������������������35
Indexing and Slicing �������������������������������������������������������������������������������������������������������35
One-dimensional Arrays �����������������������������������������������������������������������������������������������������������������������35
Multidimensional Arrays �����������������������������������������������������������������������������������������������������������������������37
Views ����������������������������������������������������������������������������������������������������������������������������������������������������38
Fancy Indexing and Boolean-valued Indexing ��������������������������������������������������������������������������������������39
Reshaping and Resizing �������������������������������������������������������������������������������������������������40
Vectorized Expressions ��������������������������������������������������������������������������������������������������44
Arithmetic Operations ���������������������������������������������������������������������������������������������������������������������������46
Elementwise Functions ������������������������������������������������������������������������������������������������������������������������48
Aggregate Functions�����������������������������������������������������������������������������������������������������������������������������50
Boolean Arrays and Conditional Expressions ����������������������������������������������������������������������������������������52
Set Operations ��������������������������������������������������������������������������������������������������������������������������������������55
Operations on Arrays ����������������������������������������������������������������������������������������������������������������������������56
viii
www.it-ebooks.info
■ Contents
Matrix and Vector Operations �����������������������������������������������������������������������������������������57
Summary ������������������������������������������������������������������������������������������������������������������������61
Further Reading ��������������������������������������������������������������������������������������������������������������62
References ���������������������������������������������������������������������������������������������������������������������62
■ Chapter 3: Symbolic Computing ��������������������������������������������������������������������������63
Importing SymPy ������������������������������������������������������������������������������������������������������������63
Symbols ��������������������������������������������������������������������������������������������������������������������������64
Numbers �����������������������������������������������������������������������������������������������������������������������������������������������66
Expressions ��������������������������������������������������������������������������������������������������������������������70
Manipulating Expressions ����������������������������������������������������������������������������������������������72
Simplification ����������������������������������������������������������������������������������������������������������������������������������������72
Expand ��������������������������������������������������������������������������������������������������������������������������������������������������73
Factor, Collect, and Combine ����������������������������������������������������������������������������������������������������������������74
Apart, Together, and Cancel ������������������������������������������������������������������������������������������������������������������75
Substitutions �����������������������������������������������������������������������������������������������������������������������������������������75
Numerical Evaluation �����������������������������������������������������������������������������������������������������76
Calculus ��������������������������������������������������������������������������������������������������������������������������77
Derivatives ��������������������������������������������������������������������������������������������������������������������������������������������77
Integrals ������������������������������������������������������������������������������������������������������������������������������������������������79
Series����������������������������������������������������������������������������������������������������������������������������������������������������80
Limits ����������������������������������������������������������������������������������������������������������������������������������������������������82
Sums and Products ������������������������������������������������������������������������������������������������������������������������������82
Equations������������������������������������������������������������������������������������������������������������������������83
Linear Algebra ����������������������������������������������������������������������������������������������������������������85
Summary ������������������������������������������������������������������������������������������������������������������������88
Further Reading ��������������������������������������������������������������������������������������������������������������88
References ���������������������������������������������������������������������������������������������������������������������88
ix
www.it-ebooks.info
■ Contents
■ Chapter 4: Plotting and Visualization ������������������������������������������������������������������89
Importing Matplotlib �������������������������������������������������������������������������������������������������������90
Getting Started ���������������������������������������������������������������������������������������������������������������90
Interactive and Noninteractive Modes ��������������������������������������������������������������������������������������������������93
Figure �����������������������������������������������������������������������������������������������������������������������������95
Axes ��������������������������������������������������������������������������������������������������������������������������������96
Plot Types ���������������������������������������������������������������������������������������������������������������������������������������������97
Line Properties �������������������������������������������������������������������������������������������������������������������������������������98
Legends ����������������������������������������������������������������������������������������������������������������������������������������������101
Text Formatting and Annotations ��������������������������������������������������������������������������������������������������������102
Axis Properties ������������������������������������������������������������������������������������������������������������������������������������104
Advanced Axes Layouts ������������������������������������������������������������������������������������������������113
Insets ��������������������������������������������������������������������������������������������������������������������������������������������������113
Subplots ����������������������������������������������������������������������������������������������������������������������������������������������114
Subplot2grid ���������������������������������������������������������������������������������������������������������������������������������������116
GridSpec ���������������������������������������������������������������������������������������������������������������������������������������������117
Colormap Plots �������������������������������������������������������������������������������������������������������������118
3D plots ������������������������������������������������������������������������������������������������������������������������120
Summary ����������������������������������������������������������������������������������������������������������������������122
Further Reading ������������������������������������������������������������������������������������������������������������122
References �������������������������������������������������������������������������������������������������������������������123
■ Chapter 5: Equation Solving ������������������������������������������������������������������������������125
Importing Modules �������������������������������������������������������������������������������������������������������126
Linear Equation Systems ����������������������������������������������������������������������������������������������126
Square Systems ����������������������������������������������������������������������������������������������������������������������������������127
Rectangular Systems ��������������������������������������������������������������������������������������������������������������������������131
Eigenvalue Problems ����������������������������������������������������������������������������������������������������134
x
www.it-ebooks.info
Description:Leverage the numerical and mathematical modules in Python and its Standard Library as well as popular open source numerical Python packages like NumPy, SciPy, SymPy, Matplotlib, Pandas, and more to numerically compute solutions and mathematically model applications in a number of areas like big data