Python Scientific Lecture Notes

Python Scientific Lecture Notes

Python Scientific lecture notes Release 2013.2 beta (euroscipy 2013) EuroScipy tutorial team Editors: Valentin Haenel, Emmanuelle Gouillart, Gaël Varoquaux http://scipy-lectures.github.com August 22, 2013 (2013.1-163-g0e5f47c) Contents I Getting started with Python for science2 1 Scientific computing with tools and workflow4 1.1 Why Python?.............................................4 1.2 Scientific Python building blocks..................................5 1.3 The interactive workflow: IPython and a text editor.........................6 2 The Python language 10 2.1 First steps............................................... 10 2.2 Basic types.............................................. 11 2.3 Control Flow............................................. 19 2.4 Defining functions.......................................... 22 2.5 Reusing code: scripts and modules................................. 27 2.6 Input and Output........................................... 34 2.7 Standard Library........................................... 35 2.8 Exception handling in Python.................................... 39 2.9 Object-oriented programming (OOP)................................ 42 3 NumPy: creating and manipulating numerical data 43 3.1 The Numpy array object....................................... 43 3.2 Numerical operations on arrays................................... 55 3.3 More elaborate arrays........................................ 67 3.4 Advanced operations......................................... 70 3.5 Some exercises............................................ 75 4 Matplotlib: plotting 80 4.1 Introduction............................................. 81 4.2 Simple plot.............................................. 82 4.3 Figures, Subplots, Axes and Ticks.................................. 88 4.4 Other Types of Plots: examples and exercises............................ 90 4.5 Beyond this tutorial......................................... 97 4.6 Quick references........................................... 99 5 Scipy : high-level scientific computing 102 5.1 File input/output: scipy.io .................................... 103 5.2 Special functions: scipy.special ................................ 103 5.3 Linear algebra operations: scipy.linalg ............................ 104 5.4 Fast Fourier transforms: scipy.fftpack ............................ 105 5.5 Optimization and fit: scipy.optimize ............................. 109 5.6 Statistics and random numbers: scipy.stats .......................... 113 5.7 Interpolation: scipy.interpolate ............................... 115 5.8 Numerical integration: scipy.integrate ............................ 116 5.9 Signal processing: scipy.signal ................................ 118 5.10 Image processing: scipy.ndimage ............................... 119 i 5.11 Summary exercises on scientific computing............................. 124 6 Getting help and finding documentation 137 II Advanced topics 141 7 Advanced Python Constructs 143 7.1 Iterators, generator expressions and generators........................... 144 7.2 Decorators.............................................. 148 7.3 Context managers.......................................... 156 8 Advanced Numpy 159 8.1 Life of ndarray............................................ 160 8.2 Universal functions.......................................... 173 8.3 Interoperability features....................................... 182 8.4 Array siblings: chararray, maskedarray, matrix ...................... 185 8.5 Summary............................................... 188 8.6 Contributing to Numpy/Scipy.................................... 188 9 Debugging code 192 9.1 Avoiding bugs............................................ 192 9.2 Debugging workflow......................................... 195 9.3 Using the Python debugger...................................... 195 9.4 Debugging segmentation faults using gdb.............................. 200 10 Optimizing code 203 10.1 Optimization workflow........................................ 203 10.2 Profiling Python code........................................ 204 10.3 Making code go faster........................................ 207 10.4 Writing faster numerical code.................................... 208 11 Sparse Matrices in SciPy 211 11.1 Introduction............................................. 211 11.2 Storage Schemes........................................... 213 11.3 Linear System Solvers........................................ 224 11.4 Other Interesting Packages...................................... 229 12 Image manipulation and processing using Numpy and Scipy 230 12.1 Opening and writing to image files................................. 231 12.2 Displaying images.......................................... 232 12.3 Basic manipulations......................................... 234 12.4 Image filtering............................................ 236 12.5 Feature extraction.......................................... 240 12.6 Measuring objects properties: ndimage.measurements .................... 243 13 Mathematical optimization: finding minima of functions 247 13.1 Knowing your problem........................................ 248 13.2 A review of the different optimizers................................. 250 13.3 Practical guide to optimization with scipy.............................. 257 13.4 Special case: non-linear least-squares................................ 259 13.5 Optimization with constraints.................................... 261 14 Interfacing with C 263 14.1 Introduction............................................. 263 14.2 Python-C-Api............................................ 264 14.3 Ctypes................................................ 268 14.4 SWIG................................................. 271 14.5 Cython................................................ 276 14.6 Summary............................................... 279 ii 14.7 Further Reading and References................................... 279 14.8 Exercises............................................... 280 III Packages and applications 282 15 Sympy : Symbolic Mathematics in Python 284 15.1 First Steps with SymPy....................................... 285 15.2 Algebraic manipulations....................................... 286 15.3 Calculus............................................... 286 15.4 Equation solving........................................... 288 15.5 Linear Algebra............................................ 289 16 Scikit-image: image processing 291 16.1 Introduction and concepts...................................... 291 16.2 Input/output, data types and colorspaces............................... 293 16.3 Image preprocessing / enhancement................................. 295 16.4 Image segmentation......................................... 298 16.5 Measuring regions’ properties.................................... 301 16.6 Data visualization and interaction.................................. 301 16.7 Feature extraction for computer vision................................ 303 17 Traits: building interactive dialogs 305 17.1 Introduction............................................. 306 17.2 Example............................................... 306 17.3 What are Traits............................................ 307 17.4 References.............................................. 322 18 3D plotting with Mayavi 324 18.1 Mlab: the scripting interface..................................... 324 18.2 Interactive work........................................... 330 18.3 Slicing and dicing data: sources, modules and filters........................ 331 18.4 Animating the data.......................................... 333 18.5 Making interactive dialogs...................................... 334 18.6 Putting it together.......................................... 336 19 scikit-learn: machine learning in Python 337 19.1 Loading an example dataset..................................... 338 19.2 Classification............................................. 339 19.3 Clustering: grouping observations together............................. 342 19.4 Dimension Reduction with Principal Component Analysis..................... 343 19.5 Putting it all together: face recognition............................... 344 19.6 Linear model: from regression to sparsity.............................. 346 19.7 Model selection: choosing estimators and their parameters..................... 347 Index 348 iii Python Scientific lecture notes, Release 2013.2 beta (euroscipy 2013) Contents 1 Part I Getting started with Python for science 2 Python Scientific lecture notes, Release 2013.2 beta (euroscipy 2013) 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. 3 CHAPTER 1 Scientific computing with tools and workflow authors Fernando Perez, Emmanuelle Gouillart, Gaël Varoquaux, Valentin Haenel 1.1 Why Python? 1.1.1 The scientist’s needs • Get data (simulation, experiment control) • Manipulate and process data. • Visualize results... to understand what we are doing! • Communicate results: produce figures for reports or publications, write presentations. 1.1.2 Specifications • Rich collection of already existing bricks corresponding to classical numerical methods or basic actions: we don’t want to re-program the plotting of a curve, a Fourier transform or a fitting algorithm. Don’t reinvent the wheel! • Easy to learn: computer science is neither our job nor our education. We want to

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    352 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us