0
$\begingroup$

I am trying to finish a series of interpolation functions. The problem is more related with organizing the data than how to do the interpolations.

Using the RegularGridInterpolator, I created this function:

def interp_3d(x,y,z,fxyz,x_desired,y_desired,z_desired):
    interp_func = RegularGridInterpolator((x,y,z), fxyz)
    return interp_func([x_desired,y_desired,z_desired])

It gets three one dimensional arrays (x,y,z) and the function answer (f(x,y,z)) to return the interpolating function. The problem I am having is related to the format the data is given to me and how the function receives it. The data comes to me as a table:

x   y   z   f(x,y,z)
26600   5000    0.05    0.01
26600   5000    0.10    0.02
26600   5000    0.15    0.03
26600   5000    0.20    0.04
26600   5000    0.25    0.05
26600   10000   0.05    0.01
26600   10000   0.10    0.02
26600   10000   0.15    0.03
26600   10000   0.20    0.04
40000   5000    0.00    0.00
40000   5000    0.05    0.01
40000   5000    0.10    0.02
40000   5000    0.15    0.03

And I have to organize the f(x,y,z) function in an array of fxyz = [[[],[],[],[]]].

  • How can I do it?

  • Do you guys recommend another method to create this interpolation function assuming that the data comes in this form?

  • Any recommendations?

$\endgroup$
  • 2
    $\begingroup$ Your data do not appear to be on a regular grid. In your example, you are missing 4 values for x=40000. Thus, you should be using an interpolator that does not assume that your data is defined over a grid. $\endgroup$ – nicoguaro Mar 27 at 12:06
2
$\begingroup$

As I mentioned in my comment, your data is not defined over a regular grid and that's why you should not be using that function.

One option available in scipy.interpolate is griddata, where you pass your points and values and interpolate in another set of points that you pass. None of them need to be regular. One caveat is that you are going to obtain the interpolation on the convex hull of your data (unless you use the Nearest neighbor method).

Following is an example.

import numpy as np
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Data
data = np.array([
        [2.66e+04, 5.00e+03, 5.00e-02, 1.00e-02],
        [2.66e+04, 5.00e+03, 1.00e-01, 2.00e-02],
        [2.66e+04, 5.00e+03, 1.50e-01, 3.00e-02],
        [2.66e+04, 5.00e+03, 2.00e-01, 4.00e-02],
        [2.66e+04, 5.00e+03, 2.50e-01, 5.00e-02],
        [2.66e+04, 1.00e+04, 5.00e-02, 1.00e-02],
        [2.66e+04, 1.00e+04, 1.00e-01, 2.00e-02],
        [2.66e+04, 1.00e+04, 1.50e-01, 3.00e-02],
        [2.66e+04, 1.00e+04, 2.00e-01, 4.00e-02],
        [4.00e+04, 5.00e+03, 0.00e+00, 0.00e+00],
        [4.00e+04, 5.00e+03, 5.00e-02, 1.00e-02],
        [4.00e+04, 5.00e+03, 1.00e-01, 2.00e-02],
        [4.00e+04, 5.00e+03, 1.50e-01, 3.00e-02]])
x, y, z, f = data.T

# Interpolation
grid_x, grid_y, grid_z = np.mgrid[26600:40000:5j, 5000:10000:5j, 0.05:0.20:5j]
grid_f = griddata(data[:, :3], f, (grid_x, grid_y, grid_z))

# Visualization
fig = plt.figure(figsize=(7, 3))
ax0 = fig.add_subplot(121, projection='3d')
ax0.scatter(x, y, z, c=f)
ax1 = fig.add_subplot(122, projection='3d')
ax1.scatter(grid_x.flatten(), grid_y.flatten(), grid_z.flatten(),
            c=grid_f.flatten())
plt.show()

Where you obtain the following result. At the left is your original dataset and the interpolated values are on the right.

enter image description here

$\endgroup$
  • $\begingroup$ Thank you ! Just a question. Whats the little j after the fives in the mgrid definitions ? grid_x, grid_y, grid_z = np.mgrid[26600:40000:5j, 5000:10000:5j, 0.05:0.20:5j] $\endgroup$ – LM_O Mar 27 at 20:56
  • 1
    $\begingroup$ @LM_O, that is an imaginary number. When used with mgrid for the third position it defines the number of subdivisions between the first two values. $\endgroup$ – nicoguaro Mar 27 at 20:58
  • $\begingroup$ What your opinion on using the Rbf interpolation in scipy ? f_xyz = Rbf(x,y,z,fxyz,function='multiquadric') $\endgroup$ – LM_O Mar 29 at 23:19
  • 1
    $\begingroup$ @LM_O, I can't remember using it. I suggest you ask another question for that matter. $\endgroup$ – nicoguaro Mar 29 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.