SciPy interpolation

chapter


Interpolation, is based on a series of points (xi, yi) to find a suitable function through a certain algorithm to include (approximate) of these points, these points reflect the trend of law, and the other seeking process according to the trend value point rule.

scipy.interpolate package can implement several classes to some known interpolating points, i.e., to find a suitable function, e.g., interp1d class, when the interpolation function can be used to obtain other computing yj xj corresponding values ​​after the interpolation function this is the significance of interpolation.

One-dimensional interpolation interp1d

interp1d point input can be prepared according to create fitting function.

Prepare data

Let's start by creating some point as input:

Examples

Data acquired by sampling several points:

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

Export

[0.         0.36363636 0.72727273 1.09090909 1.45454545 1.81818182
 2.18181818 2.54545455 2.90909091 3.27272727 3.63636364 4.        ]
[ 0.28366219  0.29287074  0.35652484  0.52035398  0.78524277  0.99671469
  0.70096272 -0.43008856 -0.87804302  0.84953035 -0.4614798   0.4979562 ]

Let us draw these points:

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

scipy interpolation 1

interp1d interpolation

The data in the above example, the fitting function using interp1d class to create:

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

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

The above creates two functions f1 and f2. By these functions, input x can be calculated y. kindIt indicates the type of interpolation technique used, for example: 'Linear', 'Nearest' , 'Zero', 'Slinear', 'Quadratic', 'Cubic' and the like.

Now, the increase in input data, and compare the earlier example:

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

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

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

plt.show()

The above procedure produces the following output:

scipy interpolation 2

Noise data interpolation

Interpolation may be data containing noise interpolate module UnivariateSpline class.

Use UnivariateSpline class, a set of input data points, the noise is removed by drawing a smooth curve. Smoothing parameter s may be provided when drawing the curve, if the parameter s = 0, all points will (including noise) interpolation operation, i.e. s = 0 when the noise is not removed.

Examples

import numpy as np
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) # 通过random方法添加噪声数据
plt.plot(x, y, 'ro', ms=5)

# 平滑参数使用默认值
spl = UnivariateSpline(x, y)
xs = np.linspace(-3, 3, 1000)
plt.plot(xs, spl(xs), 'b', lw=3) # 蓝色曲线

# 设置平滑参数
spl.set_smoothing_factor(0.5)
plt.plot(xs, spl(xs), 'g', lw=3) # 绿色曲线

# 设置平滑参数为0
spl.set_smoothing_factor(0)
plt.plot(xs, spl(xs), 'yellow', lw=3) # 黄色曲线

plt.show() 

Export

scipy interpolation 3

Guess you like

Origin www.cnblogs.com/jinbuqi/p/11811148.html