Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 739
xnx Avatar asked Dec 30 '14 01:12

xnx


1 Answers

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

enter image description here

like image 105
unutbu Avatar answered Nov 15 '22 16:11

unutbu