2013-07-04 76 views
2

我一直在通過documentation for scipy.interpolate上的例子,我無法弄清楚我將如何採集不均勻間隔的數據並插入它,因爲所有的教程都使用了linspaces - 它們均勻間隔。用SciPy插值不均勻數據

例如,我有一些數據傳播,像這樣:

[--1--4-----5-3-22---55-] 

其中每個-代表缺失值。

我將如何去擬合插值函數使用scipy.interpolate

+0

您是否嘗試過設置你的'x'矢量等於您擁有的數據等於這些索引值的指數,和你的'y'載體?我不確定這是否適用於您的情況,但是這是想到的。 – Engineero

回答

3

interpolate.interp1d適用於不均勻間隔的數據。例如,

import re 
import numpy as np 
import scipy.interpolate as interpolate 
import matplotlib.pyplot as plt 

text = '--1--4-----5-3-22---55-' 
parts = [c for c in re.split(r'(-|\d+)', text) if c] 
data = np.array([(x, int(y)) for x, y in enumerate(parts) if y != '-']) 
x, y = data.T 
f = interpolate.interp1d(x, y, kind='cubic') 

newx = np.linspace(x.min(), x.max()) 
newy = f(newx) 
plt.plot(newx, newy) 
plt.scatter(x, y, s=20) 
plt.show() 

產生 enter image description here

+0

謝謝!作爲後續,我有可能擴展內插函數超過數據嗎? 當我嘗試通過在linspace(0,some_number> x.max())上運行它來執行此操作時,我得到一個ValueError,表示插值範圍上方有一個x_new值。 – zmjjmz

+1

@zmjjmz其在文檔中的位置:只需添加'bounds_error = False' –