【问题标题】:Extract random 2d windows of a 2d numpy array提取 2d numpy 数组的随机 2d 窗口
【发布时间】:2018-10-04 08:13:23
【问题描述】:
import numpy as np
arr = np.array(range(60)).reshape(6,10)

arr

> array([[ 0,  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, 39],
>        [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
>        [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]])

我需要什么:

select_random_windows(arr, number_of windows= 3, window_size=3)



>  array([[[ 1,  2,  3],
>            [11, 12, 13],
>            [21, 22, 23]],
>            
>            [37, 38, 39],
>            [47, 48, 49],
>            [57, 58, 59]],
>           
>            [31, 32, 33],
>            [41, 42, 43],
>            [51, 52, 53]]])

在这个假设的情况下,我在主阵列 (arr) 中选择 3 个 3x3 的窗口。

我的实际阵列是一个光栅,我基本上需要一堆(上千个)3x3 小窗口。

任何帮助甚至提示都将不胜感激。

实际上我还没有找到任何实用的解决方案......因为很多小时

谢谢!

【问题讨论】:

    标签: python-3.x numpy stride


    【解决方案1】:

    我们可以利用基于np.lib.stride_tricks.as_stridedscikit-image's view_as_windows 来获得滑动窗口。 More info on use of as_strided based view_as_windows.

    from skimage.util.shape import view_as_windows
    
    def select_random_windows(arr, number_of_windows, window_size):
        # Get sliding windows
        w = view_as_windows(arr,window_size)
    
        # Store shape info
        m,n =  w.shape[:2]
    
        # Get random row, col indices for indexing into windows array
        lidx = np.random.choice(m*n,number_of_windows,replace=False)
        r,c = np.unravel_index(lidx,(m,n))
        # If duplicate windows are allowed, use replace=True or np.random.randint
    
        # Finally index into windows and return output
        return w[r,c]
    

    示例运行 -

    In [209]: arr
    Out[209]: 
    array([[ 0,  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, 39],
           [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
           [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]])
    
    In [210]: np.random.seed(0)
    
    In [211]: select_random_windows(arr, number_of_windows=3, window_size=(2,4))
    Out[211]: 
    array([[[41, 42, 43, 44],
            [51, 52, 53, 54]],
    
           [[26, 27, 28, 29],
            [36, 37, 38, 39]],
    
           [[22, 23, 24, 25],
            [32, 33, 34, 35]]])
    

    【讨论】:

    • 尽管它有效(你得到了荣誉!)..只是担心分配结果数组后的一些警告:FutureWarning:不推荐使用非元组序列进行多维索引;使用arr[tuple(seq)] 而不是arr[seq]。将来这将被解释为数组索引arr[np.array(seq)],这将导致错误或不同的结果。 indexing_strides = arr_in[slices].strides
    • @MarioFajardo 这是 scikit-image 特有的东西。尝试更新它,或者您可以忽略该警告。
    • 冠军..非常感谢
    【解决方案2】:

    你可以试试[numpy.random.choice()][1]。它采用 1D 或 ndarray 并通过对给定 ndarray 中的元素进行采样来创建单个元素或 ndarray。您还可以选择提供所需的数组大小作为输出。

    【讨论】:

      猜你喜欢
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2018-02-02
      • 2018-12-01
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多