numpy 数组上的线性插值

发布于 2024-09-10 11:35:20 字数 784 浏览 2 评论 0原文

我有以下 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+

如果用户输入 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

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

我已经完成了一些代码,例如

#boundaries:
y[y < 0] = 0
y[y > 4] = 4

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

I have the following numpy array:

#                      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+

if a user enters a y (example, 2.5) I should out put three values, one for A, B, and C:

in my example A: .19, B: .57, C: .24

More Examples:

Y     A      B      C
0.2   .20    .54    .26 
1.5   .215   .55    .245
4.0   .17    .62    .21
8.7   .17    .62    .21

The user will enter a multiple of y values as a numpy array. the result should be an array as well

I've done bits and pieces of the code for example

#boundaries:
y[y < 0] = 0
y[y > 4] = 4

I'm also assuming that scipy.ndimage / map_coordinates will best fit my requirements rather than scipy.interpolate but I could be wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

源来凯始玺欢你 2024-09-17 11:35:20
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

>>> 
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]
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

>>> 
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]
死开点丶别碍眼 2024-09-17 11:35:20

使用 scipy.ndimage 可能有更好的方法,但以下是使用 scipy.interpolate.interp1d 的方法:

import numpy as np
import scipy.interpolate as spi

#                      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] ])

print(my_arr)
Y=np.arange(len(my_arr))
interp_funcs=[spi.interp1d(Y,my_arr[:,col]) for col in range(3)]
y=np.array([2.5,0.2,1.5,4.0,8.7])
y[y < 0] = 0
y[y > 4] = 4
print(np.vstack(f(y) for f in interp_funcs))
# [[ 0.19   0.204  0.205  0.17   0.17 ]
#  [ 0.57   0.54   0.55   0.62   0.62 ]
#  [ 0.24   0.256  0.245  0.21   0.21 ]]

There might be a better way using scipy.ndimage, but here is how you could do it with scipy.interpolate.interp1d:

import numpy as np
import scipy.interpolate as spi

#                      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] ])

print(my_arr)
Y=np.arange(len(my_arr))
interp_funcs=[spi.interp1d(Y,my_arr[:,col]) for col in range(3)]
y=np.array([2.5,0.2,1.5,4.0,8.7])
y[y < 0] = 0
y[y > 4] = 4
print(np.vstack(f(y) for f in interp_funcs))
# [[ 0.19   0.204  0.205  0.17   0.17 ]
#  [ 0.57   0.54   0.55   0.62   0.62 ]
#  [ 0.24   0.256  0.245  0.21   0.21 ]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文