【问题标题】:Scipy interpolation on a numpy arraynumpy 数组上的 Scipy 插值
【发布时间】:2010-06-16 20:42:00
【问题描述】:

我有一个按以下方式定义的查找表:

       | <1    2    3    4    5+
-------|----------------------------
<10000 | 3.6   6.5  9.1  11.5 13.8
20000  | 3.9   7.3  10.0 13.1 15.9
20000+ | 4.5   9.2  12.2 14.8 18.2


TR_ua1 = np.array([ [3.6, 6.5, 9.1, 11.5, 13.8],
                    [3.9, 7.3, 10.0, 13.1, 15.9],
                    [4.5, 9.2, 12.2, 14.8, 18.2] ])
  • 标题行元素为 (hh)
  • 标题列 (inc) 元素为

用户将输入一个值示例 (1.3, 25,000)、(0.2, 50,000) 等。 scipy.interpolate() 应该进行插值以确定正确的值。

目前,我能做到这一点的唯一方法是使用一堆if/elifs,如下所示。我很确定有更好、更有效的方法来做到这一点

这是我目前所得到的:

import numpy as np
from scipy import interpolate

if (ua == 1):
    if (inc <= low_inc):  # low_inc = 10,000
      if (hh <= 1):
        return TR_ua1[0][0]
      elif (hh >= 1 & hh < 2):
        return interpolate( (1, 2), (TR_ua1[0][1], TR_ua1[0][2]) )

【问题讨论】:

  • 感谢您的澄清!我在下面更新了我的答案。我认为它现在正是你想要的。

标签: python numpy scipy interpolation


【解决方案1】:

编辑:更新内容以反映您在上面的说明。你的问题现在更清楚了,谢谢!

基本上,您只是想在任意点插入一个二维数组。

scipy.ndimage.map_coordinates 是你想要的......

据我了解您的问题,您有一个“z”值的二维数组,范围从某个 xmin 到 xmax,以及每个方向的 ymin 到 ymax。

您想要从数组边缘返回值的边界坐标之外的任何内容。

map_coordinates 有几个选项来处理网格边界之外的点,但它们都不能完全满足您的要求。相反,我们可以将边界之外的任何东西都设置在边缘,并像往常一样使用 map_coordinates。

所以,要使用 map_coordinates,你需要转动你的实际坐标:

       | <1    2    3    4    5+
-------|----------------------------
<10000 | 3.6   6.5  9.1  11.5 13.8
20000  | 3.9   7.3  10.0 13.1 15.9
20000+ | 4.5   9.2  12.2 14.8 18.2

进入索引坐标:

       |  0     1    2    3    4
-------|----------------------------
   0   | 3.6   6.5  9.1  11.5 13.8
   1   | 3.9   7.3  10.0 13.1 15.9
   2   | 4.5   9.2  12.2 14.8 18.2

请注意,您的边界在每个方向上的表现都不同...在 x 方向上,事情表现得很顺利,但在 y 方向上,您要求“硬”中断,其中 y=20000 --> 3.9,但 y=20000.000001 --> 4.5。

举个例子:

import numpy as np
from scipy.ndimage import map_coordinates

#-- Setup ---------------------------
z = np.array([ [3.6, 6.5, 9.1, 11.5, 13.8],
               [3.9, 7.3, 10.0, 13.1, 15.9],
               [4.5, 9.2, 12.2, 14.8, 18.2] ])
ny, nx = z.shape
xmin, xmax = 1, 5
ymin, ymax = 10000, 20000

# Points we want to interpolate at
x1, y1 = 1.3, 25000
x2, y2 = 0.2, 50000
x3, y3 = 2.5, 15000

# To make our lives easier down the road, let's 
# turn these into arrays of x & y coords
xi = np.array([x1, x2, x3], dtype=np.float)
yi = np.array([y1, y2, y3], dtype=np.float)

# Now, we'll set points outside the boundaries to lie along an edge
xi[xi > xmax] = xmax
xi[xi < xmin] = xmin

# To deal with the "hard" break, we'll have to treat y differently, 
# so we're ust setting the min here...
yi[yi < ymin] = ymin

# We need to convert these to (float) indicies
#   (xi should range from 0 to (nx - 1), etc)
xi = (nx - 1) * (xi - xmin) / (xmax - xmin)

# Deal with the "hard" break in the y-direction
yi = (ny - 2) * (yi - ymin) / (ymax - ymin)
yi[yi > 1] = 2.0

# Now we actually interpolate
# map_coordinates does cubic interpolation by default, 
# use "order=1" to preform bilinear interpolation instead...
z1, z2, z3 = map_coordinates(z, [yi, xi])

# Display the results
for X, Y, Z in zip((x1, x2, x3), (y1, y2, y3), (z1, z2, z3)):
    print X, ',', Y, '-->', Z

这会产生:

1.3 , 25000 --> 5.1807375
0.2 , 50000 --> 4.5
2.5 , 15000 --> 8.12252371652

希望对您有所帮助...

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2018-01-26
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 2016-02-07
    相关资源
    最近更新 更多