2

nan我想使用包含一些值的数组执行三次插值scipy.griddata。但是,只要参数nan中存在单个values,则返回的插值仅填充nan. 当使用“最近”或“线性”插值方法时,情况并非如此。

这种行为的原因是什么,是否有一种简单的方法可以忽略输入中的 nans values

这是一个最小的工作示例,改编自griddata scipy interpolation not working (给 nan)

import numpy as np

def func(x, y):
    return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2

grid_x, grid_y = np.mgrid[0:1:10j, 0:1:10j]
points = np.random.rand(100, 2)
values = func(points[:,0], points[:,1])

values[0]=np.nan # now add a single nan value to the array

from scipy.interpolate import griddata

grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest') # no nans here
grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear') # this has nans on the edges (as expected)
grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic') # this is filled only with nans.
4

1 回答 1

1

一种解决方案是在插值数据之前nanpoints和输入数组中删除所有内容。可以有效地用于这样做:valuesnumpy

import numpy as np

def func(x, y):
    return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2

grid_x, grid_y = np.mgrid[0:1:10j, 0:1:10j]
points = np.random.rand(100, 2)
values = func(points[:,0], points[:,1])

values[0]=np.nan # now add a single nan value to the array

#Find all the indexes where there is no nan neither in values nor in points.
nonanindex=np.invert(np.isnan(points[:,0]))*np.invert(np.isnan(points[:,1]))*np.invert(np.isnan(values))

#Remove the nan using fancy indexing. griddata can now properly interpolate. The result will have nan only on the edges of the array
from scipy.interpolate import griddata
grid_z2 = riddata(np.stack((points[nonanindex,0],points[nonanindex,1]),axis=1), values[nonanindex], (grid_x, grid_y), method='cubic')

虽然这解决了问题,但我还没有回答为什么 griddata 函数的这个问题只出现在三次插值中。

于 2019-11-27T15:26:11.400 回答