Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib, Third Edition
Robert Johansson
Table of Contents
Chapter 1: Introduction to Computing with Python ……………………………………….. 1
Environments for Computing with Python ………. 4
Python …………….. 4
Interpreter …………………………….4
IPython Console … 5
Input and Output Caching ……….6
Autocompletion and Object Introspection ………………………….. 7
Documentation ……………………..7
Interaction with the System Shell ……………8
IPython Extensions ………………..8
Jupyter ………….. 12
The Jupyter QtConsole …………13
The Jupyter Notebook ………….14
Jupyter Lab …………………………16
Cell Types ……………………………17
Editing Cells ………………………..18
Markdown Cells …………………..19
Rich Output Display ……………..20
nbconvert ……………………………24
Spyder: An Integrated Development Environment ……………………….25
Source Code Editor ……………….27
Consoles in Spyder ……………….27
Object Inspector ………………….28
Summary ………..28
Further Reading ………………28
Chapter 2: Vectors, Matrices, and Multidimensional Arrays ………………………….29
Importing the Modules …….30
The NumPy Array Object …..30
Data Types ………………………….31
Order of Array Data in Memory ……………..33
Creating Arrays ……………….34
Arrays Created from Lists and Other Array-Like Objects ……..35
Arrays Filled with Constant Values ………..36
Arrays Filled with Incremental Sequences ………………………..37
Arrays Filled with Logarithmic Sequences ………………………..37
Meshgrid Arrays ………………….37
Creating Uninitialized Arrays …38
Creating Arrays with Properties of Other Arrays …………………38
Creating Matrix Arrays ………….39
Indexing and Slicing ………..40
One-Dimensional Arrays ……….40
Multidimensional Arrays ……….41
Views …………………………………42
Fancy Indexing and Boolean-Valued Indexing ……………………43
Reshaping and Resizing …..45
Vectorized Expressions ……48
Arithmetic Operations …………..50
Elementwise Functions ………..52
Aggregate Functions ……………55
Boolean Arrays and Conditional Expressions ……………………..57
Set Operations …………………….59
Operations on Arrays ……………60
Matrix and Vector Operations ………………………61
Summary ………..65
Further Reading ………………66
Chapter 3: Symbolic Computing …………67
Importing SymPy …………….67
Symbols ………….68
Numbers …………………………….70
Expressions …….75
Manipulating Expressions …………………………..76
Simplification ………………………76
Expand ……………………………….77
Factor, Collect, and Combine …78
Apart, Together, and Cancel …..79
Substitutions ……………………….79
Numerical Evaluation ………80
Calculus ………….81
Derivatives ………………………….82
Integrals ……………………………..83
Series ………………………………..85
Limits …………………………………86
Sums and Products ……………..87
Equations ……….88
Linear Algebra …90
Summary ………..93
Further Reading ………………93
Chapter 4: Plotting and Visualization ….95
Importing Modules ………….96
Getting Started ……………….96
Interactive and Noninteractive Modes ……99
Figure …………..101
Axes ……………..102
Plot Types …………………………103
Line Properties ………………….104
Legends ……………………………108
Text Formatting and Annotations …………109
Axis Properties …………………..110
Advanced Axes Layouts ….119
Insets ……………………………….119
Subplots ……………………………120
Subplot2grid ……………………..122
GridSpec …………………………..123
Colormap Plots ……………..124
3 D Plots ………..126
Summary ………128
Further Reading …………….128
Chapter 5: Equation Solving …………….129
Importing Modules ………..130
Linear Equation Systems …………………………..130
Square Systems …………………131
Rectangular Systems ………….135
Eigenvalue Problems ……..138
Nonlinear Equations ………139
Univariate Equations …………..140
Systems of Nonlinear Equations ………….145
Summary ………149
Further Reading …………….149
Chapter 6: Optimization …………………..151
Importing Modules ………..151
Classification of Optimization Problems ………152
Univariate Optimization ….154
Unconstrained Multivariate Optimization …….156
Nonlinear Least Square Problems ………………162
Constrained Optimization ………………………….164
Linear Programming …………..168
Summary ………170
Further Reading …………….170
Chapter 7: Interpolation ………………….171
Importing Modules ………..171
Interpolation ….172
Polynomials …..173
Polynomial Interpolation …175
Spline Interpolation ……….179
Multivariate Interpolation ………………………….181
Summary ………187
Further Reading …………….187
Chapter 8: Integration …………………….189
Importing Modules ………..190
Numerical Integration Methods ………………….190
Numerical Integration with SciPy ……………….194
Tabulated Integrand ……………196
Multiple Integration ……….198
Symbolic and Arbitrary-Precision Integration ……………………………………………………..202
Line Integrals …………………….204
Integral Transforms ……….204
Summary ………207
Further Reading …………….207
Chapter 9: Ordinary Differential Equations ………………………………………………..209
Importing Modules ………..209
Ordinary Differential Equations ………………….210
Symbolic Solution to ODEs ………………………..211
Direction Fields ………………….216
Solving ODEs Using Laplace Transformations ………………….219
Numerical Methods for Solving ODEs ………….222
Numerical Integration of ODEs Using SciPy …225
Summary ………236
Further Reading …………….236
Chapter 10: Sparse Matrices and Graphs ………………………………………………….237
Importing Modules ………..237
Sparse Matrices in SciPy ………………………….238
Functions for Creating Sparse Matrices ………………………….241
Sparse Linear Algebra Functions …………244
Linear Equation Systems …….244
Graphs and Networks …………249
Summary ………255
Further Reading …………….256
Chapter 11: Partial Differential Equations …………………………………………………257
Importing Modules ………..258
Partial Differential Equations ……………………..258
Finite-Difference Methods …………………………259
Finite-Element Methods …264
Survey of FEM Libraries ……..266
Solving PDEs Using FEniCS ……………………….267
Summary ………285
Further Reading …………….285
Chapter 12: Data Processing and Analysis ………………………………………………..287
Importing Modules ………..288
Introduction to Pandas …..288
Series ………………………………288
DataFrame ………………………..290
Time Series ……………………….298
The Seaborn Graphics Library ……………………307
Summary ………312
Further Reading …………….312
Chapter 13: Statistics ……………………..315
Importing Modules ………..315
Review of Statistics and Probability ……………316
Random Numbers ………….317
Random Variables and Distributions …………..320
Hypothesis Testing ………..327
Nonparametric Methods …331
Summary ………333
Further Reading …………….334
Chapter 14: Statistical Modeling ………335
Importing Modules ………..336
Introduction to Statistical Modeling ……………336
Defining Statistical Models with Patsy ………..337
Linear Regression …………345
Example Datasets ……………….351
Discrete Regression ………352
Logistic Regression ……………353
Poisson Model …………………..357
Time Series …..360
Summary ………363
Further Reading …………….364
Chapter 15: Machine Learning …………365
Importing Modules ………..366
Brief Review of Machine Learning ……………..366
Regression ……368
Classification …376
Clustering ……..380
Summary ………384
Further Reading …………….384
Chapter 16: Bayesian Statistics ……….385
Importing Modules ………..386
Introduction to Bayesian Statistics ……………..386
Model Definition ……………388
Sampling Posterior Distributions …………393
Linear Regression ……………….396
Summary ………407
Further Reading …………….407
Chapter 17: Signal Processing …………409
Importing Modules ………..409
Spectral Analysis …………..410
Fourier Transforms …………….410
Windowing ………………………..415
Spectrogram ……………………..418
Signal Filters …421
Convolution Filters ……………..422
FIR and IIR Filters ……………….424
Summary ………428
Further Reading …………….428
Chapter 18: Data Input and Output ……429
Importing Modules ………..430
Comma-Separated Values …………………………430
HDF5 …………….434
h5py …………………………………435
PyTables …………………………..444
Pandas HDFStore ……………….447
Parquet …………449
JSON ……………451
Serialization ….454
Summary ………456
Further Reading …………….456
Chapter 19: Code Optimization …………459
Importing Modules ………..461
Numba ………….461
Cython ………….467
Summary ………475
Further Reading …………….476