【发布时间】:2014-12-30 01:49:22
【问题描述】:
SciPy documentation 解释说interp1d 的kind 参数可以取值‘linear’、‘nearest’、‘zero’、‘slinear’、‘quadratic’、‘cubic’。最后三个是样条命令,'linear' 是不言自明的。 'nearest' 和 'zero' 是做什么的?
【问题讨论】:
标签: python scipy interpolation
SciPy documentation 解释说interp1d 的kind 参数可以取值‘linear’、‘nearest’、‘zero’、‘slinear’、‘quadratic’、‘cubic’。最后三个是样条命令,'linear' 是不言自明的。 'nearest' 和 'zero' 是做什么的?
【问题讨论】:
标签: python scipy interpolation
nearest“捕捉”到最近的数据点。zero 是零阶样条。它在任何时候的值都是最后看到的原始值。linear 执行线性插值,slinear 使用第一个
顺序样条。他们使用不同的代码和can produce similar but subtly different results。quadratic 使用二阶样条插值。cubic 使用三阶样条插值。请注意,k 参数也可以接受指定样条插值顺序的整数。
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate
np.random.seed(6)
kinds = ('nearest', 'zero', 'linear', 'slinear', 'quadratic', 'cubic')
N = 10
x = np.linspace(0, 1, N)
y = np.random.randint(10, size=(N,))
new_x = np.linspace(0, 1, 28)
fig, axs = plt.subplots(nrows=len(kinds)+1, sharex=True)
axs[0].plot(x, y, 'bo-')
axs[0].set_title('raw')
for ax, kind in zip(axs[1:], kinds):
new_y = interpolate.interp1d(x, y, kind=kind)(new_x)
ax.plot(new_x, new_y, 'ro-')
ax.set_title(kind)
plt.show()
【讨论】:
interpolate.interp1d 似乎仍然会在 kind='linear' 或 kind='slinear' 时产生相同的结果。
‘nearest’ 返回 X 中离参数最近的数据点,或者
interpolates function y=f(x) at the point x using the data point nearest to x
'zero' 我猜相当于截断参数,因此使用最接近零的数据点
【讨论】: