问题
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.zerois a zero order spline. It's value at any point is the last raw value seen.linearperforms linear interpolation andslinearuses a first order spline. They use different code and can produce similar but subtly different results.quadraticuses second order spline interpolation.cubicuses 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