相关文章推荐
长情的电池  ·  Colorectal Cancer ...·  1 年前    · 
快乐的大蒜  ·  终于搞懂了Flex:1 ...·  2 年前    · 

ZeroDivisionError: float除以0 USING scipy.interpolate.rbf('cubic")

0 人关注

我试图用scipy库,特别是rbf('cubic')来插值缺失值。但我得到了以下错误。

Traceback (most recent call last):
  File "C:\Users\St\Desktop\py_magn\inter.py", line 89, in <module>
    rbfp = Rbf(xn ,yn, magn, function='cubic') #PARAMETERS
  File "C:\Users\St\AppData\Local\Programs\Python\Python38\lib\site-packages\scipy\interpolate\rbf.py", line 239, in __init__
    self.epsilon = np.power(np.prod(edges)/self.N, 1.0/edges.size)
ZeroDivisionError: float division by zero

my code is the following:

x, y, mag = df[:,0], df[:,1], df[:,3]
emptyInd = np.where(np.isnan(mag))
print(emptyInd[0])
#----------------------------------------------------------------------------------------------------
def eucl_dist(pnt,x,y,mag):
    value = []
    dist = 0.0
    typ = [('Eucli Dist', float), ('x', float), ('y', float), ('Magn', float)]
    for i in range(len(x)):
        dist = sqrt((pnt[0]-x[i])**2 + (pnt[1]-y[i])**2)
        res = [dist, x[i], y[i], mag[i]]
        value.append(res)
    value = np.vstack(value)
    return value,typ
#--------------------------------------------------------------------------------
xinter=[]
yinter=[]
magInter=[]
neigh = []
if len(emptyInd)!=0 :
    listOfEMPval = list(zip(emptyInd[0])) #, emptyInd[1]))
    for ind in listOfEMPval:
        xn=[]
        yn=[]
        magn=[]
        xinter = np.take(x,ind)
        yinter = np.take(y,ind)
        edist, typ = eucl_dist((xinter, yinter), x, y, mag)
        edist = rf.unstructured_to_structured(edist, np.dtype(typ))
        indx = np.argsort(edist, order='Eucli Dist')
        edist = np.reshape([edist[i] for i in indx],(len(edist), 1))
        sz=182
        for k in range(sz):
            if np.isnan(edist[k][0][3]):
                sz+=1
            else:
                xn = np.append(xn, edist[k][0][1])
                yn = np.append(yn, edist[k][0][2])
                magn = np.append(magn, edist[k][0][3])
        rbfp = Rbf(xn ,yn, magn, function='cubic') #PARAMETERS
        magInter = np.append(magInter, rbfp(xinter,yinter)) #INTERPOLATION
    for i in range(len(listOfEMPval)):
        np.put(mag, listOfEMPval[i], magInter[i])
正如我在 "答案 "部分所写,我解决了我的问题。唯一的想法是,是否有任何方法使它更快。

4 个评论
快速提示:当你张贴整个代码时,你有较低的机会让人看它 -- 它有点令人生畏。你最好把它缩减到显示相同错误的最小可能的代码。
@RafazZ 我这样做是为了得到整张照片,但谢谢你的提示。
x (或 y )的值彼此相等时,插值时除以零就很容易发生。
@JohanC 问题是,由于它在插值一个纳米值,输入的点数是1个x,1个y和1个z。
python
scipy
interpolation
TheGame
TheGame
发布于 2021-02-14
1 个回答
TheGame
TheGame
发布于 2021-02-15
已采纳
0 人赞同

所以我意识到了这个问题,并把它解决了。在我的代码中,我计算了纳米点(我想插值的)和所有其他点之间的距离。起初我没有想到我的数据集中有这么多的纳米值,而且我认为每次我落在一个 "纳米 "值上时,通过将变量的值增加1,我在for-loop中用来挑选插值的点(具体为:for i in range(nval):),就可以保持插值的点数。但它并没有。所以我添加了以下代码,以建立一个数组,从一个排序的列表中只保留有价值的点(不是南)。

ex=[0,0,0]
for i in range(len(edist)):