Python中2个列表的3D插值 [英] 3D interpolation of 2 lists in python

查看:321
本文介绍了Python中2个列表的3D插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我从两个单独的导入文件中提取了两组数据,这两个文件都被导入到python中,并分别放置在两个单独的列表中,如下所示:

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:

列表1的格式为:

(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]]

列表2的格式为:

(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]]

从这两个列表中,我需要使用坐标来创建第三个列表,其中包含一个节点值及其对应的温度.如果所有x y和z坐标都匹配,则此任务是一个简单的字典函数,但是与我使用的数据并不总是这种情况.

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.

例如,如果在列表1中,我在列表末尾添加了一个新条目,节点号9;

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]

现在我发现自己的节点号没有相应的温度.在这一点上,将需要在列表2上执行插值功能,以提供与该节点相关的温度.通过基本的3d插值计算,我算出该温度将为97.9,因此我的最终输出列表将如下所示:

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:

输出列表:

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

我是python的新手,因此正努力寻找该插值问题的解决方案,我已经研究了数周之久如何做到这一点,但仍未能找到解决方案.

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,

谢谢

推荐答案

scipy中有很多插值例程,但是在2维以上时,大多数插值例程仅提供线性和最近邻插值-这可能不足以满足您的要求.使用.

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.

所有插值例程均列在插值页上scipy docs区域.您可以直接忽略mnivariate以及1D和2D样条曲线部分-您需要

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.

这里有9个函数,分为结构化数据和非结构化数据:

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

非结构化数据:

griddata(points, values, xi[, method, ...])插值非结构化 D维数据.

Unstructured data:

griddata(points, values, xi[, method, ...]) Interpolate unstructured D-dimensional data.

LinearNDInterpolator(points, values[, ...]) N维上的分段线性插值.

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

NearestNDInterpolator(points, values) N维中的最近邻插值.

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

CloughTocher2DInterpolator(points, values[, tol])二维C1平滑,曲率最小的分段三次插值.

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

Rbf(*args)一类用于n维分散数据的径向基函数逼近/插值.

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

interp2d(x, y, z[, kind, copy, ...])在2D网格上进行插值.对于>

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

interpn(points, values, xi[, method, ...])多维 在常规网格上进行插值.

interpn(points, values, xi[, method, ...]) Multidimensional interpolation on regular grids.

RegularGridInterpolator(points, values[, ...])在任意尺寸的常规网格上进行插值

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

RectBivariateSpline(x, y, z[, bbox, kx, ky, s])矩形网格上的二元样条曲线逼近.

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.

这些函数的工作方式是,将它们传递数据,即x,y,z坐标,以及在这些点处的相应值,然后它们返回一个函数,使您可以在任意位置获取一个点.

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.

我建议 Rbf 在这里起作用,但是从我的角度来看,它是唯一的nD选项,它不将您限制为线性或最近邻插值.

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.

例如,您有两个列表:

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)

尝试类似的事情.

这篇关于Python中2个列表的3D插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