10

次のように定義されたルックアップテーブルがあります。

       | <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)<1,2,3,4,5+です。
  • ヘッダー列(inc)要素は<10000、20000、20001+です

ユーザーは、値の例(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]) )
4

1 に答える 1

8

編集:上記の説明を反映するように更新しました。あなたの質問は今、はるかに明確になっています、ありがとう!

基本的には、任意のポイントで2D配列を補間したいだけです。

scipy.ndimage.map_coordinatesはあなたが望むものです...

私があなたの質問を理解しているように、あなたはそれぞれの方向にいくつかのxminからxmax、そしてyminからymaxの範囲の「z」値の2D配列を持っています。

配列の端から値を返したい境界座標の外側にあるもの。

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

うまくいけば、それが役立つ...

于 2010-06-17T00:03:05.057 に答える