简体   繁体   中英

Interpolation of gridded data

I was hoping someone could help me with a problem that Ive been having (I'm still very new to python). I have been trying to interpolate data from a 50x4 array that is read from an excel sheet seen below.

 [ 60.      0.     23.88   22.38 ]  
 [ 60.      5.     19.508  28.2  ]  
 [ 60.     10.     16.9    32.23 ]  
 [ 60.     15.     15.4    34.03 ]  
 [ 60.     20.     14.4    35.12 ]  
 [ 60.     25.     13.66   36.02 ]  
 [ 60.     30.     13.14   36.61 ]  
 [ 60.     35.     12.69   37.14 ]  
 [ 60.     40.     12.53   37.56 ]  
 [ 60.     50.     12.33   38.32 ]  
 [ 70.      0.     19.3    21.34 ]  
 [ 70.      5.     16.06   25.37 ]  
 [ 70.     10.     13.74   28.08 ]  
 [ 70.     15.     12.33   40.07 ]  
 [ 70.     20.     11.45   41.78 ]  
 [ 70.     25.     10.77   42.8  ]

etc...

What I'm trying to achieve is to enter 2 values (say 65 and 12) which correspond to interpolated values in the 1st and 2nd column, and it would return the interpolated values for columns 3 and 4. I managed to get it working using the griddata function in matlab. However no luck in python yet.

Thanks in advance

I think that scipy.interpolate might do the same (or at least similar) as MATLAB's Griddata . Below code uses the Radial Basis Function for interpolation. I've only made the example for your column 3 as z-axis.

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt
x = np.array([60] * 10 + [70] * 6)
y = np.array([0,5,10,15,20,25,30,35,40,50,0,5,10,15,20,25])
z = np.array([23.88, 19.508, 16.9, 15.4, 14.4, 13.66, 13.14, 12.69, 12.53, 12.33, 19.3, 16.06, 13.74, 12.33, 11.45, 10.77])
x_ticks = np.linspace(60, 70, 11)
y_ticks = np.linspace(0, 50, 51)    
XI, YI = np.meshgrid(x_ticks, y_ticks)

rbf = interpolate.Rbf(x, y, z, epsilon=2)
ZI = rbf(XI, YI)
print(ZI[np.argwhere(y_ticks==12)[0][0], np.argwhere(x_ticks==65)[0][0]])

>>> 14.222288614849171

Be aware that the result is ZI[y,x] , not ZI[x,y] . Also be aware that your ticks must contain the x and y values you query, otherwise you'll get an IndexError.

Maybe you can build up on that solution depending on your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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