My Coding > Programming language > Python > Python libraries and packages > Python SciPy > SciPy: Interpolation 3 dimension data

SciPy: Interpolation 3 dimension data

In many calculations in the 1, 2 or 3 dimensional data we only have calculated values in the points of our

calculating grid, but for the further analysis we need to know, at least approximated values in any points

within the range of our domain.

In this article I will show, how to interpolate 3D domain with SciPy library.

Domain for interpolation

First of all, load libraries

import numpy as np
from scipy.interpolate import RegularGridInterpolator

At the second step we need to make a function to calculate our test function and create domain for


# this is our test function
def f(x, y, z):
return 2 * x**2 + 3 * y**2 + z
X, Y, Z = np.mgrid[0:5:10j, 0:10:20j, 0:20:40j]
data = f(X, Y, Z)

for working with RegularGridInterpolator it is necessary to use mgrid, or meshgrid with options

indexing='ij', sparse=True.

Initializing and using Interpolator

When data is calculated in every point, it is necessary to initialize interpolator. At the moment, in 3D

space, only "linear" and "nearest"methods are available.

interpolator = RegularGridInterpolator((X[:,0,0], Y[0,:,0], Z[0,0,:]), data, method='linear')

When the interpolator is initialized, we can ise it for calculation values for any points within our domain.

pts = np.array([[2.23, 3.58, 10.3],
[4.34, 8.22, 17.21]])
# [ 58.86455114 257.7785469 ]
print([f(2.23, 3.58, 10.3), f(4.34, 8.22, 17.21)])
# [58.69499999999999, 257.5864]

The interpolation looks pretty close to real values.

Published: 2022-10-10 22:24:57

Last 10 artitles

9 popular artitles

© 2020 -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.