【问题标题】:What's the best way to downsample a numpy array?对 numpy 数组进行下采样的最佳方法是什么?
【发布时间】:2017-06-13 17:05:10
【问题描述】:

我有一个 3 维 numpy 数组,形状为 Nx64x64。我想通过取平均值在维度 1 和维度 2 上对其进行下采样,从而得到一个形状为 Nx8x8 的新数组。

我有几个可行的实现,但我觉得必须有一种更简洁的方法。

我最初尝试使用 np.split:

def subsample(inparray, n):
    inp = inparray.copy()
    res = np.moveaxis(np.array(np.hsplit(inp, inp.shape[1]/n)), 1, 0)
    res = np.moveaxis(np.array(np.split(res, inp.shape[2]/n, axis=3)), 1, 0)
    res = np.mean(res, axis=(3,4))
    return res

我也尝试过使用普通索引:

def subsample2(inparray, n):
    res = np.zeros((inparray.shape[0], n, n))
    lin = np.linspace(0, inparray.shape[1], n+1).astype(int)
    bounds = np.stack((lin[:-1], lin[1:]), axis=-1)

    for i, b in enumerate(bounds):
        for j, b2 in enumerate(bounds):
            res[:, i, j] = np.mean(inparray[:, b[0]:b[1], b2[0]:b2[1]], axis=(1,2))
    return res

我曾想过使用 itertools.groupby,但它看起来也很复杂。

有人知道干净的解决方案吗?

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    scikit-image 模块 (link to docs) 中的函数 block_reduce 有一个简洁的解决方案。

    它有一个非常简单的接口,可以通过应用numpy.mean 之类的函数来对数组进行下采样。通过为块提供具有不同大小的元组,可以通过针对不同轴的不同因素来完成下采样。这是一个二维数组的例子;使用均值仅对轴 1 下采样 5:

    import numpy as np
    from skimage.measure import block_reduce
    
    arr = np.stack((np.arange(1,20), np.arange(20,39)))
    
    # array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
    #        [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38]])
    
    arr_reduced = block_reduce(arr, block_size=(1,5), func=np.mean, cval=np.mean(arr))
    
    # array([[ 3. ,  8. , 13. , 17.8],
    #        [22. , 27. , 32. , 33. ]])
    

    【讨论】:

    • 谢谢!你的回复真的很有用。
    【解决方案2】:

    重塑以将最后两个轴拆分为另外两个轴,使后拆分轴的长度等于块大小,给我们一个5D数组,然后在第三个和第五个轴上使用mean -

    BSZ = (8,8)
    m,n = a.shape[1:]
    out = a.reshape(N,m//BSZ[0],BSZ[0],n//BSZ[1],BSZ[1]).mean(axis=(2,4))
    

    样本在具有较小块大小的较小阵列上运行(2,2) -

    1) 输入:

    In [271]: N = 2
    
    In [272]: a = np.random.randint(0,9,(N,6,6))
    
    In [273]: a
    Out[273]: 
    array([[[3, 1, 8, 7, 8, 2],
            [0, 6, 2, 6, 8, 2],
            [2, 1, 1, 0, 0, 1],
            [8, 3, 0, 2, 8, 0],
            [4, 7, 2, 6, 6, 7],
            [5, 5, 1, 7, 2, 7]],
    
           [[0, 0, 8, 1, 7, 6],
            [8, 6, 5, 8, 4, 0],
            [0, 3, 7, 7, 6, 1],
            [7, 1, 7, 6, 3, 6],
            [7, 6, 4, 6, 4, 5],
            [4, 2, 0, 2, 6, 2]]])
    

    2) 手动验证获取少量输出值:

    In [274]: a[0,:2,:2].mean()
    Out[274]: 2.5
    
    In [275]: a[0,:2,2:4].mean()
    Out[275]: 5.75
    
    In [276]: a[0,:2,4:6].mean()
    Out[276]: 5.0
    
    In [277]: a[0,2:4,:2].mean()
    Out[277]: 3.5
    

    3) 使用建议的方法并手动验证:

    In [278]: BSZ = (2,2)
    
    In [279]: m,n = a.shape[1:]
    
    In [280]: a.reshape(N,m//BSZ[0],BSZ[0],n//BSZ[1],BSZ[1]).mean(axis=(2,4))
    Out[280]: 
    array([[[ 2.5 ,  5.75,  5.  ],
            [ 3.5 ,  0.75,  2.25],
            [ 5.25,  4.  ,  5.5 ]],
    
           [[ 3.5 ,  5.5 ,  4.25],
            [ 2.75,  6.75,  4.  ],
            [ 4.75,  3.  ,  4.25]]])
    

    【讨论】:

      【解决方案3】:

      您可以使用zoom - scipy.ndimage。 此库允许您沿 3 个轴中的任何一个进行缩放。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-04
        • 2013-12-17
        • 2010-09-14
        • 1970-01-01
        • 1970-01-01
        • 2019-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多