What do the different values of the kind argument mean in scipy.interpolate.interp1d?

别来无恙 提交于 2019-12-18 13:13:37

问题


The SciPy documentation explains that interp1d's kind argument can take the values ‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’. The last three are spline orders and 'linear' is self-explanatory. What do 'nearest' and 'zero' do?


回答1:


  • nearest "snaps" to the nearest data point.
  • zero is a zero order spline. It's value at any point is the last raw value seen.
  • linear performs linear interpolation and slinear uses a first order spline. They use different code and can produce similar but subtly different results.
  • quadratic uses second order spline interpolation.
  • cubic uses third order spline interpolation.

Note that the k parameter can also accept an integer specifying the order of spline interpolation.


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()




回答2:


‘nearest’ returns data point from X nearest to the argument, or interpolates function y=f(x) at the point x using the data point nearest to x

'zero' I would guess is equivalent to truncation of argument and thus using data point closest toward zero



来源:https://stackoverflow.com/questions/27698604/what-do-the-different-values-of-the-kind-argument-mean-in-scipy-interpolate-inte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!