你X
值是顛倒的,scipy.interpolate.spline
要求自變量是單調遞增,並此方法已棄用 - 請使用interp1d
(請參閱下文)。
>>> from scipy.interpolate import spline
>>> import numpy as np
>>> X = [736176.0, 736175.0, 736174.0] # <-- your original X is decreasing
>>> Y = [711.74, 730.0, 698.0]
>>> Xsmooth = np.linspace(736174.0, 736176.0, 10)
>>> spline(X, Y, Xsmooth)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
反向X
和Y
第一,它的工作原理
>>> spline(
... list(reversed(X)), # <-- reverse order of X so also
... list(reversed(Y)), # <-- reverse order of Y to match
... Xsmooth
...)
array([ 698. , 262.18297973, 159.33767533, 293.62017489,
569.18656683, 890.19293934, 1160.79538066, 1285.149979 ,
1167.41282274, 711.74 ])
需要注意的是很多樣條插值方法需要X
是單調遞增:
x
: (N,) array_like - 1-D array of independent input data. Must be increasing.
x
: (N,) array_like - Input dimension of data points – must be increasing
的scipy.interpolate.spline
默認順序爲立方體。由於只有3個數據點,因此三次樣條(order=3
)與二次樣條(order=2
)之間存在較大差異。下圖顯示了不同階次樣條之間的差異;注:100分用於平滑擬合曲線更多。
![scipy.interpolate.spline](https://i.stack.imgur.com/m4DCw.png)
的文檔scipy.interpolate.spline
是模糊的,並表明,它可能不被支持。例如,它不在scipy.interpolate
main page或interploation tutorial上列出。該source for spline
表明,它實際上調用spleval
並且被Additional Tools下列爲splmake
:
Functions existing for backward compatibility (should not be used in new code).
我會按照cricket_007的建議,並使用interp1d
。它是當前建議的方法,它是很好的與detailed examples in both the tutorial和API記錄,並且它允許獨立變量是未排序的(任何順序)由缺省值(參見API assume_sorted
參數)。
>>> from scipy.interpolate import interp1d
>>> f = interp1d(X, Y, kind='quadratic')
>>> f(Xsmooth)
array([ 711.74 , 720.14123457, 726.06049383, 729.49777778,
730.45308642, 728.92641975, 724.91777778, 718.4271605 ,
709.4545679 , 698. ])
此外,如果數據缺乏等級,則會引發錯誤。
>>> f = interp1d(X, Y, kind='cubic')
ValueError: x and y arrays must have at least 4 entries
我只是想一個例子,你的方法爲我工作。嘗試調試每一行。 Xnew.min()和Xnew.max()是什麼值?發佈更多細節。 'X','X_smooth','Xnew'的值是多少?對於[在執行期間在IPython中調試,請嘗試使用'%debug'](https://ipython.org/ipython-doc/1/interactive/tutorial.html#debugging)添加到[添加斷點](https:// docs。 python.org/2/library/pdb.html#debugger-commands),或者可能使用[logging](https://docs.python.org/2/library/logging.html)在執行期間查看中間值。 –
我編輯了問題並添加了變量 – AimiHat
從您的輸出中我推斷出問題在於樣條曲線,而不是日期時間。你能改說你的問題嗎?也許可以用一組較小的數字來嘗試你的問題,比如10而不是300,這樣可以更容易地在SO中顯示,並且更容易調試。仔細檢查'np.splie'文檔 –