python - python griddata 的替代品

标签 python scipy resampling

我正在使用 griddata 对网格上的 numpy 二维数组进行重新采样。

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 无法管理常规输入网格。有谁知道一个简单的替代方案吗?

最佳答案

使用文档中列出的任何适合网格数据的方法: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

https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.interpolation.map_coordinates.html

另请注意,您错误地使用了 griddata。您的代码对应于从由 1000 (x, y) 坐标定义的线进行插值,其中每个点都有 1000 个与其关联的值。但是,从 1D 线到 2D 的插值定义不正确,并且尝试对沿线的一组点进行三角测量会导致失败。

你应该这样做

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 点实在是太多的数据(三角测量需要大量内存+速度很慢),因此您应该使用网格数据算法。

关于python - python griddata 的替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276550/

相关文章:

python - 如果子进程更改了系统时间,pexpect 会错误地超时

Python OOP - 类关系

python - 从 3d 到 2d 的 Project Scipy Voronoi 图

Python:更快地计算两个字典的余弦相似度

python - 在 SciPy 中将十进制矩阵转换为二进制矩阵

python - 使用 Pandas 在将每日数据转换为每周数据时格式化索引

python - Pandas 将数据重新采样到秒,每约 10 秒进行分组

Python:合并两个列表

python - 变量正在更新以打印准确的分数

c - binning/jack-knife c 程序中的段错误