栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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]: aOut[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_indicesOut[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]: aOut[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_indicesOut[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_indicesOut[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')
or
out.view('u1')
int8
uint8
dtype输出。

对于其他数据类型,我们将需要

.astype()
用于dtype转换。

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

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

return mask_indices_nd>=r

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

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

return mask_indices_nd<r


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669203.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号