In many data-processing scenarios it is necessary to use a discrete set of available data-points to infer the value of a function at a new data-point. One approach to this problem is interpolation, which constructs a new model-function that goes through the original data-points. There are many forms of interpolation (polynomial, spline, kriging, radial basis function, etc.), and SciPy includes some of these interpolation forms. This webinar will review the interpolation modules available in SciPy and in the larger Python community and provide instruction on their use via example.
9. Interpolation basic mathematics
Given data points (xi , fi )
f (x) = fj φ(x − xj )
j
φ(xi − xj ) = δij
f (xi ) = fi
Wednesday, December 2, 2009
10. Interpolation
scipy.interpolate — General purpose Interpolation
•1D Interpolating Class
• Constructs callable function from data points and
desired spline interpolation order.
• Function takes vector of inputs and returns interpolated
value using the spline.
•1D and 2D spline interpolation (FITPACK)
• Smoothing splines up to order 5
• Parametric splines
10
Wednesday, December 2, 2009
11. 1D Spline Interpolation
>>> from scipy.interpolate import interp1d
interp1d(x, y, kind='linear', axis=-1, copy=True,
bounds_error=True, fill_value=numpy.nan)
Returns a function that uses interpolation to find the
value of new points.
• x – 1d array of increasing real values which cannot
contain duplicates
• y – Nd array of real values whose length along the
interpolation axis must be len(x)
• kind – kind of interpolation (e.g. 'linear',
'nearest', 'quadratic', 'cubic'). Can also be an
integer n>1 which returns interpolating spline (with
minimum sum-of-squares discontinuity in nth
derivative).
• axis – axis of y along which to interpolate
• copy – make internal copies of x and y
• bounds_error – raise error for out-of-bounds
• fill_value – if bounds_error is False, then use this
value to fill in out-of-bounds.
11
Wednesday, December 2, 2009
12. 1D Spline Interpolation
# demo/interpolate/spline.py
from scipy.interpolate import interp1d
from pylab import plot, axis, legend
from numpy import linspace
# sample values
x = linspace(0,2*pi,6)
y = sin(x)
# Create a spline class for interpolation.
# kind=5 sets to 5th degree spline.
# kind='nearest' -> zeroth older hold.
# kind='linear' -> linear interpolation
# kind=n -> use an nth order spline
spline_fit = interp1d(x,y,kind=5)
xx = linspace(0,2*pi, 50)
yy = spline_fit(xx)
# display the results.
plot(xx, sin(xx), 'r-', x, y, 'ro', xx, yy, 'b--', linewidth=2)
axis('tight')
legend(['actual sin', 'original samples', 'interpolated curve'])
12
Wednesday, December 2, 2009
13. 2D Spline Interpolation
>>> from scipy.interpolate import interp2d
interp2d(x, y, z, kind='linear')
Returns a function, f, that uses interpolation to find the value of new points: z_new = f(x_new,
y_new)
x – 1d or 2d array
y – 1d or 2d array
z – 1d or 2d array representing function evaluated at x
and y
kind – kind of interpolation: 'linear', 'quadratic', or
'cubic'
The shape of x, y, and z must be the same.
Resulting function is evaluated
at cross product of new inputs.
13
Wednesday, December 2, 2009
14. 2D Spline Interpolation
EXAMPLE
>>> from scipy.interpolate import
... interp2d
>>> from numpy import hypot, mgrid,
... linspace
>>> from scipy.special import j0
>>> x,y = mgrid[-5:6,-5:6]
>>> z = j0(hypot(x,y))
>>> newfunc = interp2d(x, y, z,
... kind='cubic')
>>> xx = linspace(-5,5,100)
>>> yy = xx
# xx and yy are 1-d
# result is evaluated on the
# cross product
>>> zz = newfunc(xx,yy)
>>> from enthought.mayavi import mlab
>>> mlab.surf(x,y,z)
>>> x2, y2 = mgrid[-5:5:100j,
... -5:5:100j]
>>> mlab.surf(x2,y2,zz)
14
Wednesday, December 2, 2009
15. What else is in SciPy?
• Interpolate: Fitpack interface
• splrep : represent 1-d data as spline
• splprep : represent N-d parametric curve as spline
• splev : evaluate spline at new data points
• splint : evaluate integral of spline
• splalde : evaluate all derivatives of spline
• bisplrep : reprsent 2-d surface as spline
• bisplev : evaluate 2-d spline at new data points
• Additional class-based interface:
–UnivariateSpline
–InterpolatedUnivariateSpline
–LSQUnivariateSpline
–BivariateSpline
–SmoothBivariateSpline
Wednesday, December 2, 2009
16. What else is in SciPy?
• Interpolate: General spline interface (no fitpack)
• splmake : create a general spline representation from data
• spleval : evaluate spline at new data
• spline : front end to splmake and spleval (all in one)
• spltopp : take spline reprentation and return piece-wise
polynomial representation of a spline
• ppform : piecewise polynomial representation of spline
• PiecewisePolynomial : class to generate arbitrary piecewise
polynomial interpolator given data + derivatives
• lagrange : Lagrange polynomial interpolator
• BarycentricInterpolator : polynomial interpolation class
• KroghInterpolator : polynomial interpolation class that allows
setting derivatives as well
Wednesday, December 2, 2009
17. What else is in SciPy?
• Signal: Fast B-spline implementations (equally-spaced)
• bspline : B-spline basis functions of order n
• gauss_spline : Gaussian approximation to the B-spline
• qspline1d : quadratic B-spline coefficients from 1-d data
• cspline1d : cubic B-spline coefficients from 1-d data
• qspline1d_eval : evaluate quadratic spline
• cspline1d_eval : evaluat cubic spline
• qspline2d : quadratic B-spline coefficients from 2-d data
• cspline2d : cubic B-spline coefficients from 2-d data
• spline_filter : spline filter using from coefficients
• resample : sinc-interpolation using a Fourier method
Wednesday, December 2, 2009
18. What else is in SciPy?
• ndimage: N-d spline interpolation
• map_coordinates : N-d interpolation
• spline_filter : repeated interpolation from spline coefficients
• interpolate : Radial Basis Functions
• Rbf : N-d interpolation using radial basis functions
Wednesday, December 2, 2009
19. Things I’d like to see
• Monotonic Splines
• PCHIP
• Finishing out the splmake, spleval interface
• Improving a simplified interface to all the
tools
• An interpnd
Wednesday, December 2, 2009
20. Scientific Python Classes
http://www.enthought.com/training
Dec 7-11
Feb 22-26 Python for Scientists and Engineers (3 days)
Advanced Modules (2 days)
May 17-21 Take both together to receive a discount
Aug 23-27
Wednesday, December 2, 2009