问题描述
我正在使用scipy.interpolate.interp2d
为曲面创建插值函数.然后,我有两个实际数据数组,我想为其计算插值点.如果将两个数组传递给interp2d
函数,我将得到一个包含所有点的数组,而不仅仅是点对.
I'm using scipy.interpolate.interp2d
to create an interpolation function for a surface. I then have two arrays of real data that I want to calculate interpolated points for. If I pass the two arrays to the interp2d
function I get an array of all the points, not just the pairs of points.
我对此的解决方案是将两个数组压缩到一个坐标对列表中,然后将其循环传递给插值函数:
My solution to this is to zip the two arrays into a list of coordinate pairs and pass this to the interpolation function in a loop:
f_interp = interpolate.interp2d(X_table, Y_table,Z_table, kind='cubic')
co_ords = zip(X,Y)
out = []
for i in range(len(co_ords)):
X = co_ords[i][0]
Y = co_ords[i][1]
value = f_interp(X,Y)
out.append(float(value))
我的问题是,是否有更好的方法(更优雅,Pythonic?)来实现相同的结果?
My question is, is there a better (more elegant, Pythonic?) way of achieving the same result?
推荐答案
一个就可以了
for Xtmp,Ytmp in zip(X,Y):
...
在您的循环中.甚至更好,只是
in your loop. Or even better, just
out = [float(f_interp(XX,YY)) for XX,YY in zip(X,Y)]
替换循环.
换句话说,我建议改为使用interpolate.griddata
.它的行为往往比interp2d
好得多,并且可以接受任意形状的点作为输入.如您所见,interp2d
插值器只会在网格上返回您的值.
On a different note, I suggest using interpolate.griddata
instead. It tends to behave much better than interp2d
, and it accepts arbitrary-shaped points as input. As you've seen, interp2d
interpolators will only return you values on a mesh.
这篇关于SciPy interp2D用于坐标对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!