python - 如果输入 'cubic' 包含 nan,为什么 scipy.griddata 返回具有 'values' 插值的 nan?

标签 python scipy interpolation nan cubic

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

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

这是一个最小的工作示例,改编自 griddata scipy interpolation not working (giving 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.

最佳答案

解决方案是在插值数据之前从 pointsvalues 输入数组中删除所有 nan。可以有效地使用 numpy 来实现此目的:

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 函数的这个问题只出现在三次插值中。

关于python - 如果输入 'cubic' 包含 nan,为什么 scipy.griddata 返回具有 'values' 插值的 nan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59071446/

相关文章:

python - 将 tkinter 下拉菜单选项保存在变量中以进行比较

python - 使用 scipy 最小化二参数函数时的性能问题

c++ - 在两个值之间插入一个值

python - python中的复杂对称矩阵

JAVA,用String.format插入n次相同的变量

r - 在 R 编程中修复波动率曲面图的插值

python - WSL :/usr/bin/env: ‘python’ : No such file or directory

python - 用 Pandas 导入数据时卡住了错误

python - 如何获得不和谐公会的成员(discord.py)?

python - Scipy 安装到 Mountain Lion 的错误位置?