SciPy Interpolation

What is interpolation?

In the field of numerical analysis in mathematics, interpolation is a process or method of deriving new data points within a range from known, discrete data points.

Simply put, interpolation is a method of generating points between given points.

For example:For two points 1 and 2, we can interpolate and find points 1.33 and 1.66.

Interpolation has many uses. In machine learning, we often deal with data with missing values, and interpolation can usually be used to replace these values.

This method of filling in values is called imputation.

In addition to imputation, interpolation is often used where we need to smooth discrete points in a dataset.

How to implement interpolation in SciPy?

SciPy provides the scipy.interpolate module to handle interpolation.

One-dimensional interpolation

For one-dimensional data interpolation, you can use the methodinterp1d()to complete it.

This method takes two parameters: x points and y points.

The return value is a callable function, which can be called with a new x and returns the corresponding y,y = f(x)。

Interpolate the given xs and ys, from 2.1, 2.2... to 2.9:

Example

from scipy.interpolate import interp1d
import numpy as np

xs = np.arange(10)
ys = 2*xs + 1

interp_func = interp1d(xs, ys)

newarr = interp_func(np.arange(2.1, 3, 0.1))

print(newarr)

The output result is:

[5.2  5.4  5.6  5.8  6.   6.2  6.4  6.6  6.8]

Note:The new xs should be in the same range as the old xs, meaning we cannot call interp_func() with values greater than 10 or less than 0.

Univariate interpolation

In one-dimensional interpolation, points are fitted to a single curve, while in spline interpolation, points are fitted to functions defined piecewise using polynomials.

Univariate interpolation uses the UnivariateSpline() function, which accepts xs and ys and generates a callable function that can be called with new xs.

A piecewise function is a function that has different analytical expressions for different value ranges of the independent variable x.

Find the univariate spline interpolation for non-linear points 2.1, 2.2...2.9:

Example

from scipy.interpolate import UnivariateSpline
import numpy as np

xs = np.arange(10)
ys = xs**2 + np.sin(xs) + 1

interp_func = UnivariateSpline(xs, ys)

newarr = interp_func(np.arange(2.1, 3, 0.1))

print(newarr)

The output result is:

[5.62826474 6.03987348 6.47131994 6.92265019 7.3939103  7.88514634
   8.39640439 8.92773053 9.47917082]

Radial basis function interpolation

A radial basis function is a function defined with respect to fixed reference points.

In surface interpolation, we generally use radial basis function interpolation.

The Rbf() function accepts xs and ys as parameters and generates a callable function that can be called with new xs.

Example

from scipy.interpolate import Rbf
import numpy as np

xs = np.arange(10)
ys = xs**2 + np.sin(xs) + 1

interp_func = Rbf(xs, ys)

newarr = interp_func(np.arange(2.1, 3, 0.1))

print(newarr)

The output result is:

  [6.25748981  6.62190817  7.00310702  7.40121814  7.8161443   8.24773402
   8.69590519  9.16070828  9.64233874]
Other Extensions