numpy数组上的线性插值

das*_*uki 2 python interpolation numpy scipy

我有以下numpy数组:

#                      A    B    C         Y
my_arr = np.array([ [.20, .54, .26],     # <0
                    [.22, .54, .24],     # 1
                    [.19, .56, .25],     # 2
                    [.19, .58, .23],     # 3
                    [.17, .62, .21] ])   # 4+
Run Code Online (Sandbox Code Playgroud)

如果用户输入ay(例如,2.5),我应该输出三个值,一个用于A,B和C:

在我的例子A:.19,B:.57,C:.24

更多例子:

Y     A      B      C
0.2   .20    .54    .26 
1.5   .215   .55    .245
4.0   .17    .62    .21
8.7   .17    .62    .21
Run Code Online (Sandbox Code Playgroud)

用户将输入多个y值作为numpy数组.结果也应该是一个数组

例如,我已经完成了代码的各个部分

#boundaries:
y[y < 0] = 0
y[y > 4] = 4
Run Code Online (Sandbox Code Playgroud)

我也假设scipy.ndimage/map_coordinates最符合我的要求而不是scipy.interpolate但我可能是错的

Jud*_*den 6

from scipy import array, ndimage

#              A    B    C         Y
m = array([ [.20, .54, .26],     # 0
            [.22, .54, .24],     # 1
            [.19, .56, .25],     # 2
            [.19, .58, .23],     # 3
            [.17, .62, .21] ])   # 4

inputs = array([-1, 0, 0.2, 1, 1.5, 2, 2.5, 3, 4, 8.7])
inputs[inputs < 0] = 0
inputs[inputs > 4] = 4

for y in inputs:
    x = ndimage.map_coordinates(m, [y * numpy.ones(3), numpy.arange(3)], order=1)
    print y, x
Run Code Online (Sandbox Code Playgroud)
>>> 
0.0 [ 0.2   0.54  0.26]
0.0 [ 0.2   0.54  0.26]
0.2 [ 0.204  0.54   0.256]
1.0 [ 0.22  0.54  0.24]
1.5 [ 0.205  0.55   0.245]
2.0 [ 0.19  0.56  0.25]
2.5 [ 0.19  0.57  0.24]
3.0 [ 0.19  0.58  0.23]
4.0 [ 0.17  0.62  0.21]
4.0 [ 0.17  0.62  0.21]
Run Code Online (Sandbox Code Playgroud)