Python插值与Nan值

时间:2016-09-06 17:51:13

标签: python-2.7 interpolation spline

我在2d阵列上使用插值函数。在阵列中,4个角中有3个是Nan值 - 插值例程在这种情况下效果很好。但是,当我在第四个角落添加NaN时,我的回归并不相同。

示例代码如下:

import numpy
import scipy.interpolate
import matplotlib.pylab as plt

## Create sampling space
x = numpy.linspace(-1,1,11)
y = numpy.linspace(-1,1,11)
X,Y = numpy.meshgrid(x,y)
R = numpy.sqrt(X**2 + Y**2)

## Create array with NaNs on corners
R[0:2,0:2] = numpy.nan # Works if only this subarray is nan
R[0:2,9:11] = numpy.nan # Works if only this subarray is nan
R[9:11,0:2] = numpy.nan # Works if only this subarray is nan
# R[9:11,9:11] = numpy.nan # Returns nan if only this subarray is nan

## Plot array
plt.figure()
plt.imshow(R)
plt.colorbar()
plt.show();

## Perform interpolation
f = scipy.interpolate.RectBivariateSpline(x, y, R, kx=1, ky=1)
print "f(0.05,0.05) = %.4f" % f(0.05,0.05)

运行上面的脚本,我的输出是:f(0.05,0.05) = 0.0927 我的输出图像显示了4个角中的3个角的NaN值:enter image description here

但是,当我取消注释数组中的第四个Nan(R[9:11,9:11] = numpy.nan)时,我的输出变为:f(0.05,0.05) = nan;巧合的是,我的2d数组的图表显示第四个角有NaN值: enter image description here

为什么插值器的值为NaN,具有最后一个条件?

0 个答案:

没有答案