SlideShare a Scribd company logo
1 of 20
Download to read offline
Interpolation with Python



                                    November 20, 2009



Wednesday, December 2, 2009
Enthought Python Distribution (EPD)
                          http://www.enthought.com/products/epd.php




Wednesday, December 2, 2009
Enthought Training Courses
               Dec. 7 - 9
               Dec. 10 - 11




                              Python Basics, NumPy, SciPy,
                              Matplotlib, Traits, TraitsUI,
                              Chaco…



Wednesday, December 2, 2009
Enthought Consulting




Wednesday, December 2, 2009
Interpolation




Wednesday, December 2, 2009
Contrast with

                          Curve-fitting   Extrapolation




Wednesday, December 2, 2009
2-D Interpolation




Wednesday, December 2, 2009
Curve interpolation and scattered samples




Wednesday, December 2, 2009
Interpolation basic mathematics

           Given data points            (xi , fi )



                                    f (x)    =             fj φ(x − xj )
                                                      j
                              φ(xi − xj )    =       δij
                                   f (xi )   =       fi




Wednesday, December 2, 2009
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
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
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
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
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
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
                              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

More Related Content

More from Enthought, Inc.

February EPD Webinar: How do I...use PiCloud for cloud computing?
February EPD Webinar: How do I...use PiCloud for cloud computing?February EPD Webinar: How do I...use PiCloud for cloud computing?
February EPD Webinar: How do I...use PiCloud for cloud computing?Enthought, Inc.
 
Parallel Processing with IPython
Parallel Processing with IPythonParallel Processing with IPython
Parallel Processing with IPythonEnthought, Inc.
 
Scientific Computing with Python Webinar: Traits
Scientific Computing with Python Webinar: TraitsScientific Computing with Python Webinar: Traits
Scientific Computing with Python Webinar: TraitsEnthought, Inc.
 
Scientific Computing with Python Webinar 9/18/2009:Curve Fitting
Scientific Computing with Python Webinar 9/18/2009:Curve FittingScientific Computing with Python Webinar 9/18/2009:Curve Fitting
Scientific Computing with Python Webinar 9/18/2009:Curve FittingEnthought, Inc.
 
Scientific Computing with Python Webinar --- August 28, 2009
Scientific Computing with Python Webinar --- August 28, 2009Scientific Computing with Python Webinar --- August 28, 2009
Scientific Computing with Python Webinar --- August 28, 2009Enthought, Inc.
 
Scientific Computing with Python Webinar --- June 19, 2009
Scientific Computing with Python Webinar --- June 19, 2009Scientific Computing with Python Webinar --- June 19, 2009
Scientific Computing with Python Webinar --- June 19, 2009Enthought, Inc.
 
Scientific Computing with Python Webinar --- May 22, 2009
Scientific Computing with Python Webinar --- May 22, 2009Scientific Computing with Python Webinar --- May 22, 2009
Scientific Computing with Python Webinar --- May 22, 2009Enthought, Inc.
 

More from Enthought, Inc. (8)

February EPD Webinar: How do I...use PiCloud for cloud computing?
February EPD Webinar: How do I...use PiCloud for cloud computing?February EPD Webinar: How do I...use PiCloud for cloud computing?
February EPD Webinar: How do I...use PiCloud for cloud computing?
 
SciPy India 2009
SciPy India 2009SciPy India 2009
SciPy India 2009
 
Parallel Processing with IPython
Parallel Processing with IPythonParallel Processing with IPython
Parallel Processing with IPython
 
Scientific Computing with Python Webinar: Traits
Scientific Computing with Python Webinar: TraitsScientific Computing with Python Webinar: Traits
Scientific Computing with Python Webinar: Traits
 
Scientific Computing with Python Webinar 9/18/2009:Curve Fitting
Scientific Computing with Python Webinar 9/18/2009:Curve FittingScientific Computing with Python Webinar 9/18/2009:Curve Fitting
Scientific Computing with Python Webinar 9/18/2009:Curve Fitting
 
Scientific Computing with Python Webinar --- August 28, 2009
Scientific Computing with Python Webinar --- August 28, 2009Scientific Computing with Python Webinar --- August 28, 2009
Scientific Computing with Python Webinar --- August 28, 2009
 
Scientific Computing with Python Webinar --- June 19, 2009
Scientific Computing with Python Webinar --- June 19, 2009Scientific Computing with Python Webinar --- June 19, 2009
Scientific Computing with Python Webinar --- June 19, 2009
 
Scientific Computing with Python Webinar --- May 22, 2009
Scientific Computing with Python Webinar --- May 22, 2009Scientific Computing with Python Webinar --- May 22, 2009
Scientific Computing with Python Webinar --- May 22, 2009
 

Recently uploaded

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 

Interpolation with SciPy and NumPy

  • 1. Interpolation with Python November 20, 2009 Wednesday, December 2, 2009
  • 2. Enthought Python Distribution (EPD) http://www.enthought.com/products/epd.php Wednesday, December 2, 2009
  • 3. Enthought Training Courses Dec. 7 - 9 Dec. 10 - 11 Python Basics, NumPy, SciPy, Matplotlib, Traits, TraitsUI, Chaco… Wednesday, December 2, 2009
  • 6. Contrast with Curve-fitting Extrapolation Wednesday, December 2, 2009
  • 8. Curve interpolation and scattered samples Wednesday, December 2, 2009
  • 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