logo资料库

Scipy Tutorial(Scipy教程).pdf

第1页 / 共657页
第2页 / 共657页
第3页 / 共657页
第4页 / 共657页
第5页 / 共657页
第6页 / 共657页
第7页 / 共657页
第8页 / 共657页
资料共657页,剩余部分请下载后查看
I Getting started with Python for science
Python scientific computing ecosystem
Why Python?
The Scientific Python ecosystem
Before starting: Installing a working environment
The workflow: interactive environments and text editors
The Python language
First steps
Basic types
Control Flow
Defining functions
Reusing code: scripts and modules
Input and Output
Standard Library
Exception handling in Python
Object-oriented programming (OOP)
NumPy: creating and manipulating numerical data
The NumPy array object
Numerical operations on arrays
More elaborate arrays
Advanced operations
Some exercises
Full code examples
Matplotlib: plotting
Introduction
Simple plot
Figures, Subplots, Axes and Ticks
Other Types of Plots: examples and exercises
Beyond this tutorial
Quick references
Full code examples
Scipy : high-level scientific computing
File input/output: scipy.io
Special functions: scipy.special
Linear algebra operations: scipy.linalg
Interpolation: scipy.interpolate
Optimization and fit: scipy.optimize
Statistics and random numbers: scipy.stats
Numerical integration: scipy.integrate
Fast Fourier transforms: scipy.fftpack
Signal processing: scipy.signal
Image manipulation: scipy.ndimage
Summary exercises on scientific computing
Full code examples for the scipy chapter
Getting help and finding documentation
II Advanced topics
Advanced Python Constructs
Iterators, generator expressions and generators
Decorators
Context managers
Advanced NumPy
Life of ndarray
Universal functions
Interoperability features
Array siblings: chararray, maskedarray, matrix
Summary
Contributing to NumPy/Scipy
Debugging code
Avoiding bugs
Debugging workflow
Using the Python debugger
Debugging segmentation faults using gdb
Optimizing code
Optimization workflow
Profiling Python code
Making code go faster
Writing faster numerical code
Sparse Matrices in SciPy
Introduction
Storage Schemes
Linear System Solvers
Other Interesting Packages
Image manipulation and processing using Numpy and Scipy
Opening and writing to image files
Displaying images
Basic manipulations
Image filtering
Feature extraction
Measuring objects properties: ndimage.measurements
Full code examples
Examples for the image processing chapter
Mathematical optimization: finding minima of functions
Knowing your problem
A review of the different optimizers
Full code examples
Examples for the mathematical optimization chapter
Practical guide to optimization with scipy
Special case: non-linear least-squares
Optimization with constraints
Full code examples
Examples for the mathematical optimization chapter
Interfacing with C
Introduction
Python-C-Api
Ctypes
SWIG
Cython
Summary
Further Reading and References
Exercises
III Packages and applications
Statistics in Python
Data representation and interaction
Hypothesis testing: comparing two groups
Linear models, multiple factors, and analysis of variance
More visualization: seaborn for statistical exploration
Testing for interactions
Full code for the figures
Solutions to this chapter’s exercises
Sympy : Symbolic Mathematics in Python
First Steps with SymPy
Algebraic manipulations
Calculus
Equation solving
Linear Algebra
Scikit-image: image processing
Introduction and concepts
Input/output, data types and colorspaces
Image preprocessing / enhancement
Image segmentation
Measuring regions’ properties
Data visualization and interaction
Feature extraction for computer vision
Full code examples
Examples for the scikit-image chapter
Traits: building interactive dialogs
Introduction
Example
What are Traits
3D plotting with Mayavi
Mlab: the scripting interface
Interactive work
Slicing and dicing data: sources, modules and filters
Animating the data
Making interactive dialogs
Putting it together
scikit-learn: machine learning in Python
Introduction: problem settings
Basic principles of machine learning with scikit-learn
Supervised Learning: Classification of Handwritten Digits
Supervised Learning: Regression of Housing Data
Measuring prediction performance
Unsupervised Learning: Dimensionality Reduction and Visualization
The eigenfaces example: chaining PCA and SVMs
Parameter selection, Validation, and Testing
Examples for the scikit-learn chapter
Index
PythonMatplotlibSciKitsNumpySciPyIPythonIP[y]:Cython2017EDITIONEdited byGaël VaroquauxEmmanuelle GouillartOlaf VahtrasScipyLecture Noteswww.scipy-lectures.orgGaël Varoquaux • Emmanuelle Gouillart • Olav VahtrasChristopher Burns • Adrian Chauve • Robert Cimrman • Christophe Combelles Pierre de Buyl • Ralf Gommers • André Espaze • Zbigniew Jędrzejewski-Szmek Valentin Haenel • Gert-Ludwig Ingold • Fabian Pedregosa • Didrik Pinte Nicolas P. Rougier • Pauli Virtanen and many others...
Contents I Getting started with Python for science 1 Python scientific computing ecosystem 1.1 Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 The Scientific Python ecosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 Before starting: Installing a working environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4 The workflow: interactive environments and text editors . . . . . . . . . . . . . . . . . . . . . . . . 2 4 4 6 8 8 2 The Python language 12 First steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.2 Basic types 2.3 Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.4 Defining functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.5 Reusing code: scripts and modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.6 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 2.7 Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.8 Exception handling in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.9 Object-oriented programming (OOP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3 NumPy: creating and manipulating numerical data 47 3.1 The NumPy array object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 3.2 Numerical operations on arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.3 More elaborate arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 3.4 Advanced operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Some exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 3.5 3.6 Full code examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 4 Matplotlib: plotting 97 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 4.1 Simple plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 4.2 4.3 Figures, Subplots, Axes and Ticks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 4.4 Other Types of Plots: examples and exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 4.5 Beyond this tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 4.6 Quick references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 4.7 Full code examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 5 Scipy : high-level scientific computing 184 File input/output: scipy.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Special functions: scipy.special . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Linear algebra operations: scipy.linalg . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 5.1 5.2 5.3 i
Interpolation: scipy.interpolate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 5.4 5.5 Optimization and fit: scipy.optimize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Statistics and random numbers: scipy.stats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 5.6 5.7 Numerical integration: scipy.integrate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Fast Fourier transforms: scipy.fftpack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 5.8 Signal processing: scipy.signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 5.9 5.10 Image manipulation: scipy.ndimage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 5.11 Summary exercises on scientific computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 5.12 Full code examples for the scipy chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 6 Getting help and finding documentation II Advanced topics 259 262 7 Advanced Python Constructs 264 Iterators, generator expressions and generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 7.1 7.2 Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 7.3 Context managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 8 Advanced NumPy 281 8.1 Life of ndarray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 8.2 Universal functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 8.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 8.4 Array siblings: chararray, maskedarray, matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 8.6 Contributing to NumPy/Scipy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Interoperability features 9 Debugging code 314 9.1 Avoiding bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 9.2 Debugging workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 9.3 Using the Python debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 9.4 Debugging segmentation faults using gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 10 Optimizing code 325 10.1 Optimization workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 10.2 Profiling Python code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 10.3 Making code go faster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 10.4 Writing faster numerical code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 11 Sparse Matrices in SciPy 332 11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 11.2 Storage Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 11.3 Linear System Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 11.4 Other Interesting Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 12 Image manipulation and processing using Numpy and Scipy 352 12.1 Opening and writing to image files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 12.2 Displaying images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 12.3 Basic manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 12.4 Image filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 12.5 Feature extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 12.6 Measuring objects properties: ndimage.measurements . . . . . . . . . . . . . . . . . . . . . . . . 366 12.7 Full code examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 12.8 Examples for the image processing chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 13 Mathematical optimization: finding minima of functions 397 13.1 Knowing your problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 13.2 A review of the different optimizers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 13.3 Full code examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 13.4 Examples for the mathematical optimization chapter . . . . . . . . . . . . . . . . . . . . . . . . . . 406 ii
13.5 Practical guide to optimization with scipy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 13.6 Special case: non-linear least-squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 13.7 Optimization with constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 13.8 Full code examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438 13.9 Examples for the mathematical optimization chapter . . . . . . . . . . . . . . . . . . . . . . . . . . 438 14 Interfacing with C 439 14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439 14.2 Python-C-Api . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440 14.3 Ctypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 14.4 SWIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 14.5 Cython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 14.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 14.7 Further Reading and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 14.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 458 III Packages and applications 460 15 Statistics in Python 462 15.1 Data representation and interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 15.2 Hypothesis testing: comparing two groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 15.3 Linear models, multiple factors, and analysis of variance . . . . . . . . . . . . . . . . . . . . . . . . 471 15.4 More visualization: seaborn for statistical exploration . . . . . . . . . . . . . . . . . . . . . . . . . . 476 15.5 Testing for interactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 15.6 Full code for the figures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 15.7 Solutions to this chapter’s exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502 16 Sympy : Symbolic Mathematics in Python 505 16.1 First Steps with SymPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 16.2 Algebraic manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507 16.3 Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 16.4 Equation solving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510 16.5 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 17 Scikit-image: image processing 513 17.1 Introduction and concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514 17.2 Input/output, data types and colorspaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 17.3 Image preprocessing / enhancement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 17.4 Image segmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 17.5 Measuring regions’ properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 17.6 Data visualization and interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 17.7 Feature extraction for computer vision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525 17.8 Full code examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526 17.9 Examples for the scikit-image chapter 18 Traits: building interactive dialogs 538 18.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 18.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540 18.3 What are Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540 19 3D plotting with Mayavi 557 19.1 Mlab: the scripting interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558 19.2 Interactive work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564 19.3 Slicing and dicing data: sources, modules and filters . . . . . . . . . . . . . . . . . . . . . . . . . . . 565 19.4 Animating the data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 19.5 Making interactive dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569 19.6 Putting it together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571 20 scikit-learn: machine learning in Python 572 iii
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 20.1 Introduction: problem settings 20.2 Basic principles of machine learning with scikit-learn . . . . . . . . . . . . . . . . . . . . . . . . . . 577 20.3 Supervised Learning: Classification of Handwritten Digits . . . . . . . . . . . . . . . . . . . . . . . 582 20.4 Supervised Learning: Regression of Housing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 20.5 Measuring prediction performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 20.6 Unsupervised Learning: Dimensionality Reduction and Visualization . . . . . . . . . . . . . . . . 594 20.7 The eigenfaces example: chaining PCA and SVMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597 20.8 Parameter selection, Validation, and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603 20.9 Examples for the scikit-learn chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610 Index 652 iv
Scipy lecture notes, Edition 2017.1 Contents 1
Part I Getting started with Python for science 2
This part of the Scipy lecture notes is a self-contained introduction to everything that is needed to use Python for science, from the language itself, to numerical computing or plotting. Scipy lecture notes, Edition 2017.1 3
分享到:
收藏