Numerical Python,2E, Robert

Chapter 1: ␇Introduction to Computing with Python␈ 1

Environments for Computing with Python␈ 5 Python␈ 6 Interpreter␈ 7 IPython Console␈ 8 Input and Output Caching␈ 9 Autocompletion and Object Introspection␈ 11 Documentation␈ 11 Interaction with the System Shell␈ 12 IPython Extensions␈ 13 Jupyter␈ 19 The Jupyter QtConsole␈ 20 The Jupyter Notebook␈ 21 Jupyter Lab␈ 24 Cell Types␈ 25 Editing Cells␈ 26 Markdown Cells␈ 28 Rich Output Display␈ 30 nbconvert␈ 34 vTable of Contents Spyder: An Integrated Development Environment␈ 37 Source Code Editor␈ 38 Consoles in Spyder␈ 40 Object Inspector␈ 40 Summary␈ 41 Further Reading␈ 41 References␈ 41

Chapter 2: ␇Vectors, Matrices, and Multidimensional Arrays␈ 43

Importing the Modules␈ 44 The NumPy Array Object␈ 44 Data Types␈ 46 Order of Array Data in Memory␈ 49 Creating Arrays␈ 50 Arrays Created from Lists and Other Array-Like Objects␈ 52 Arrays Filled with Constant Values␈ 52 Arrays Filled with Incremental Sequences␈ 54 Arrays Filled with Logarithmic Sequences␈ 54 Meshgrid Arrays␈ 55 Creating Uninitialized Arrays␈ 56 Creating Arrays with Properties of Other Arrays␈ 56 Creating Matrix Arrays␈ 57 Indexing and Slicing␈ 58 One-Dimensional Arrays␈ 58 Multidimensional Arrays␈ 60 Views␈ 62 Fancy Indexing and Boolean-Valued Indexing␈ 63 Reshaping and Resizing␈ 66 Vectorized Expressions␈ 70 Arithmetic Operations␈ 72 Elementwise Functions␈ 76 viTable of Contents Aggregate Functions␈ 79 Boolean Arrays and Conditional Expressions␈ 82 Set Operations␈ 85 Operations on Arrays␈ 87 Matrix and Vector Operations␈ 88 Summary␈ 95 Further Reading␈ 95 References␈ 96

Chapter 3: ␇Symbolic Computing␈ 97

Importing SymPy␈ 98 Symbols␈ 99 Numbers␈ 102 Expressions␈ 109 Manipulating Expressions␈ 110 Simplification␈ 111 Expand␈ 112 Factor, Collect, and Combine␈ 114 Apart, Together, and Cancel␈ 115 Substitutions␈ 115 Numerical Evaluation␈ 117 Calculus␈ 118 Derivatives␈ 119 Integrals␈ 121 Series␈ 123 Limits␈ 125 Sums and Products␈ 126 Equations␈ 127 Linear Algebra␈ 130 viiTable of Contents Summary␈ 134 Further Reading␈ 134 Reference␈ 134

Chapter 4: ␇Plotting and Visualization␈ 135

Importing Modules␈ 136 Getting Started␈ 137 Interactive and Noninteractive Modes␈ 141 Figure␈ 143 Axes␈ 145 Plot Types␈ 146 Line Properties␈ 147 Legends␈ 152 Text Formatting and Annotations␈ 153 Axis Properties␈ 156 Advanced Axes Layouts␈ 168 Insets␈ 168 Subplots␈ 170 Subplot2grid␈ 172 GridSpec␈ 173 Colormap Plots␈ 174 3 ␇D Plots␈ 177 Summary␈ 180 Further Reading␈ 180 References␈ 181

Chapter 5: ␇Equation Solving␈ 183

Importing Modules␈ 184 Linear Equation Systems␈ 185 Square Systems␈ 186 Rectangular Systems␈ 192 viiiTable of Contents Eigenvalue Problems␈ 196 Nonlinear Equations␈ 198 Univariate Equations␈ 199 Systems of Nonlinear Equations␈ 207 Summary␈ 212 Further Reading␈ 212 References␈ 212

Chapter 6: ␇Optimization␈ 213

Importing Modules␈ 214 Classification of Optimization Problems␈ 214 Univariate Optimization␈ 217 Unconstrained Multivariate Optimization␈ 221 Nonlinear Least Square Problems␈ 230 Constrained Optimization␈ 232 Linear Programming␈ 238 Summary␈ 241 Further Reading␈ 241 References␈ 242

Chapter 7: ␇Interpolation␈ 243

Importing Modules␈ 244 Interpolation␈ 244 Polynomials␈ 245 Polynomial Interpolation␈ 249 Spline Interpolation␈ 255 Multivariate Interpolation␈ 258 Summary␈ 265 Further Reading␈ 265 References␈ 265 ixTable of Contents

