【问题标题】:Python3: Interpolation for certain points (not grid)Python3:某些点的插值(不是网格)
【发布时间】:2020-04-19 22:18:38
【问题描述】:

我有一个格式为(x y z)的文本文件(data.txt):

例子:

1.1 0.0 12
1.2 2.2 15
1.3 5.5 30
2.6 1.0 20
2.1 4.8 31
3.9 0.5 12
...

我使用以下代码获取此数据:

x,y,z = np.genfromtxt(r'data.txt', unpack=True)

我想做一个插值来找到某些点的 z 值。示例:

(1.0; 1.0), (1.9; 3.05), (1.4; 4.0), (2.2; 0.9), (2.4; 2.1), (2.9; 3.0), (3.0; 1.8)

我所做的是用这些点创建一个文本文件(“points.txt”)并加载它们

positions_path = np.loadtxt("points.txt")     #(x, y)

这是我计算插值的代码:

intial_points = np.transpose(np.vstack((x, y)))   #array with the x and y points from data.txt



L1 = []  #empty list 

for i in range(len(positions_path)):


    n1=float(interpolate.griddata(intial_points, z, (positions_path[i,0], positions_path[i,1] ), method='linear'))   


    #L1 saves  x, y, z  
    L1.append([positions_path[i,0], positions_path[i,1], n1])

这可以完成工作。问题是对于更多的点是不可行的。 我基本上是在为每个点做一个插值。如果我有 10000 分,我将不得不做 10000 次。

我知道我可以创建一个特定的网格并获取离散网格的值。但是,如果我得到像 (0.001, 0.25) 这样的点的值怎么办?我必须有非常精细的网格。这就是我避免使用网格的原因。

有没有办法一次对 1000 个点进行插值?

谢谢!

【问题讨论】:

    标签: python python-3.x numpy linear-interpolation


    【解决方案1】:

    解决方法很简单:

    
    n1=interpolate.griddata(intial_points, z, (positions_path[:,0], positions_path[:,0] ), method='linear')
    

    它返回一个数组,其中包含 points.txt 中点的“z”值。不需要网格。 谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2016-05-17
      • 2013-09-27
      • 1970-01-01
      相关资源
      最近更新 更多