Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么scipy.griddata返回带有';立方';如果输入为';价值观';纳南?_Python_Scipy_Interpolation_Nan_Cubic - Fatal编程技术网

Python 为什么scipy.griddata返回带有';立方';如果输入为';价值观';纳南?

Python 为什么scipy.griddata返回带有';立方';如果输入为';价值观';纳南?,python,scipy,interpolation,nan,cubic,Python,Scipy,Interpolation,Nan,Cubic,我想使用scipy.griddata对包含一些nan值的数组执行立方插值。但是,只要values参数中存在单个nan,返回的插值将仅填充nan。使用“最近”或“线性”插值方法时,情况并非如此 这种行为的原因是什么?是否有一种简单的方法可以忽略值输入中的NAN 这是一个最小的工作示例,改编自: 解决方案是在插入数据之前,从点和值的输入数组中删除所有nannumpy可以有效地用于此操作: import numpy as np def func(x, y): return x*(1-x)*n

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

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

这是一个最小的工作示例,改编自:


解决方案是在插入数据之前,从
点和
值的输入数组中删除所有
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函数的这个问题只出现在三次插值中

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')