简体   繁体   English

ZeroDivisionError:浮点除以零使用 scipy.interpolate.rbf('cubic")

[英]ZeroDivisionError: float division by zero USING scipy.interpolate.rbf('cubic")

i am trying to interpolate missing values using scipy library and specifically rbf('cubic').我正在尝试使用 scipy 库,特别是 rbf('cubic') 插入缺失值。 But i get the following error:但我收到以下错误:

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]):
                k+=1
                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])
    

 

i take in consideration a specific amount of data points to use for interpolation.我考虑了用于插值的特定数量的数据点。 Also, is there any other way to make it faster?另外,有没有其他方法可以让它更快? thanks谢谢

UPDATE As i wrote in the "ANSWER" section, i solved my problem.更新正如我在“答案”部分中所写,我解决了我的问题。 the only think that remained is if there is any way to make it faster.剩下的唯一想法是是否有任何方法可以使其更快。 i think the problem is the interpolation part我认为问题是插值部分

So i realised the problem and i fixed it.所以我意识到了这个问题并修复了它。 In my code i calculate the distance between the nan point (which i want to interpolate) and all the other.在我的代码中,我计算了 nan 点(我想插值)和所有其他点之间的距离。 At first i didn't have in mind about so many nan values in my dataset and also i thought that each time i fell on a 'nan' value, by increasing the value by 1 of the variable, which i use in a for-loop to pick the points for interpolation ( specifically: for i in range(nval): ), would maintain the amount of points for interpolation.起初我并没有想到我的数据集中有这么多的 nan 值,而且我认为每次我遇到一个“nan”值时,通过将变量的值增加 1,我在一个 for-循环选择插值点(特别是:for i in range(nval): ),将保持插值点的数量。 But it didnt.但它没有。 So i just added the following code in order to build an array that from a sorted list kept only the points with values(not nan)所以我只是添加了以下代码,以便从排序列表中构建一个数组,只保留具有值的点(不是 nan)

ex=[0,0,0]
for i in range(len(edist)):
    if np.isnan(edist[i][0][3])==False:
        ex=np.vstack((ex,[edist[i][0][1],edist[i][0][2],edist[i][0][3]]))
        
ex=ex[1:,:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM