Scipy 简明教程

SciPy - Interpolate

在本章中,我们将讨论插值如何帮助 SciPy。

In this chapter, we will discuss how interpolation helps in SciPy.

What is Interpolation?

插值是在线或曲线上两个点之间找到一个值的过程。为了帮助我们记住它的含义,我们应该把这个词的第一部分“inter”理解为“enter”(进入),这提醒我们查看我们最初拥有数据的“内部”。这个工具 - 插值,不仅在统计学中有用,在科学、商业或需要预测位于两个现有数据点之内的值的场合也有用。

Interpolation is the process of finding a value between two points on a line or a curve. To help us remember what it means, we should think of the first part of the word, 'inter,' as meaning 'enter,' which reminds us to look 'inside' the data we originally had. This tool, interpolation, is not only useful in statistics, but is also useful in science, business, or when there is a need to predict values that fall within two existing data points.

让我们创建一些数据,看看如何使用 scipy.interpolate 包来完成这种插值。

Let us create some data and see how this interpolation can be done using the scipy.interpolate package.

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 12)
y = np.cos(x**2/3+4)
print x,y

上述程序将生成以下输出。

The above program will generate the following output.

(
   array([0.,  0.36363636,  0.72727273,  1.09090909,  1.45454545, 1.81818182,
          2.18181818,  2.54545455,  2.90909091,  3.27272727,  3.63636364,  4.]),

   array([-0.65364362,  -0.61966189,  -0.51077021,  -0.31047698,  -0.00715476,
           0.37976236,   0.76715099,   0.99239518,   0.85886263,   0.27994201,
          -0.52586509,  -0.99582185])
)

现在,我们有两个数组。假设这两个数组是空间中点的两个维度,让我们使用以下程序绘制它们并看看它们是什么样子的。

Now, we have two arrays. Assuming those two arrays as the two dimensions of the points in space, let us plot using the following program and see how they look like.

plt.plot(x, y,’o’)
plt.show()

上述程序将生成以下输出。

The above program will generate the following output.

interpolation

1-D Interpolation

scipy.interpolate 中的 interp1d 类是一个便捷的方法,用于基于固定数据点创建函数,该函数可以使用线性插值在由给定数据定义的范围内任何位置进行求值。

The interp1d class in the scipy.interpolate is a convenient method to create a function based on fixed data points, which can be evaluated anywhere within the domain defined by the given data using linear interpolation.

让我们使用上述数据创建一个插值函数并绘制一个新的插值图。

By using the above data, let us create a interpolate function and draw a new interpolated graph.

f1 = interp1d(x, y,kind = 'linear')

f2 = interp1d(x, y, kind = 'cubic')

使用 interp1d 函数,我们创建了两个函数 f1 和 f2。这些函数为给定的输入 x 返回 y。第三个变量 kind 表示插值技术的类型。“Linear”(线性)、“Nearest”(最近)、“Zero”(零)、“Slinear”(线性分段)、“Quadratic”(二次)、“Cubic”(三次)是一些插值技术。

Using the interp1d function, we created two functions f1 and f2. These functions, for a given input x returns y. The third variable kind represents the type of the interpolation technique. 'Linear', 'Nearest', 'Zero', 'Slinear', 'Quadratic', 'Cubic' are a few techniques of interpolation.

现在,让我们创建一个更长的新的输入来清楚地了解插值的区别。我们将对新数据使用旧数据的相同函数。

Now, let us create a new input of more length to see the clear difference of interpolation. We will use the same function of the old data on the new data.

xnew = np.linspace(0, 4,30)

plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')

plt.legend(['data', 'linear', 'cubic','nearest'], loc = 'best')

plt.show()

上述程序将生成以下输出。

The above program will generate the following output.

1d interpolation

Splines

为了绘制通过数据点的平滑曲线,绘图员曾经使用称为机械样条的薄柔性木、硬橡胶、金属或塑料条。为了使用机械样条,在沿着设计中曲线上精心挑选的点上放置销钉,然后弯曲样条,以便它触碰到这些销钉中的每一个。

To draw smooth curves through data points, drafters once used thin flexible strips of wood, hard rubber, metal or plastic called mechanical splines. To use a mechanical spline, pins were placed at a judicious selection of points along a curve in a design, and then the spline was bent, so that it touched each of these pins.

显然,通过这种构造,样条在这些销钉处对曲线进行插值。它可用于在其他图纸中再现曲线。放置销钉的点称为结。我们可以通过调整结的位置来改变样条定义曲线的形状。

Clearly, with this construction, the spline interpolates the curve at these pins. It can be used to reproduce the curve in other drawings. The points where the pins are located is called knots. We can change the shape of the curve defined by the spline by adjusting the location of the knots.

Univariate Spline

一维平滑样条拟合给定的一组数据点。scipy.interpolate 中的 UnivariateSpline 类是一个便捷的方法,用于基于固定数据点创建函数,类 - scipy.interpolate.UnivariateSpline(x, y, w = None, bbox = [None, None], k = 3, s = None, ext = 0, check_finite = False)。

One-dimensional smoothing spline fits a given set of data points. The UnivariateSpline class in scipy.interpolate is a convenient method to create a function, based on fixed data points class – scipy.interpolate.UnivariateSpline(x, y, w = None, bbox = [None, None], k = 3, s = None, ext = 0, check_finite = False).

Parameters − 以下是 Univariate 样条的参数。

Parameters − Following are the parameters of a Univariate Spline.

  1. This fits a spline y = spl(x) of degree k to the provided x, y data.

  2. ‘w’ − Specifies the weights for spline fitting. Must be positive. If none (default), weights are all equal.

  3. ‘s’ − Specifies the number of knots by specifying a smoothing condition.

  4. ‘k’ − Degree of the smoothing spline. Must be ⇐ 5. Default is k = 3, a cubic spline.

  5. Ext − Controls the extrapolation mode for elements not in the interval defined by the knot sequence.

  6. check_finite – Whether to check that the input arrays contain only finite numbers.

让我们考虑以下示例。

Let us consider the following example.

import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline
x = np.linspace(-3, 3, 50)
y = np.exp(-x**2) + 0.1 * np.random.randn(50)
plt.plot(x, y, 'ro', ms = 5)
plt.show()

对平滑参数使用默认值。

Use the default value for the smoothing parameter.

splines
spl = UnivariateSpline(x, y)
xs = np.linspace(-3, 3, 1000)
plt.plot(xs, spl(xs), 'g', lw = 3)
plt.show()

手动更改平滑量。

Manually change the amount of smoothing.

splines smoothing
spl.set_smoothing_factor(0.5)
plt.plot(xs, spl(xs), 'b', lw = 3)
plt.show()
splines smoothing2