用Python进行样条插值 [英] Spline Interpolation with Python
问题描述
我编写了以下代码来执行样条插值:
I wrote the following code to perform a spline interpolation:
import numpy as np
import scipy as sp
x1 = [1., 0.88, 0.67, 0.50, 0.35, 0.27, 0.18, 0.11, 0.08, 0.04, 0.04, 0.02]
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]
x = np.array(x1)
y = np.array(y1)
new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
但是我得到了:
ValueError: A value in x_new is below the interpolation range.
在interpolate.py
任何帮助将不胜感激.
推荐答案
来自有关scipy.interpolate.interp1d的科学文档:
scipy.interpolate.interp1d(x,y,kind ='linear',axis = -1,copy = True,bounds_error = True,fill_value = np.nan)
scipy.interpolate.interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=True, fill_value=np.nan)
x:类似array_.一维数组,其单调递增的实数值.
x : array_like. A 1-D array of monotonically increasing real values.
...
问题在于x值不是单调递增.实际上,它们正在单调减少.让我知道这是否有效,以及它是否仍在您要寻找的计算中.:
The problem is that the x values are not monotonically increasing. In fact they are monotonically decreasing. Let me know if this works and if its still the computation you are looking for.:
import numpy as np
import scipy as sp
from scipy.interpolate import interp1d
x1 = sorted([1., 0.88, 0.67, 0.50, 0.35, 0.27, 0.18, 0.11, 0.08, 0.04, 0.04, 0.02])
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]
new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
这篇关于用Python进行样条插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!