2013-07-04 74 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' –