Python 使用scipy.interpolate.griddata时出现零分割错误

Python 使用scipy.interpolate.griddata时出现零分割错误,python,numpy,scipy,interpolation,Python,Numpy,Scipy,Interpolation,我从以下代码中得到了一个zero错误: #stacking the array into a complex array allows np.unique to choose #truely unique points. We also keep a handle on the unique indices #to allow us to index `self` in the same order. unique_points,index = np.unique(xdata[mask]+1j

我从以下代码中得到了一个
zero错误

#stacking the array into a complex array allows np.unique to choose
#truely unique points.  We also keep a handle on the unique indices
#to allow us to index `self` in the same order.
unique_points,index = np.unique(xdata[mask]+1j*ydata[mask],
                         return_index=True)
#Now we break it into the data structure we need.
points = np.column_stack((unique_points.real,unique_points.imag))

xx1,xx2 = self.meta['rcm_xx1'],self.meta['rcm_xx2']
yy1 = self.meta['rcm_yy2']
gx = np.arange(xx1,xx2+dx,dx)
gy = np.arange(-yy1,yy1+dy,dy)
GX,GY = np.meshgrid(gx,gy)

xi = np.column_stack((GX.ravel(),GY.ravel()))
gdata = griddata(points,self[mask][index],xi,method='linear',
                           fill_value=np.nan)
这里,
xdata
ydata
self
都是具有相同形状和
dtype=np.float32
的2D
numpy.ndarray
s(或其子类)
mask
是一个具有相同形状和
dtype=bool
的2d
ndarray
。这里有一个链接,供那些想细读这本书的人使用

最初,
xdata
ydata
是从具有4点模具的非均匀圆柱形网格中派生出来的——我认为错误可能来自同一点被多次定义的事实,因此我按照建议使输入点集唯一。不幸的是,这似乎没有帮助。完整回溯是:

Traceback (most recent call last):
  File "/xxxxxxx/rcm.py", line 428, in <module>
    x[...,1].to_pz0()
  File "/xxxxxxx/rcm.py", line 285, in to_pz0
    fill_value=fill_value)
  File "/usr/local/lib/python2.7/site-packages/scipy/interpolate/ndgriddata.py", line 183, in griddata
    ip = LinearNDInterpolator(points, values, fill_value=fill_value)
  File "interpnd.pyx", line 192, in scipy.interpolate.interpnd.LinearNDInterpolator.__init__ (scipy/interpolate/interpnd.c:2935)
  File "qhull.pyx", line 996, in scipy.spatial.qhull.Delaunay.__init__ (scipy/spatial/qhull.c:6607)
  File "qhull.pyx", line 183, in scipy.spatial.qhull._construct_delaunay (scipy/spatial/qhull.c:1919)
ZeroDivisionError: float division
回溯(最近一次呼叫最后一次):
文件“/xxxxxxx/rcm.py”,第428行,输入
x[…,1]。到_pz0()
文件“/xxxxxxx/rcm.py”,第285行,输入至_pz0
填充值=填充值)
griddata中的文件“/usr/local/lib/python2.7/site packages/scipy/interpolate/ndgriddata.py”,第183行
ip=线性插值(点、值、填充值=填充值)
文件“interpnd.pyx”,第192行,在scipy.interpolate.interpnd.LinearNDInterpolator.\uuuu init\uuuu中(scipy/interpolate/interpnd.c:2935)
文件“qhull.pyx”,第996行,位于scipy.spatial.qhull.Delaunay.\uuuu init\uuuu(scipy/spatial/qhull.c:6607)中
文件“qhull.pyx”,第183行,在scipy.spatial.qhull.\u construct\u delaunay中(scipy/spatial/qhull.c:1919)
零除法错误:浮点除法

值得一提的是,如果我使用“最近的”方法,代码“有效”(没有例外)。

您要插值的数据的相对范围(例如最大值和最小值)是多少?我完全是在猜测,但我敢打赌你的问题来自
griddata
使用的delaunay三角剖分中的“薄三角形”(类似于:在极端情况下,你会得到除零的结果,而不仅仅是看起来不稳定的插值)另一个解决方法是使用径向基函数代替delaunay三角剖分:@JoeKington——如果你问的是
xx1
xx2
yy1
,值分别是
-10,20,20
,所以我没有一个非常极端的总宽高比。产生此错误的实际数据在
xdata
ydata
中的峰-峰差异分别为17.68和21.21。。。结果是:嗯。。。真讨厌。。。您使用的是哪个版本的numpy/scipy?我想后端的Qhull库也可能有所不同。。。(我在scipy版本0.11.0,numpy 1.6.2)结果是升级到scipy 0.12.0就成功了。。。(奇怪)。。。