Chapter 8: ␇Integration␈ 267

Importing Modules␈ 268 Numerical Integration Methods␈ 269 Numerical Integration with SciPy␈ 274 Tabulated Integrand␈ 277 Multiple Integration␈ 280 Symbolic and Arbitrary-Precision Integration␈ 285 Line Integrals␈ 288 Integral Transforms␈ 289 Summary␈ 292 Further Reading␈ 293 References␈ 293

Chapter 9: ␇Ordinary Differential Equations␈ 295

Importing Modules␈ 296 Ordinary Differential Equations␈ 296 Symbolic Solution to ODEs␈ 298 Direction Fields␈ 304 Solving ODEs Using Laplace Transformations␈ 309 Numerical Methods for Solving ODEs␈ 313 Numerical Integration of ODEs Using SciPy␈ 317 Summary␈ 332 Further Reading␈ 333 References␈ 333

Chapter 10: ␇Sparse Matrices and Graphs␈ 335

Importing Modules␈ 336 Sparse Matrices in SciPy␈ 336 Functions for Creating Sparse Matrices␈ 342 Sparse Linear Algebra Functions␈ 345 xTable of Contents Linear Equation Systems␈ 345 Graphs and Networks␈ 352 Summary␈ 360 Further Reading␈ 361 References␈ 361

Chapter 11: ␇Partial Differential Equations␈ 363

Importing Modules␈ 364 Partial Differential Equations␈ 365 Finite-Difference Methods␈ 366 Finite-Element Methods␈ 373 Survey of FEM Libraries␈ 377 Solving PDEs Using FEniCS␈ 378 Summary␈ 403 Further Reading␈ 403 References␈ 404

Chapter 12: ␇Data Processing and Analysis␈ 405

Importing Modules␈ 406 Introduction to Pandas␈ 407 Series␈ 407 DataFrame␈ 410 Time Series␈ 422 The Seaborn Graphics Library␈ 434 Summary␈ 440 Further Reading␈ 440 References␈ 441

Chapter 13: ␇Statistics␈ 443

Importing Modules␈ 444 Review of Statistics and Probability␈ 444 Random Numbers␈ 446 xiTable of Contents Random Variables and Distributions␈ 451 Hypothesis Testing␈ 460 Nonparametric Methods␈ 466 Summary␈ 469 Further Reading␈ 470 References␈ 470

Chapter 14: ␇Statistical Modeling␈ 471

Importing Modules␈ 472 Introduction to Statistical Modeling␈ 473 Defining Statistical Models with Patsy␈ 474 Linear Regression␈ 485 Example Datasets␈ 494 Discrete Regression␈ 496 Logistic Regression␈ 496 Poisson Model␈ 502 Time Series␈ 506 Summary␈ 511 Further Reading␈ 511 References␈ 511

Chapter 15: ␇Machine Learning␈ 513

Importing Modules␈ 514 Brief Review of Machine Learning␈ 515 Regression␈ 518 Classification␈ 529 Clustering␈ 535 Summary␈ 540 Further Reading␈ 540 References␈ 541 xiiTable of Contents

Chapter 16: ␇Bayesian Statistics␈ 543

Importing Modules␈ 544 Introduction to Bayesian Statistics␈ 545 Model Definition␈ 548 Sampling Posterior Distributions␈ 553 Linear Regression␈ 558 Summary␈ 571 Further Reading␈ 572 References␈ 572 Chapter 17: ␇Signal Processing␈ 573 Importing Modules␈ 574 Spectral Analysis␈ 574 Fourier Transforms␈ 575 Windowing␈ 581 Spectrogram␈ 585 Signal Filters␈ 590 Convolution Filters␈ 590 FIR and IIR Filters␈ 593 Summary␈ 598 Further Reading␈ 599 References␈ 599

Chapter 18: ␇Data Input and Output␈ 601

Importing Modules␈ 602 Comma-Separated Values␈ 603 HDF5␈ 608 h5py␈ 610 PyTables␈ 623 Pandas HDFStore␈ 629 xiiiTable of Contents JSON␈ 631 Serialization␈ 636 Summary␈ 639 Further Reading␈ 639 Reference␈ 640

Chapter 19: ␇Code Optimization␈ 641

Importing Modules␈ 644 Numba␈ 644 Cython␈ 652 Summary␈ 664 Further Reading␈ 665 References␈ 665 ␇Appendix: Installation␈ 667 ␇Miniconda and Conda␈ 668 ␇A Complete Environment␈ 676 ␇Summary␈ 680 ␇Further Reading␈ 680 Index␈ 683