python griddata的替代品 [英] Alternatives to python griddata
问题描述
我正在使用 griddata 对网格上的 numpy 二维数组重新采样.
I am using griddata to resample a numpy 2 dimensional array on a grid.
z.shape = (1000, 1000)
x, y = np.arange(-5, 5, 0.01), np.arange(-5, 5, 0.01)
newx, newy = np.arange(-2, 2, 0.1), np.arange(-2, 2, 0.1)
griddata((x, y), z, (newx[None, :], newy[:, None]))
代码应该:
- 将 z(代表图像)重新采样到新的粗或细网格
- 新网格不一定涵盖所有原始网格.
但是 griddata 无法管理常规输入网格.有谁知道一个简单的替代方法?
However griddata cannot manage a regular input grid. Does anyone know an easy alternative?
推荐答案
使用任何适用于文档中列出的网格数据的方法:https://docs.scipy.org/doc/scipy/reference/interpolate.html#multivariate-interpolation
Use any of the methods suitable for data on a grid listed in the documentation: https://docs.scipy.org/doc/scipy/reference/interpolate.html#multivariate-interpolation
即:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RegularGridInterpolator.html
或 https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RectBivariateSpline.html
另请注意,您错误地使用了 griddata
.您的代码对应于从 1000 (x, y) 坐标定义的线进行插值,其中每个点都有 1000 个与之关联的值.但是,从 1D 线插值到 2D 的定义不正确,并且尝试对沿线的一组点进行三角测量会导致失败.
Note also that you are using griddata
incorrectly. Your code corresponds to interpolating from a line defined by your 1000 (x, y) coordinates, where each point has 1000 values associated with it. However, interpolation to 2D from a 1D line is badly defined, and the failure results from trying to triangulate a set of points that are along a line.
你应该这样做
import numpy as np
from scipy.interpolate import griddata
z = np.random.rand(100, 100)
z.shape = (100, 100)
x, y = np.arange(-5, 5, 0.1), np.arange(-5, 5, 0.1)
xx, yy = np.meshgrid(x, y, indexing='ij')
newx, newy = np.arange(-2, 2, 0.1), np.arange(-2, 2, 0.1)
griddata((xx.ravel(), yy.ravel()), z.ravel(), (newx[None, :], newy[:, None]))
这将正常工作 --- 但是,2D 中的 1000x1000 = 1000000 个点对于基于三角剖分的非结构化插值来说简直是太多数据(三角剖分需要大量内存+它很慢),因此您应该使用网格数据算法.
This will work correctly --- however, 1000x1000 = 1000000 points in 2D is simply way too much data for triangulation-based unstructured interpolation (needs large amounts of memory for the triangulation + it's slow), so you should use the gridded data algorithms.
这篇关于python griddata的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!