【问题标题】:Changing data resolution in Python在 Python 中更改数据分辨率
【发布时间】:2017-02-17 00:42:12
【问题描述】:

我有一组数据(存储在 2D numpy 数组中)代表相同问题的模拟。但是,每个模拟都来自不同的模型,这导致每个模拟的分辨率不同。例如,这些是一些模拟的大小:

  1. 1159 x 1367
  2. 144 x 157
  3. 72 x 82
  4. 446 x 500
  5. 135 x 151

我想做的是将它们全部转换为相同的分辨率,例如 144 x 157。我相信我必须执行插值,但是,我不确定在 Python 中使用哪种方法。

我一直在阅读这些:

  1. scipy.interpolate.griddata
  2. scipy.ndimage.interpolation.zoom.html
  3. scipy.interpolate.RegularGridInterpolator.html
  4. scipy.ndimage.interpolation.map_coordinates.html

(3) 和 (4) 似乎最适合该问题,但是,我不确定如何使它们返回具有指定分辨率的新网格 (2D) 数据。

【问题讨论】:

  • 您的所有数据是否都覆盖相同的坐标范围?换句话说,是否所有测量值都在(例如)x 中的 0 到 3 和 y 中的 0 到 10 之间?
  • 是的,它们都覆盖同一个区域,但分辨率不同。
  • 这些是模拟,你的数据没有噪音吗?
  • 它们在模拟的最后一步被平滑。
  • 我只是在问,因为如果噪声很大,您可能需要考虑不使用插值,而是对数据的过采样位进行某种平均。

标签: python numpy scipy interpolation


【解决方案1】:

原来我可以用scipy.interpolate.RegularGridInterpolator.html解决它:

import numpy as np
import pylab as plt

from scipy.interpolate import RegularGridInterpolator

def regrid(data, out_x, out_y):
    m = max(data.shape[0], data.shape[1])
    y = np.linspace(0, 1.0/m, data.shape[0])
    x = np.linspace(0, 1.0/m, data.shape[1])
    interpolating_function = RegularGridInterpolator((y, x), data)

    yv, xv = np.meshgrid(np.linspace(0, 1.0/m, out_y), np.linspace(0, 1.0/m, out_x))

    return interpolating_function((xv, yv))

输入:

输出:

【讨论】:

  • 为什么在你的例子中导入 pylab?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-03
  • 2019-06-28
  • 1970-01-01
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多