SciPy Tutorial · SciPy Tutorial

SciPy Interpolation

Learn all about SciPy Interpolation in this comprehensive tutorial.

5 min read advanced
  • Interpolation is a method for generating points between given points.
  • SciPy provides us with a module called scipy.
  • The function interp1d() is used to interpolate a distribution with 1 variable.
  • In 1D interpolation the points are fitted for a single curve whereas in Spline interpolation the points are fitted against a piecewise function defined with polynomials called splines.
  • Radial basis function is a function that is defined corresponding to a fixed reference point.

What is Interpolation?

Interpolation is a method for generating points between given points.

For example: for points 1 and 2, we may interpolate and find points 1.33 and 1.66.

Interpolation has many usage, in Machine Learning we often deal with missing data in a dataset, interpolation is often used to substitute those values.

This method of filling values is called imputation.

Apart from imputation, interpolation is often used where we need to smooth the discrete points in a dataset.

How to Implement it in SciPy?

SciPy provides us with a module called scipy.interpolate which has many functions to deal with interpolation:

1D Interpolation

The function interp1d() is used to interpolate a distribution with 1 variable.

It takes x and y points and returns a callable function that can be called with new x and returns corresponding y.

python
Note: Note: that new xs should be in same range as of the old xs, meaning that we can't call interp_func() with values higher than 10, or less than 0.

Spline Interpolation

In 1D interpolation the points are fitted for a single curve whereas in Spline interpolation the points are fitted against a piecewise function defined with polynomials called splines.

The UnivariateSpline() function takes xs and ys and produce a callable funciton that can be called with new xs.

Note: Piecewise function: A function that has different definition for different ranges.
python

Interpolation with Radial Basis Function

Radial basis function is a function that is defined corresponding to a fixed reference point.

The Rbf() function also takes xs and ys as arguments and produces a callable function that can be called with new xs.

python

Module quiz

2 questions
1

Which of the following is true about SciPy Interpolation?

2

What is the most common pitfall when working with SciPy Interpolation?

Answer all questions to submit.