根据起始索引有效填充面膜

发布于 2021-01-29 17:26:40

我有一个2D数组(对于这个例子,实际上可以是ND),我想为其创建一个遮盖每行末尾的遮罩。例如:

np.random.seed(0xBEEF)
a = np.random.randint(10, size=(5, 6))
mask_indices = np.argmax(a, axis=1)

我想转换mask_indices为布尔型掩码。目前,我想不出一种更好的方法

mask = np.zeros(a.shape, dtype=np.bool)
for r, m in enumerate(mask_indices):
    mask[r, m:] = True

因此对于

a = np.array([[6, 5, 0, 2, 1, 2],
              [8, 1, 3, 7, 1, 9],
              [8, 7, 6, 7, 3, 6],
              [2, 7, 0, 3, 1, 7],
              [5, 4, 0, 7, 6, 0]])

mask_indices = np.array([0, 5, 0, 1, 3])

我想看看

mask = np.array([[ True,  True,  True,  True,  True,  True],
                 [False, False, False, False, False,  True],
                 [ True,  True,  True,  True,  True,  True],
                 [False,  True,  True,  True,  True,  True],
                 [False, False, False,  True,  True,  True]])

此操作是否有矢量化形式?

通常,除了定义索引点的维度之外,我希望能够在所有维度上做到这一点。

关注者
0
被浏览
43
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    I.沿最后一个轴(行)的Ndim数组蒙版

    为了使n-dim数组沿行屏蔽,我们可以-

    def mask_from_start_indices(a, mask_indices):
        r = np.arange(a.shape[-1])
        return mask_indices[...,None]<=r
    

    样品运行-

    In [177]: np.random.seed(0)
         ...: a = np.random.randint(10, size=(2, 2, 5))
         ...: mask_indices = np.argmax(a, axis=-1)
    
    In [178]: a
    Out[178]: 
    array([[[5, 0, 3, 3, 7],
            [9, 3, 5, 2, 4]],
    
           [[7, 6, 8, 8, 1],
            [6, 7, 7, 8, 1]]])
    
    In [179]: mask_indices
    Out[179]: 
    array([[4, 0],
           [2, 3]])
    
    In [180]: mask_from_start_indices(a, mask_indices)
    Out[180]: 
    array([[[False, False, False, False,  True],
            [ True,  True,  True,  True,  True]],
    
           [[False, False,  True,  True,  True],
            [False, False, False,  True,  True]]])
    

    二。沿通用轴的Ndim阵列遮罩

    对于沿通用轴进行遮罩的n维数组,它将是-

    def mask_from_start_indices_genericaxis(a, mask_indices, axis):
        r = np.arange(a.shape[axis]).reshape((-1,)+(1,)*(a.ndim-axis-1))
        mask_indices_nd = mask_indices.reshape(np.insert(mask_indices.shape,axis,1))
        return mask_indices_nd<=r
    

    样品运行-

    数据阵列设置:

    In [288]: np.random.seed(0)
         ...: a = np.random.randint(10, size=(2, 3, 5))
    
    In [289]: a
    Out[289]: 
    array([[[5, 0, 3, 3, 7],
            [9, 3, 5, 2, 4],
            [7, 6, 8, 8, 1]],
    
           [[6, 7, 7, 8, 1],
            [5, 9, 8, 9, 4],
            [3, 0, 3, 5, 0]]])
    

    指标设置和掩盖axis=1-

    In [290]: mask_indices = np.argmax(a, axis=1)
    
    In [291]: mask_indices
    Out[291]: 
    array([[1, 2, 2, 2, 0],
           [0, 1, 1, 1, 1]])
    
    In [292]: mask_from_start_indices_genericaxis(a, mask_indices, axis=1)
    Out[292]: 
    array([[[False, False, False, False,  True],
            [ True, False, False, False,  True],
            [ True,  True,  True,  True,  True]],
    
           [[ True, False, False, False, False],
            [ True,  True,  True,  True,  True],
            [ True,  True,  True,  True,  True]]])
    

    指标设置和掩盖axis=2-

    In [293]: mask_indices = np.argmax(a, axis=2)
    
    In [294]: mask_indices
    Out[294]: 
    array([[4, 0, 2],
           [3, 1, 3]])
    
    In [295]: mask_from_start_indices_genericaxis(a, mask_indices, axis=2)
    Out[295]: 
    array([[[False, False, False, False,  True],
            [ True,  True,  True,  True,  True],
            [False, False,  True,  True,  True]],
    
           [[False, False, False,  True,  True],
            [False,  True,  True,  True,  True],
            [False, False, False,  True,  True]]])
    

    其他情况

    A.扩展到给定的结束/停止索引以进行遮罩

    为了将解决方案扩展到给定要屏蔽的结束/停止索引的情况下(即,我们正在寻求向量化)mask[r, :m] = True,我们只需要在发布的解决方案中将比较的最后一步编辑为以下内容:

    return mask_indices_nd>r
    

    B.输出整数数组

    在某些情况下,我们可能希望获取一个int数组。在这些文件上,只需简单地查看输出即可。因此,如果out输出是发布的解决方案的输出,那么我们可以分别简单地为out.view('i1')orout.view('u1')int8uint8dtype输出。

    对于其他数据类型,我们将需要.astype()用于dtype转换。

    C.对于包含停止索引的索引的蒙版

    对于包含索引的掩码,即在停止索引的情况下要包含索引,我们只需在比较中包括相等性即可。因此,最后一步将是-

    return mask_indices_nd>=r
    

    D.对于起始索引的索引专有屏蔽

    在给定起始索引并且不对这些索引进行屏蔽的情况下,仅从下一个元素开始直到结束进行屏蔽都是这种情况。因此,类似于上一节中列出的推理,对于这种情况,我们将最后一步修改为-

    return mask_indices_nd<r
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看