SciPy Interpolation
What is Interpolation?
In the field of numerical analysis in mathematics, interpolation (English: interpolation) is a process or method of estimating new data points within the range of a discrete set of known 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, and in machine learning, we often deal with missing data. Interpolation can typically be used to replace these values.
This method of filling in values is called imputation.
Besides imputation, interpolation is often used where we need to smooth out discrete points in a dataset.
How to Implement Interpolation in SciPy?
SciPy provides the scipy.interpolate
module to handle interpolation.
One-dimensional Interpolation interp1d()
Completed.
This method takes two parameters, x points and y points.
The return value is a callable function that can be called with new x values and will return the corresponding y values, y = f(x)
.
Interpolate for 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)
Output result:
[5.2 5.4 5.6 5.8 6. 6.2 6.4 6.6 6.8]
Note: The new xs should be within the same range as the old xs, which means 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, whereas in spline interpolation, points are fitted to functions defined piecewise by polynomials.
Univariate interpolation uses the UnivariateSpline()
function, which takes xs and ys and generates a callable function that can be called with new xs.
A piecewise function is a function that has different definitions for different ranges of the independent variable x.
Find univariate spline interpolation for 2.1, 2.2...2.9 for nonlinear points:
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)
Output result:
[5.62826474 6.03987348 6.47131994 6.92265019 7.3939103 7.88514634
8.39640439 8.92773053 9.47917082]
Radial Basis Function Interpolation
Radial basis functions are functions defined relative to a fixed reference point.
In surface interpolation, we generally use radial basis function interpolation.
The Rbf()
function takes 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)
Output result:
[6.25748981 6.62190817 7.00310702 7.40121814 7.8161443 8.24773402
8.69590519 9.16070828 9.64233874]