二维插值问题

发布于 2024-08-16 17:48:09 字数 336 浏览 2 评论 0原文

我在 x 和 y 轴上有数据,输出在 z 上,

例如

y = 10
x = [1,2,3,4,5,6]
z = [2.3,3.4,5.6,7.8,9.6,11.2]

y = 20 
x = [1,2,3,4,5,6]
z = [4.3,5.4,7.6,9.8,11.6,13.2]

y = 30 
x = [1,2,3,4,5,6]
z = [6.3,7.4,8.6,10.8,13.6,15.2]

当 y = 15 x = 3.5 时我如何找到 z 的值

我试图使用 scipy 但我对此很陌生

非常感谢您的帮助

维布尔

I have DATA on x and y axes and the output is on z

for example

y = 10
x = [1,2,3,4,5,6]
z = [2.3,3.4,5.6,7.8,9.6,11.2]

y = 20 
x = [1,2,3,4,5,6]
z = [4.3,5.4,7.6,9.8,11.6,13.2]

y = 30 
x = [1,2,3,4,5,6]
z = [6.3,7.4,8.6,10.8,13.6,15.2]

how can i find the value of z when y = 15 x = 3.5

I was trying to use scipy but i am very new at it

Thanks a lot for the help

vibhor

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

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

发布评论

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

评论(3

╭⌒浅淡时光〆 2024-08-23 17:48:09

scipy.interpolate.bisplrep

参考:
http://docs.scipy.org/doc/ scipy/reference/ generated/scipy.interpolate.bisplrep.html

import scipy
import math
import numpy
from scipy import interpolate


x= [1,2,3,4,5,6]
y= [10,20,30]

Y = numpy.array([[i]*len(x) for i in y])
X = numpy.array([x for i in y])
Z = numpy.array([[2.3,3.4,5.6,7.8,9.6,11.2],
                 [4.3,5.4,7.6,9.8,11.6,13.2],
                 [6.3,7.4,8.6,10.8,13.6,15.2]]) 

tck = interpolate.bisplrep(X,Y,Z)
print interpolate.bisplev(3.5,15,tck) 


7.84921875

上面的解决方案并不能给你完美的配合。
检查

print interpolate.bisplev(x,y,tck)

[[  2.2531746    4.2531746    6.39603175]
 [  3.54126984   5.54126984   7.11269841]
 [  5.5031746    7.5031746    8.78888889]
 [  7.71111111   9.71111111  10.9968254 ]
 [  9.73730159  11.73730159  13.30873016]
 [ 11.15396825  13.15396825  15.2968254 ]]

以克服此插值,在 x 方向上插入 5 次多项式,在 y 方向上插入 2 次多项式,

tck = interpolate.bisplrep(X,Y,Z,kx=5,ky=2)
print interpolate.bisplev(x,y,tck) 

[[  2.3   4.3   6.3]
 [  3.4   5.4   7.4]
 [  5.6   7.6   8.6]
 [  7.8   9.8  10.8]
 [  9.6  11.6  13.6]
 [ 11.2  13.2  15.2]]

结果

print interpolate.bisplev(3.5,15,tck)

7.88671875

绘图:
参考http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo.html

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z,rstride=1, cstride=1, cmap=cm.jet)
plt.show()

scipy.interpolate.bisplrep

Reference:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.bisplrep.html

import scipy
import math
import numpy
from scipy import interpolate


x= [1,2,3,4,5,6]
y= [10,20,30]

Y = numpy.array([[i]*len(x) for i in y])
X = numpy.array([x for i in y])
Z = numpy.array([[2.3,3.4,5.6,7.8,9.6,11.2],
                 [4.3,5.4,7.6,9.8,11.6,13.2],
                 [6.3,7.4,8.6,10.8,13.6,15.2]]) 

tck = interpolate.bisplrep(X,Y,Z)
print interpolate.bisplev(3.5,15,tck) 


7.84921875

Upper solution does not give you perfect fit.
check

print interpolate.bisplev(x,y,tck)

[[  2.2531746    4.2531746    6.39603175]
 [  3.54126984   5.54126984   7.11269841]
 [  5.5031746    7.5031746    8.78888889]
 [  7.71111111   9.71111111  10.9968254 ]
 [  9.73730159  11.73730159  13.30873016]
 [ 11.15396825  13.15396825  15.2968254 ]]

to overcome this interpolate whit polyinomials of 5rd degree in x and 2nd degree in y direction

tck = interpolate.bisplrep(X,Y,Z,kx=5,ky=2)
print interpolate.bisplev(x,y,tck) 

