Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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.interpolate.griddata和method=nearest的边界外填充值_Python_Numpy_Scipy_Interpolation_Qhull - Fatal编程技术网

Python 使用scipy.interpolate.griddata和method=nearest的边界外填充值

Python 使用scipy.interpolate.griddata和method=nearest的边界外填充值,python,numpy,scipy,interpolation,qhull,Python,Numpy,Scipy,Interpolation,Qhull,函数允许指定关键字fill\u值,其中文档说明: 用于填充的凸包外的请求点的值 输入点。如果未提供,则默认为nan。 此选项对“最近”方法无效。 但是,我需要指定一个fill_值,以便在 对二维数据使用方法='nearest'。如何实现这一点?通过以下解决方法,可以相对容易地实现这一点: 使用method='nearest'运行 使用方法='linear'再次运行;在这里,外部区域充满了np.nan 只要2的结果中有一个NaN。将所需的fill_值分配给1的结果 代码: 一种不太特别的方法是显式

函数允许指定关键字
fill\u值
,其中文档说明:

用于填充的凸包外的请求点的值 输入点。如果未提供,则默认为nan。 此选项对“最近”方法无效。

但是,我需要指定一个
fill_值
,以便在
对二维数据使用
方法='nearest'
。如何实现这一点?

通过以下解决方法,可以相对容易地实现这一点:

  • 使用
    method='nearest'运行
  • 使用
    方法='linear'
    再次运行;在这里,外部区域充满了
    np.nan
  • 只要2的结果中有一个NaN。将所需的
    fill_值
    分配给1的结果
  • 代码:


    一种不太特别的方法是显式地计算凸包并使用它来分配填充值。这需要更多的努力,但可能会运行得更快。

    感谢您的回答,这确实是一个简单的解决方案!尽管在实践中,使用单个
    方法class='nearest'
    所需的时间是使用单个
    方法所需时间的两倍以上。我来看看,它可能会奏效。
    
    import numpy as np
    from scipy.interpolate import griddata
    
    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:100j, 0:1:200j]
    
    points = np.random.rand(100, 2)
    values = func(points[:,0], points[:,1])
    
    grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
    grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
    
    fill_value = 123  # Whatever you like
    grid_z0[np.isnan(grid_z1)] = fill_value