我在圖中所示的階梯狀的方式,而我試圖插值與Python與MWE data,但我得到的錯誤:如何在Python中使用類似步驟的值插值和平滑數據?
錯誤
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/polyint.py", line 54, in __call__ y = self._evaluate(x) File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 448, in _evaluate out_of_bounds = self._check_bounds(x_new) File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 478, in _check_bounds raise ValueError("A value in x_new is above the interpolation " ValueError: A value in x_new is above the interpolation range.
MWE
import numpy as np
from scipy import optimize, interpolate
from scipy.interpolate import interp1d
import time
with open('./pdf_data.dat', "r") as data:
while True:
line = data.readline()
if not line.startswith('#'):
break
data_header = [i for i in line.strip().split('\t') if i]
_data_ = np.genfromtxt(data, names = data_header, dtype = None, delimiter = '\t')
_data_.dtype.names = [j.replace('_', ' ') for j in _data_.dtype.names]
x = _data_['X']
y = _data_['Y']
interp_fn = interp1d(x, y)
x, index = np.unique(x, return_index = True)
pdf_interp = interp_fn(x)
如何內插和平滑Python中的階躍式值?這樣我就可以得到一條平滑的曲線。
它看起來像你沒有包括整個錯誤回溯 - 它只能追溯到'scipy/interpolate/polyint.py',並且不會顯示錯誤發生在* your code的哪裏。當你在你列出的代碼的末尾調用'interp_fn'時,還是在別的地方? –
當你構建你的'interp1d'對象時,你可以傳入'bounds_error = False,fill_value =「extrapolate」',然後而不是抱怨超出範圍的值你會得到外推。這是否是一個好主意取決於它們實際上有多遠。 –
似乎你的'np.unique'在這裏沒有效果 – gdlmx