Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
i am trying to interpolate missing values using scipy library and specifically 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]):
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. 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. 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)
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:,:]
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.