我编写了执行样条插值的代码:

x1 = [ 0., 13.99576991, 27.99153981, 41.98730972, 55.98307963, 69.97884954, 83.97461944, 97.97038935, 111.9661593, 125.9619292, 139.9576991, 153.953469 ]
y1 = [ 1., 0.88675318, 0.67899118, 0.50012243, 0.35737022, 0.27081293, 0.18486778, 0.11043095, 0.08582272, 0.04946131, 0.04285015, 0.02901567]

x = np.array(x1)
y = np.array(y1)

# Interpolate the data using a cubic spline to "new_length" samples
new_length = 50
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)

但是在新生成的new_xnew_y数据集中,原始点被消除,仅保留第一个和最后一个值。我想保留原来的观点。

最佳答案

正确,linspace不会在x中生成任何值,除非您传递给它(x.min()x.max())。

我没有一个很好的答案,但是这是一种解决方法:

# Interpolate the data using a cubic spline to "new_length" samples
new_length = 50
interpolated_x = np.linspace(x.min(), x.max(), new_length - len(x) + 2)
new_x = np.sort(np.append(interpolated_x, x[1:-1]))  # include the original points
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)

该代码使用:
  • np.linspace创建我们需要的尽可能多的额外点
  • np.append将多余点的数组与x中的原始点组合在一起
  • np.sort将组合数组按
  • 的顺序放置

    关于Python在样条插值中保留点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11884195/

    10-11 22:46
    查看更多