[[TableOfContents]] 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. = Scipy Central = * You can find and submit more recipes at http://scipy-central.org/ (which eventually can supersede this wiki page) = NumPy / SciPy = * !NumPy arrays: tips and tricks: * [:Cookbook/BuildingArrays:Building Arrays] Introduction to numerical arrays. * [:Cookbook/Indexing:Indexing] Indexing numpy arrays, from simple to complicated. * [:Cookbook/MultiDot:Multiplying multiple arguments] Generalizing `dot(a,b)` to the case of N arguments. * [:Cookbook/SegmentAxis:Segment axis] Devious trick using strides to allow general operations (like convolution) on successive, overlapping pieces of an array * [:Cookbook/GameOfLifeStrides:Stride tricks for the Game of Life] This is similar to [:Cookbook/SegmentAxis:Segment axis], but for 2D arrays with 2D windows. * [:Cookbook/MetaArray:MetaArray] Class for storing per-axis meta [http://open.salon.com/blog/smebs/2011/05/16/marketing_data_box_-_complete_strategy_guide_for_smbs data] information with an array (axis names, column names, units, etc.) * [:Cookbook/Obarray:Obarray] Trick for avoiding object arrays when dealing with arrays of objects. * [:Cookbook/Recarray:Recarray] Accessing array columns with structured arrays and recarrays. * [:Cookbook/AccumarrayLike:accumarray-like function] A function that behaves like MATLAB's accumarray. * Linear Algebra * [:Cookbook/RankNullspace:Rank and nullspace] of a matrix. * Interpolation: * [:Cookbook/Interpolation:Interpolation] Examples of interpolation (see also [:Cookbook/Matplotlib/Gridding irregularly spaced data:Gridding irregularly spaced data]). * [:Cookbook/RadialBasisFunctions:Radial Basis Functions] Using radial basis functions for smoothing/interpolation * Optimization and fitting techniques: * [:Cookbook/OptimizationDemo1:Optimization] Quick example of fminbound with plot. * [:Cookbook/OptimizationAndFitDemo1:Optimization with fit] Similar to above with spline fit and chaco plot. * [:Cookbook/FittingData:Fitting Data] Day to day work in the lab: fitting experimental data. * [:Cookbook/LinearRegression:Linear regression] Simple Linear regression example. * [:Cookbook/OLS:Fit statistics] Estimates a multi-variate regression model and provides various fit statistics. * [:Cookbook/Least Squares Circle:Least Squares Circle] Fitting the least squares circle from a series of 2D points * [:Cookbook/RANSAC:RANSAC algorithm] Implementation of the robust estimation method. * Ordinary differential equations * [:Cookbook/Zombie Apocalypse ODEINT:Zombie Apocalypse ODE Modeling] Modeling a Zombie Apocalypse by solving a system of ordinary differential equations using the SciPy routine, ''odeint''. * [:Cookbook/CoupledSpringMassSystem:A coupled spring-mass system] Another example of solving differential equations. * [:LoktaVolterraTutorial:Lotka-Volterra Tutorial] Solving ordinary differential equations with Scipy * [:Cookbook/KdV:KdV] Solving the Korteweg-deVries equation on a periodic domain using the pseudo-spectral method with the method of lines. * [:Cookbook/Theoretical Ecology/Hastings and Powell:Hastings and Powell food chain model] * Root finding * [:Cookbook/SphericalBesselZeros:Spherical Bessel Zeros] Finding the zeros of the spherical Bessel functions and its derivative * [:Cookbook/Intersection:Intersection of functions] Compute the points at which two given functions intersect. * [:Cookbook/Rebinning:Data rebinning] Examples of rebinning data to produce smaller arrays with and without interpolation. * [:Cookbook/Histograms:Histograms] 2D histograms with variable bin width * [:Cookbook/Finding Convex Hull:Convex Hull] Finds the convex hull around a set of data points. * [:Cookbook/Finding Convex Hull Minimum Point:Minimum Point of a Convex Hull] Finds the minimum point of the convex hull of a finite set of points. * [:Cookbook/KalmanFiltering:Kalman Filtering] Example from the Welch & Bishop [http://www.cs.unc.edu/~welch/kalman/kalmanIntro.html Introduction to the Kalman Filter]. * [:Cookbook/CommTheory:Comm Theory] Example of BPSK simulation. * [:Cookbook/SignalSmooth:Smoothing a signal] Performing smoothing of 1D and 2D signals by convolving them with a window. * [:Cookbook/ButterworthBandpass:Butterworth Bandpass Filter] Create and apply a Butterworth bandpass filter. * [:Cookbook/FIRFilter:FIR Filter Design] Design a low-pass FIR filter using the window method. * [:Cookbook/ApplyFIRFilter:How to apply a FIR filter:] signal.convolve, signal.fftconvolve, ndimage.convolve1d or signal.lfilter? * [:Cookbook/FiltFilt: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. * [:Cookbook/SavitzkyGolay:Savitzky Golay filtering of data] Sample code for Savitzky Golay filtering. * [:Cookbook/FrequencySweptDemo:Frequency-swept signals] Generating frequency-swept signals with chirp() and sweep_poly() from scipy.signal (SciPy 0.8) * [:Cookbook/Multithreading:Multithreading] Easy multithreading for embarrassingly parallel problems * [:Cookbook/KDTree:KDTree] Searching multidimensional space using kd-trees. * [:Cookbook/ParticleFilter:Particle Filter] A simple particle filter algorithm for tracking objects in a video sequence. * [:Cookbook/BrownianMotion:Brownian Motion] Compute Brownian motion (i.e. the Wiener process). * [:Cookbook/CorrelatedRandomSamples:Correlated Random Samples] Generate correlated normally distributed random samples. * [:Cookbook/Solving Large Markov Chains:Large Markov Chains] Find the stationary distribution of a large Markov chain; the M/M/1 tandem queue * [:Cookbook/Watershed:Watershed algorithm] Apply the watershed algorithm in order to split an array into distinct components (e.g. for the segmentation of an image into objects). * [:Cookbook/LinearClassification:Linear Classification] Fisher's discriminant function and Probabilistic Generative model * [:Cookbook/EyeDiagram: Plot an eye diagram] using numpy and matplotlib. == Advanced topics == * [:Cookbook/ViewsVsCopies:Views vs Copies] A quick introduction to array views and some caveats on situations where you should expect a [http://open.salon.com/blog/smebs/2011/05/16/marketing_data_box_-_complete_strategy_guide_for_smbs data] view or a data copy. = Compiling Extensions = * [:Cookbook/CompilingExtensionsOnWindowsWithMinGW:Compiling Extensions on Windows] A quick tutorial on how to compile extension modules on Windows using MinGW = Scientific Scripts = * [:Cookbook/Theoretical Ecology:Theoretical Ecology] * [:Cookbook/SchrodingerFDTD: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 = * [:Cookbook/hdf5 in Matlab:Loading hdf5 in Matlab] * [:Cookbook/Reading mat files:Reading Matlab .mat files] * [:Cookbook/DataFrame:DataFrames] A useful class for storing alphanumerical data, similar to GNU R's data frames. * [:Cookbook/Data Acquisition with PyUL:Data acquisition with PyUniversalLibrary] A series of examples using an inexpensive USB data acquisition device from Measurement Computing. * [:Cookbook/Data Acquisition with NIDAQmx:Data acquisition with Ni-DAQmx] A simple example of using ctypes and numpy to access data acquisition devices from National Instruments. * [:Cookbook/InputOutput:input/output] Reading and writing a !NumPy array from/to an ascii/binary file. * [:Cookbook/FortranIO: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). * [:Cookbook/Reading SPE files:Reading SPE files] Reading SPE binary files produced by CCD cameras (Princeton and like). * [:Cookbook/LASReader:Reading LAS files] Reading LAS (Log ASCII Standard) well log files. = 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. * [:Cookbook/Matplotlib:Matplotlib cookbook]. Matplotlib is the preferred package for 2D graphics. * [:Cookbook/MayaVi:3D plotting with Mayavi]. Advanced 3D data visualization with !MayaVi2 (and TVTK): a very powerful interactive scientific data visualizer. * [:Cookbook/PIL:Python Imaging Library]. Create/manipulate images as numpy array's. * [:Cookbook/xplt: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. * [:WilnaDuToit:Mat3d]. Simple 3D plotting using an OpenGL backend. * [:Cookbook/LineIntegralConvolution:Line Integral Convolution] code in cython for visualizing vector fields * [:vtkVolumeRendering:VTK volume rendering]. This is a simple example that show how to use VTK to volume render your three dimensional numpy arrays. * [:Cookbook/OldMatplotlib:Old Matplotlib recipes]. The recipes on this page are being contributed to the Matplotlib project and will eventually be deleted from this Wiki. = Using NumPy With Other Languages (Advanced) = * A [:PerformancePython:comparison] of Weave with !NumPy, Pyrex, Psyco, Fortran and C++ using Laplace's equation as an example. * Using [:Cookbook/Pyrex and NumPy:Pyrex and NumPy] to share data between your Pyrex/C extension module and !NumPy. * Using [:Cookbook/ArrayStruct and Pyrex:Pyrex and the array_struct interface] to access array data without requiring a C dependency on Numeric, numarray, or !NumPy. * '''!NumInd''': [:Cookbook/A Numerical Agnostic Pyrex Class:A Numerical Agnostic Pyrex Class] to access Numeric/numarray/!NumPy arrays in an uniform way from both C and Pyrex space. * Using [:Cookbook/SWIG and NumPy:SWIG and NumPy] to access and modify !NumPy arrays in C libraries. * '''numpy.i''': A few [:Cookbook/SWIG NumPy examples:SWIG and numpy.i] basic examples. * '''numpy.i''': Using [:Cookbook/SWIG Memory Deallocation:SWIG and numpy.i] to handle automatic C memory deallocation from Python (using a modified numpy.i). * Using [:Cookbook/F2Py:f2py] to wrap Fortran codes. * Using [:Cookbook/f2py and NumPy:f2py and Numpy] to wrap C codes. * Writing [:Cookbook/C Extensions:C Extensions]. * Using [:Cookbook/Ctypes:ctypes with NumPy]. * Using ["/Weave"] and '''iterators''' for fast, generalized code. = Scientific GUIs = * Using [:Cookbook/wxPython dialogs:wxPython dialogs] for simple user interaction. * Using ["TraitsUI"] to build interactive applications. ---- . '''List of all pages in the category "Cookbook":''' . [[FullSearch(regex:(----(-*)(\r)?\n)(.*)CategoryCookbook\b)]] ---- . CategoryCookbook ---- . CategoryCookbook CategoryCookbook CategoryCookbook CategoryCookbook CategoryCookbook