[[  2.3   4.3   6.3]
 [  3.4   5.4   7.4]
 [  5.6   7.6   8.6]
 [  7.8   9.8  10.8]
 [  9.6  11.6  13.6]
 [ 11.2  13.2  15.2]]

This yield

print interpolate.bisplev(3.5,15,tck)

7.88671875

Plotting:
reference http://matplotlib.sourceforge.net/examples/mplot3d/surface3d_demo.html

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(X, Y, Z,rstride=1, cstride=1, cmap=cm.jet)
plt.show()
帅气尐潴 2024-08-23 17:48:09

给定(不是Python代码,因为当然,第二个赋值会消除第一个赋值;-):

y = 10
x = [1,2,3,4,5,6]
z = [2.3,3.4,5.6,7.8,9.6,11.2]

y = 20 
x = [1,2,3,4,5,6]
z = [4.3,5.4,7.6,9.8,11.6,13.2]

你问:“当 y = 15 x = 3.5 时,我如何找到 z 的值”?

由于您正在查看与给定“网格”的 x 和 y 完全等距的点,因此您只需取网格值之间的中点(如果您的值不等距,则将采用比例中点,请参阅下文) 。因此,对于 y=10,x 3 和 4 的 z 值分别为 5.6 和 7.8,因此对于 x 3.5,您估计它们的中点 6.7;同样,对于 y=20,您估计 7.6 和 9.8 之间的中点,即 8.7。最后,由于 y=15,6.7 和 8.7 之间的中点就是 z 的最终插值:7.7。

假设您有 y=13 和 x=3.8。然后对于 x,您将取 80% 的值,即:

  • for y=10, 0.2*5.6+0.8*7.8 -> 7.36

  • 对于 y=20,0.2*7.6+0.8*9.8 -> 9.46

现在你想要 z 介于这些之间的 30%,0.3*7.36 + 0.7*9.46 -> 8.83,就是z。

这就是线性插值,而且非常简单。您想手动计算它,还是找到为您执行此操作的例程(例如将 numpy 数组作为“网格”)?即使在后一种情况下,我希望这个“手动”解释(用最基本的算术术语显示您正在做的事情)可以帮助您理解您正在做的事情......;-)。

当然,还有更高级的插值形式——您是否需要这些,或者线性插值足以满足您的用例吗?

Given (not as Python code, since the second assignment would obliterate the first in each case, of course;-):

y = 10
x = [1,2,3,4,5,6]
z = [2.3,3.4,5.6,7.8,9.6,11.2]

y = 20 
x = [1,2,3,4,5,6]
z = [4.3,5.4,7.6,9.8,11.6,13.2]

you ask: "how can i find the value of z when y = 15 x = 3.5"?

Since you're looking at a point exactly equidistant in both x and y from the given "grid", you just take the midpoint between the grid values (if you had values not equidistant, you'd take a proportional midpoint, see later). So for y=10, the z values for x 3 and 4 are 5.6 and 7.8, so for x 3.5 you estimate their midpoint, 6.7; and similarly for y=20 you estimate the midpoint between 7.6 and 9.8, i.e., 8.7. Finally, since you have y=15, the midpoint between 6.7 and 8.7 is your final interpolated value for z: 7.7.

Say you had y=13 and x=3.8 instead. Then for x you'd take the values 80% of the way, i.e.:

  • for y=10, 0.2*5.6+0.8*7.8 -> 7.36

  • for y=20, 0.2*7.6+0.8*9.8 -> 9.46

Now you want the z 30% of the way between these, 0.3*7.36 + 0.7*9.46 -> 8.83, that's z.

This is linear interpolation, and it's really very simple. Do you want to compute it by hand, or find routines that do it for you (given e.g. numpy arrays as "the grids")? Even in the latter case, I hope this "manual" explanation (showing what you're doing in the most elementary of arithmetical terms) can help you understand what you're doing...;-).

There are more advanced forms of interpolation, of course -- do you need those, or does linear interpolation suffice for your use case?

携余温的黄昏 2024-08-23 17:48:09

我想说的是取周围值的平均值。因此,如果您需要 X=3.5 且 Y=15 (3.5,15),则对 (3,10)、(3,20)、(4,10) 和 (4,20) 进行平均。因为我不知道你正在处理的数据是什么,所以我不确定确切的接近度是否重要 - 在这种情况下你可以只坚持平均值 - 或者你是否需要进行某种反距离加权。

I would say just take the average of the values around it. So if you need X=3.5 and Y=15 (3.5,15), you average (3,10), (3,20), (4,10) and (4,20). Since I have no idea what the data is you are dealing with, I am not sure if the exact proximity would matter - in which case you can just stick w/the average - or if you need to do some sort of inverse distance weighting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文