Interpolation with SciPy and NumPy
-
Upload
enthought-inc -
Category
Technology
-
view
41.442 -
download
4
description
Transcript of Interpolation with SciPy and NumPy
Enthought Python Distribution (EPD)http://www.enthought.com/products/epd.php
Wednesday, December 2, 2009
Enthought Training Courses
Python Basics, NumPy, SciPy, Matplotlib, Traits, TraitsUI, Chaco…
Dec. 7 - 9Dec. 10 - 11
Wednesday, December 2, 2009
Interpolation basic mathematics
Given data points (xi, fi)
f(x) =!
j
fj!(x! xj)
!(xi ! xj) = "ij
f(xi) = fi
Wednesday, December 2, 2009
10
Interpolationscipy.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
Wednesday, December 2, 2009
11
1D Spline Interpolation
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.
>>> from scipy.interpolate import interp1d
Wednesday, December 2, 2009
12
1D Spline Interpolation# demo/interpolate/spline.pyfrom scipy.interpolate import interp1dfrom pylab import plot, axis, legendfrom numpy import linspace
# sample valuesx = 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 splinespline_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'])
Wednesday, December 2, 2009
13
2D Spline Interpolation
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 arrayy – 1d or 2d arrayz – 1d or 2d array representing function evaluated at x and ykind – kind of interpolation: 'linear', 'quadratic', or 'cubic'
The shape of x, y, and z must be the same.
>>> from scipy.interpolate import interp2d
Resulting function is evaluated at cross product of new inputs.
Wednesday, December 2, 2009
14
2D Spline InterpolationEXAMPLE
>>> 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)
Wednesday, December 2, 2009
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
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
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
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
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
Scientific Python Classes
Dec 7-11Feb 22-26May 17-21Aug 23-27
http://www.enthought.com/training
Python for Scientists and Engineers (3 days)Advanced Modules (2 days)Take both together to receive a discount
Wednesday, December 2, 2009