Python/Scipy插值(地图坐标)

Python/Scipy插值(地图坐标),python,numpy,scipy,interpolation,Python,Numpy,Scipy,Interpolation,我想用scipy做一些插值。我看过很多例子,但我没有找到我想要的 假设我有一些数据,其中row和column变量可以从0到1变化。每行和每列之间的增量变化并不总是相同的(见下文) 现在我希望能够获取一组x,y点,并确定插值。我知道我可以用地图坐标做这件事。我想知道是否有任何简单/聪明的方法可以将x,y值添加到数据数组中的适当索引中 例如,如果我输入x,y=0.60,0.25,那么我应该返回要插值的正确索引。在本例中,这将是1.0,1.0,因为0.60,0.25将精确映射到第二行和第二列。x=0.

我想用scipy做一些插值。我看过很多例子,但我没有找到我想要的

假设我有一些数据,其中row和column变量可以从0到1变化。每行和每列之间的增量变化并不总是相同的(见下文)

现在我希望能够获取一组x,y点,并确定插值。我知道我可以用地图坐标做这件事。我想知道是否有任何简单/聪明的方法可以将x,y值添加到数据数组中的适当索引中

例如,如果我输入x,y=0.60,0.25,那么我应该返回要插值的正确索引。在本例中,这将是1.0,1.0,因为0.60,0.25将精确映射到第二行和第二列。x=0.3将映射为0.5,因为它介于0.00和0.60之间

我知道如何得到我想要的结果,但我确信有一个非常快速/清晰的一两行程序(或已经存在的函数)可以做到这一点,使我的代码更加清晰。基本上,它需要在一些数组之间进行分段插值

下面是一个示例(主要基于中的代码)-我将TODO放在这个新函数的位置:

from scipy.ndimage import map_coordinates
from numpy import arange
import numpy as np
#            0.000,  0.175,  0.817,  1.000
z = array([ [ 3.6,    6.5,    9.1,    11.5],    # 0.0000
            [ 3.9,   -7.3,    10.0,   13.1],    # 0.2620
            [ 1.9,    8.3,   -15.0,  -12.1],    # 0.6121
            [-4.5,    9.2,    12.2,   14.8] ])  # 1.0000
ny, nx = z.shape
xmin, xmax = 0., 1.
ymin, ymax = 0., 1.

xrange = array([0.000,  0.175,  0.817,  1.000 ])
yrange = array([0.0000, 0.2620, 0.6121, 1.0000])

# Points we want to interpolate at
x1, y1 = 0.20, 0.45
x2, y2 = 0.30, 0.85
x3, y3 = 0.95, 1.00

# 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

yi[yi > ymax] = ymax
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)
yi = (ny - 1) * (yi - ymin) / (ymax - ymin)
# TODO: Instead, xi and yi need to be mapped as described.  This can only work with
# even spacing...something like:
#xi = SomeInterpFunction(xi, xrange)
#yi = SomeInterpFunction(yi, yrange)

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

# Display the results
for X, Y, Z in zip((x1, x2, x3), (y1, y2, y3), (z1, z2, z3)):
    print X, ',', Y, '-->', Z
从scipy.ndimage导入地图坐标
来自numpy import arange
将numpy作为np导入
#            0.000,  0.175,  0.817,  1.000
z=数组([[3.6,6.5,9.1,11.5],#0.0000
[ 3.9,   -7.3,    10.0,   13.1],    # 0.2620
[ 1.9,    8.3,   -15.0,  -12.1],    # 0.6121
[-4.5,    9.2,    12.2,   14.8] ])  # 1.0000
ny,nx=z形状
xmin,xmax=0,1。
ymin,ymax=0,1。
xrange=阵列([0.000,0.175,0.817,1.000])
Y范围=数组([0.0000,0.2620,0.6121,1.0000])
#我们要插值的点
x1,y1=0.20,0.45
x2,y2=0.30,0.85
x3,y3=0.95,1.00
#为了让我们的生活更轻松,让我们
#将它们转换为x&y坐标数组
席=NP数组([x1,x2,x3],dType=np.浮标)
yi=np.array([y1,y2,y3],dtype=np.float)
#现在,我们将在边界外设置点,使其位于边上
xi[xi>xmax]=xmax
xi[xiymax]=ymax
yi[yi',Z
我想你想要一个:


我发布了一个关于这个的后续问题,你也可以帮助我。在这里:。再次感谢。
from scipy.ndimage import map_coordinates
from numpy import arange
import numpy as np
#            0.000,  0.175,  0.817,  1.000
z = array([ [ 3.6,    6.5,    9.1,    11.5],    # 0.0000
            [ 3.9,   -7.3,    10.0,   13.1],    # 0.2620
            [ 1.9,    8.3,   -15.0,  -12.1],    # 0.6121
            [-4.5,    9.2,    12.2,   14.8] ])  # 1.0000
ny, nx = z.shape
xmin, xmax = 0., 1.
ymin, ymax = 0., 1.

xrange = array([0.000,  0.175,  0.817,  1.000 ])
yrange = array([0.0000, 0.2620, 0.6121, 1.0000])

# Points we want to interpolate at
x1, y1 = 0.20, 0.45
x2, y2 = 0.30, 0.85
x3, y3 = 0.95, 1.00

# 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

yi[yi > ymax] = ymax
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)
yi = (ny - 1) * (yi - ymin) / (ymax - ymin)
# TODO: Instead, xi and yi need to be mapped as described.  This can only work with
# even spacing...something like:
#xi = SomeInterpFunction(xi, xrange)
#yi = SomeInterpFunction(yi, yrange)

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

# Display the results
for X, Y, Z in zip((x1, x2, x3), (y1, y2, y3), (z1, z2, z3)):
    print X, ',', Y, '-->', Z
import numpy
from scipy import interpolate
x = numpy.array([0.0, 0.60, 1.0])
y = numpy.array([0.0, 0.25, 0.80, 1.0])
z = numpy.array([ 
   [ 1.4 ,  6.5 ,  1.5 ,  1.8 ],
   [ 8.9 ,  7.3 ,  1.1 ,  1.09],
   [ 4.5 ,  9.2 ,  1.8 ,  1.2 ]])
# you have to set kx and ky small for this small example dataset
# 3 is more usual and is the default
# s=0 will ensure this interpolates.  s>0 will smooth the data
# you can also specify a bounding box outside the data limits
# if you want to extrapolate
sp = interpolate.RectBivariateSpline(x, y, z, kx=2, ky=2, s=0)

sp([0.60], [0.25])  # array([[ 7.3]])
sp([0.25], [0.60])  # array([[ 2.66427408]])