Alternatives to python griddata

為{幸葍}努か 提交于 2020-01-15 17:38:21

问题


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

The code should:

  • resample z (which represents an image) to a new coarser or finer grid
  • the new grid does not necessarily cover all of the original one.

However griddata cannot manage a regular input grid. Does anyone know an easy alternative?


回答1:


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

That is:

https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RegularGridInterpolator.html

or https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.RectBivariateSpline.html

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

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.

You should do

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

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.



来源:https://stackoverflow.com/questions/26276550/alternatives-to-python-griddata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!