不同的结果与scipy.interpolate.griddata
问题描述:
2D插值我需要在2D阵列numpy的执行一些Nan
值的内插,例如参见下面的图片:不同的结果与scipy.interpolate.griddata
以我当前的方法对于插值过程,我使用了scipy.interpolate.griddata
。但是我注意到当 镜像两个轴上的阵列,即d2 = d[::-1, ::-1]
, 时,插值给出了不同的结果。 下面是一个完整的例子:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interp
def replace_outliers(f):
mask = np.isnan(f)
lx, ly = f.shape
x, y = np.mgrid[0:lx, 0:ly]
z = interp.griddata(np.array([x[~mask].ravel(),y[~mask].ravel()]).T,
f[~mask].ravel(),
(x,y), method='linear', fill_value=0)
return z
def main():
d = np.load('test.npy')
d2 = d[::-1, ::-1]
dn = replace_outliers(d)
dn2 = replace_outliers(d2)
print np.sum(dn - dn2[::-1, ::-1])
plt.imshow(dn-dn2[::-1, ::-1], interpolation='nearest')
plt.colorbar()
plt.show()
if __name__=='__main__':
main()
这给了两个插补之间的区别:
或np.sum
作为评价其约-62.7
那么怎么会这样数组 的简单镜像在插值过程中给出了不同的结果? 我使用的坐标有可能是错的吗?
答
原因可能是线性插值为三角形为基础的。然而,这样的正方形网格是Delaunay三角剖分的退化情况,并且三角剖分不是唯一的。我可以想象,结果取决于数据点的顺序。
对于丢失的数据点,我猜这两种情况对应的空空间的不同三角:
A A
* * * *---*---* *---*---*
|/ \ | |/| \ |
* * => D*-------*B or D* | *B
| \ /| | \ |/|
* * * *---*---* *---*---*
C C
如果你现在计算为中心的价值,你会得到(B + d)/ 2个来自一个三角测量,(A + C)/ 2来自另一个三角测量。
谢谢您的回复,这可能确实如此......但是这两种情况之间的总体差异应该小于上述情况吗?它与现场的顺序相同。 – jrsm
顺便说一下,如果有更好的方法来执行这种插值,请让我知道:) – jrsm
如果数据如上所述,我猜这个字段的顺序是垂直插入与水平插入之间可能的差异。 –