This page hosts "recipes", or worked examples of commonly-done tasks. Some are introductory in nature, while others are quite advanced (these are at the bottom of the page). We encourage new users to post recipes even for simple tasks that are not yet represented here. Our goal is an easy learning experience for new users. Some of these recipes may be incorporated into tutorials in the future.
NumPy / SciPy
Building Arrays Introduction to numerical arrays.
Indexing Indexing numpy arrays, from simple to complicated.
Interpolation Examples of interpolation (see also Gridding irregularly spaced data).
Data rebinning Examples of rebinning data to produce smaller arrays with and without interpolation.
Linear regression Simple Linear regression example.
Fit statistics Estimates a multi-variate regression model and provides various fit statistics.
Optimization Quick example of fminbound with plot.
Optimization with fit Similar to above with spline fit and chaco plot.
Convex Hull Finds the convex hull around a set of data points
Multiplying multiple arguments Generalizing dot(a,b) to the case of N arguments.
Kalman Filtering Example from the Welch & Bishop Introduction to the Kalman Filter.
Fitting Data Day to day work in the lab: fitting experimental data.
Comm Theory Example of BPSK simulation.
Smoothing a signal Performing smoothing of 1D and 2D signals by convolving them with a window.
A zero phase delay filter Sample code for a null phase delay filter that processes the signal in the forward and backward direction removing the phase delay.
RANSAC algorithm Implementation of the robust estimation method.
Savitzky Golay filtering of data Sample code for Savitzky Golay filtering.
Multithreading Easy multithreading for embarrassingly parallel problems
Spherical Bessel Zeros Finding the zeros of the spherical Bessel functions and its derivative
Radial Basis Functions Using radial basis functions for smoothing/interpolation
Segment axis Devious trick using strides to allow general operations (like convolution) on successive, overlapping pieces of an array
MetaArray Class for storing per-axis meta information with an array (axis names, column names, units, etc.)
Obarray Trick for avoiding object arrays when dealing with arrays of objects.
Recarray Accessing array columns with structured arrays and recarrays.
KDTree Searching multidimensional space using kd-trees.
Particle Filter A simple particle filter algorithm for tracking objects in a video sequence.
Lotka-Volterra Tutorial Solving ordinary differential equations with Scipy
A coupled spring-mass system Another example of solving differential equations.
Large Markov Chains Find the stationary distribution of a large Markov chain; the M/M/1 tandem queue
Watershed algorithm Apply the watershed algorithm in order to split an array into distinct components (e.g. for the segementation of an image into objects).
Advanced topics
Views vs Copies A quick introduction to array views and some caveats on situations where you should expect a data view or a data copy.
Compiling Extensions
Compiling Extensions on Windows A quick tutorial on how to compile extension modules on Windows using MinGW
SciKits
OpenOpt numerical optimization example for NLP (non-linear problem), NSP (non-smooth), QP (quadratic), LP (linear), MILP (mixed-integer LP)
timeseries Includes examples for plotting, reporting, frequency conversion, and more. Some recipies at /TimeSeries/FAQ.
Scientific Scripts
Schrödinger's equation: a 1-d FDTD solver that animates the time evolution of a gaussian wave packet interacting with simple potentials.
Input Output
DataFrames A useful class for storing alphanumerical data, similar to GNU R's data frames.
Data acquisition with PyUniversalLibrary A series of examples using an inexpensive USB data acquisition device from Measurement Computing.
Data acquisition with Ni-DAQmx A simple example of using ctypes and numpy to access data acquisition devices from National Instruments.
input/output Reading and writing a NumPy array from/to an ascii/binary file.
Fortran I/O Reading FORTRAN record-structured binary files (if you don't know what these are, thank your stars and you don't need this).
Reading SPE files Reading SPE binary files produced by CCD cameras (Princeton and like).
Graphics
There are several packages available to produce interactive screen graphics (use the mouse to zoom, orient, and fine-tune) and publication-quality printed plots, in 2D, 3D, and 4D (animations). These packages have releases more frequently than SciPy. Rather than bundling out-of-date packages with SciPy, the plotting packages are released separately. However, their developers work closely with the SciPy developers to ensure compatibility.
Matplotlib cookbook. Matplotlib is the preferred package for 2D graphics.
3D Plotting with Matplotlib. Simple 3D plots using matplotlib and its now-included 3D capabilities.
Plotting with xplt. xplt is very fast but less flexible than matplotlib. It allows simple 3-d surface visualizations as well. It is based on pygist (included) and is available under the sandbox directory in SVN scipy.
MayaVi/TVTK cookbook. 3D plotting and data visualization with MayaVi2 (and TVTK): a very powerful interactive scientific data visualizer.
Python Imaging Library. Create/manipulate images as numpy array's.
Mat3d. Simple 3D plotting using an OpenGL backend.
Line Integral Convolution code in cython for visualizing vector fields
VTK volume rendering. This is a simple example that show how to use VTK to volume render your three dimensional numpy arrays.
Using NumPy With Other Languages (Advanced)
A comparison of Weave with NumPy, Pyrex, Psyco, Fortran and C++ using Laplace's equation as an example.
Using Pyrex and NumPy to share data between your Pyrex/C extension module and NumPy.
Using Pyrex and the array_struct interface to access array data without requiring a C dependency on Numeric, numarray, or NumPy.
NumInd: A Numerical Agnostic Pyrex Class to access Numeric/numarray/NumPy arrays in an uniform way from both C and Pyrex space.
Using SWIG and NumPy to access and modify NumPy arrays in C libraries.
numpy.i: A few SWIG and numpy.i basic examples.
numpy.i: Using SWIG and numpy.i to handle automatic C memory deallocation from Python (using a modified numpy.i).
Using f2py to wrap Fortran codes.
Using f2py and Numpy to wrap C codes.
Writing C Extensions.
Using ctypes with NumPy.
Using /Weave and iterators for fast, generalized code.
Scientific GUIs
Using wxPython dialogs for simple user interaction.
Using TraitsUI to build interactive applications.
List of all pages in the category "Cookbook":
- Cookbook
- Cookbook/A Numerical Agnostic Pyrex Class
- Cookbook/ArrayStruct and Pyrex
- Cookbook/BuildingArrays
- Cookbook/C Extensions
- Cookbook/C Extensions/NumPy arrays
- Cookbook/CommTheory
- Cookbook/Ctypes
- Cookbook/Data Acquisition with PyUL
- Cookbook/DataFrame
- Cookbook/F2Py
- Cookbook/Finding Convex Hull
- Cookbook/InputOutput
- Cookbook/KDTree
- Cookbook/KalmanFiltering
- Cookbook/Matplotlib
- Cookbook/MayaVi
- Cookbook/MayaVi/Examples
- Cookbook/MayaVi/RunningMayavi2
- Cookbook/MayaVi/ScriptingMayavi2
- Cookbook/MayaVi/ScriptingMayavi2/BasicModules
- Cookbook/MayaVi/ScriptingMayavi2/Filters
- Cookbook/MayaVi/ScriptingMayavi2/MainModules
- Cookbook/MayaVi/Tips
- Cookbook/MayaVi/tvtk
- Cookbook/MetaArray
- Cookbook/Multithreading
- Cookbook/OptimizationDemo1
- Cookbook/PIL
- Cookbook/Pyrex and NumPy
- Cookbook/RANSAC
- Cookbook/Rebinning
- Cookbook/SWIG Memory Deallocation
- Cookbook/SWIG NumPy examples
- Cookbook/SWIG and NumPy
- Cookbook/SchrodingerFDTD
- Cookbook/ViewsVsCopies
- Cookbook/Weave
- Cookbook/dbase
- Cookbook/f2py and NumPy
- Cookbook/hdf5 in Matlab
- Cookbook/multiprocessing
- LoktaVolterraTutorial
- vtkVolumeRendering
