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.
FittingData Day to day work in the lab: fitting experimental data.
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
SphericalBesselZeros Finding the zeros of the spherical Bessel functions and its derivative
RadialBasisFunctions Using radial basis functions for smoothing/interpolation
SegmentAxis 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.
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. Same recipies at 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.
FortranIO Reading FORTRAN record-structured binary files (if you don't know what these are, thank your stars and you don't need this).
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 graphics with MayaVi2/TVTK (and related tools) which is the next generation of the MayaVi scientific data visualizer.
Python Imaging Library. Create/manipulate images as numpy array's.
Mat3d. Simple 3D plotting using an OpenGL backend.
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.
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/Ctypes
- Cookbook/Data Acquisition with NIDAQmx
- Cookbook/Data Acquisition with PyUL
- Cookbook/DataFrame
- Cookbook/F2Py
- Cookbook/Finding Convex Hull
- Cookbook/InputOutput
- 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 and NumPy
- Cookbook/SchrodingerFDTD
- Cookbook/ViewsVsCopies
- Cookbook/Weave
- Cookbook/dbase
- Cookbook/f2py and NumPy
