Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SciPy Interpolation

SciPy Interpolation

What is Interpolation?

In the field of numerical analysis in mathematics, interpolation is a process or method of estimating 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 that has missing values, and interpolation can usually be used to replace those values.

This method of filling values is called imputation.

Besides imputation, interpolation is often used where we need to smooth discrete points in a data set.

How to implement interpolation in SciPy?

SciPy provides the scipy.interpolate module to handle interpolation.

One-Dimensional Interpolation

Interpolation of one-dimensional data can be done via the method

interp1d()

to complete.

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

The return value is a callable function that 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 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, 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 a function defined piecewise with 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 analytic expressions for different ranges of the independent variable x.

Find the univariate spline interpolation for the nonlinear points 2.1, 2.2...2.9:

Example

from

scipy.

interpolate

import

UnivariateSpline

import

numpy

as

np

xs

=

np.

arange

(

10

)

ys

=

xs**

2

sin

(

xs

)

1

interp_func

=

UnivariateSpline

(

xs

,

ys

)

newarr

=

interp_func

(

np.

arange

(

2.1

,

3

,

0.1

)

)

print

(

newarr

)

The output 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 relative to a fixed reference point.

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

sin

(

xs

)

1

interp_func

=

Rbf

(

xs

,

ys

)

newarr

=

interp_func

(

np.

arange

(

2.1

,

3

,

0.1

)

)

print

(

newarr

)

The output is:



  [6.25748981  6.62190817  7.00310702  7.40121814  7.8161443   8.24773402

   8.69590519  9.16070828  9.64233874]