python多维插值,Python中2个列表的3D插值

hi i have two sets of data taken from two seperate import files which are both being imported into python and have been placed in two seperate lists as follows:

list 1 is of the form:

(node, x coordinate, y coordinate, z coordinate)

example list 1: [[1,0,0,0],[2,1,0,0],[3,0,1,0],[4,1,1,0],[5,0,0,1],[6,1,0,1],[7,0,1,1],[8,1,1,1]]

list 2 is in the form:

(x coordinate, y coordinate, z coordinate, temperature)

example list 2: [[0,0,0,100],[1,0,0,90],[0,1,0,85],[1,1,0,110],[0,0,1,115],[1,0,1,118],[0,1,1,100],[1,1,11,96]]

from these two lists I need to use the coordinates to create a third list which contains a node value and its corresponding temperature. This task is a simple dictionary function if all the x y and z coordinates match up however with the data i am working with this will not always be the case.

For example if in list 1 I add a new entry at the end of the list, node number 9;

new entry at end of list 1 [9, 0.5, 0.9, 0.25]

Now I find myself with a node number with no corresponding temperature. At this point an interpolation function will need to be performed on list 2 to give me the temperature related to this node. Through basic 3d interpolation calculations I have worked out that this temperature will be 97.9 therefore my final output list would look like this:

Output list:

(node, temperature)

Output list: [[1,100],[2,90],[3,85],[4,110],[5,115],[6,118],[7,100],[8,96],[9,97.9]]

I am reasonably new to python so am struggling to find a solution to this interpolation problem, I have been researching how to do this for a number of weeks now and have still not been able to find a solution.

Any help would be greatly greatly appreciated,

Thanks

解决方案

There are quite a few interpolation routines in scipy, but above 2 dimensions, most of them only offer linear and nearest neighbour interpolation - which might not be sufficient for your use.

All of the interpolation routiens are listed on the interplation page of the scipy docs area. Straight away you can ignore the mnivariate, and 1D and 2D spline sections - you want the multivariate section.

There are 9 functions here, split into structured and unstructed data:

Unstructured data:

griddata(points, values, xi[, method, ...]) Interpolate unstructured

D-dimensional data.

LinearNDInterpolator(points, values[, ...]) Piecewise linear interpolant in N dimensions.

NearestNDInterpolator(points, values) Nearest-neighbour interpolation in N dimensions.

CloughTocher2DInterpolator(points, values[, tol]) Piecewise cubic, C1 smooth, curvature-minimizing interpolant in 2D.

Rbf(*args) A class for radial basis function approximation/interpolation of n-dimensional scattered data.

interp2d(x, y, z[, kind, copy, ...]) Interpolate over a 2-D grid. For >

data on a grid:

interpn(points, values, xi[, method, ...]) Multidimensional

interpolation on regular grids.

RegularGridInterpolator(points, values[, ...]) Interpolation on a regular grid in arbitrary dimensions

RectBivariateSpline(x, y, z[, bbox, kx, ky, s]) Bivariate spline approximation over a rectangular mesh.

plus an additional one in the see also section, though we'll ignore that.

You should read how they each work, it might help you understand a little better.

The way these functions work though, is that you pass them data i.e. x,y,z coords, and the corresponding values at those points, and they then return a function which allows you to get a point at any location.

I would recommend the Rbf function here though, as from what i can see it's the only nD option which does not limit you to linear or nearest neighbour interpolation.

For example, you have two lists:

node_locations = [(node, x_coord, y_coord, z_coord), ...]

temp_data = [(x0, y0, z0, temp0), (x1, y1, z1, temp1), ...]

xs, ys, zs, temps = zip(*teemp_data) # This will unpack your data into columns, rather than rows.

from scipy.interpolate import Rbf

rbfi = Rbf(xs, ys, zs, temps)

# I don't know how you want your output data, so i'm just dumping it in a dictionary.

node_data = {}

for node, x, y, z in node_locations:

node_data[node] = rbfi(x, y, z)

Try something like that.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值