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

Python-用2个索引列表索引2D Numpy数组

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

Python-用2个索引列表索引2D Numpy数组

np.ix_
使用索引或布尔数组/掩码进行选择或分配

1.与

indexing-arrays

一个选择

我们可以np.ix_用来获取索引数组的元组,它们可以相互广播以导致索引的高维组合。因此,当该元组用于索引输入数组时,将为我们提供相同的高维数组。因此,要基于两个1D索引数组进行选择,将是-

x_indexed = x[np.ix_(row_indices,col_indices)]

B.作业

我们可以使用相同的符号将标量或可广播数组分配给那些索引位置。因此,以下工作适用于作业-

x[np.ix_(row_indices,col_indices)] = # scalar or broadcastable array

2.用 masks

我们还可以将布尔数组/掩码与一起使用np.ix_,类似于如何使用索引数组。可以再次使用它来选择输入数组中的一个块,也可以对其进行分配。

一个选择

因此,使用row_mask和col_mask布尔数组分别作为行和列选择的掩码,我们可以使用以下内容进行选择-

x[np.ix_(row_mask,col_mask)]

B.作业

以下是作业的作品

x[np.ix_(row_mask,col_mask)] = # scalar or broadcastable array

样品运行

  1. np.ix_与indexing-arrays

输入数组和索引数组

In [221]: xOut[221]: array([[17, 39, 88, 14, 73, 58, 17, 78],       [88, 92, 46, 67, 44, 81, 17, 67],       [31, 70, 47, 90, 52, 15, 24, 22],       [19, 59, 98, 19, 52, 95, 88, 65],       [85, 76, 56, 72, 43, 79, 53, 37],       [74, 46, 95, 27, 81, 97, 93, 69],       [49, 46, 12, 83, 15, 63, 20, 79]])In [222]: row_indicesOut[222]: [4, 2, 5, 4, 1]In [223]: col_indicesOut[223]: [1, 2]

具有

np.ix_
- 的索引数组的元组

In [224]: np.ix_(row_indices,col_indices) # Broadcasting of indicesOut[224]: (array([[4],        [2],        [5],        [4],        [1]]), array([[1, 2]]))

进行选择

In [225]: x[np.ix_(row_indices,col_indices)]Out[225]: array([[76, 56],       [70, 47],       [46, 95],       [76, 56],       [92, 46]])

如OP所建议的,这实际上与执行2D数组版本的老式广播相同,该数组的2D数组row_indices将其元素/索引发送到axis=0,从而在处创建单例维度,axis=1从而允许使用进行广播col_indices。因此,我们将有一个类似的替代解决方案-

In [227]: x[np.asarray(row_indices)[:,None],col_indices]Out[227]: array([[76, 56],       [70, 47],       [46, 95],       [76, 56],       [92, 46]])

如前所述,对于作业,我们只是这样做。

行,列索引数组

In [36]: row_indices = [1, 4]In [37]: col_indices = [1, 3]

使用标量进行分配

In [38]: x[np.ix_(row_indices,col_indices)] = -1In [39]: xOut[39]: array([[17, 39, 88, 14, 73, 58, 17, 78],       [88, -1, 46, -1, 44, 81, 17, 67],       [31, 70, 47, 90, 52, 15, 24, 22],       [19, 59, 98, 19, 52, 95, 88, 65],       [85, -1, 56, -1, 43, 79, 53, 37],       [74, 46, 95, 27, 81, 97, 93, 69],       [49, 46, 12, 83, 15, 63, 20, 79]])

使用2D块(可广播数组)进行分配

In [40]: rand_arr = -np.arange(4).reshape(2,2)In [41]: x[np.ix_(row_indices,col_indices)] = rand_arrIn [42]: xOut[42]: array([[17, 39, 88, 14, 73, 58, 17, 78],       [88,  0, 46, -1, 44, 81, 17, 67],       [31, 70, 47, 90, 52, 15, 24, 22],       [19, 59, 98, 19, 52, 95, 88, 65],       [85, -2, 56, -3, 43, 79, 53, 37],       [74, 46, 95, 27, 81, 97, 93, 69],       [49, 46, 12, 83, 15, 63, 20, 79]]
  1. np.ix_
    masks

输入数组

In [19]: xOut[19]: array([[17, 39, 88, 14, 73, 58, 17, 78],       [88, 92, 46, 67, 44, 81, 17, 67],       [31, 70, 47, 90, 52, 15, 24, 22],       [19, 59, 98, 19, 52, 95, 88, 65],       [85, 76, 56, 72, 43, 79, 53, 37],       [74, 46, 95, 27, 81, 97, 93, 69],       [49, 46, 12, 83, 15, 63, 20, 79]])

输入行,列掩码

In [20]: row_mask = np.array([0,1,1,0,0,1,0],dtype=bool)In [21]: col_mask = np.array([1,0,1,0,1,1,0,0],dtype=bool)

进行选择

In [22]: x[np.ix_(row_mask,col_mask)]Out[22]: array([[88, 46, 44, 81],       [31, 47, 52, 15],       [74, 95, 81, 97]])

使用标量进行分配

In [23]: x[np.ix_(row_mask,col_mask)] = -1In [24]: xOut[24]: array([[17, 39, 88, 14, 73, 58, 17, 78],       [-1, 92, -1, 67, -1, -1, 17, 67],       [-1, 70, -1, 90, -1, -1, 24, 22],       [19, 59, 98, 19, 52, 95, 88, 65],       [85, 76, 56, 72, 43, 79, 53, 37],       [-1, 46, -1, 27, -1, -1, 93, 69],       [49, 46, 12, 83, 15, 63, 20, 79]])

使用2D块(可广播数组)进行分配

In [25]: rand_arr = -np.arange(12).reshape(3,4)In [26]: x[np.ix_(row_mask,col_mask)] = rand_arrIn [27]: xOut[27]: array([[ 17,  39,  88,  14,  73,  58,  17,  78],       [  0,  92,  -1,  67,  -2,  -3,  17,  67],       [ -4,  70,  -5,  90,  -6,  -7,  24,  22],       [ 19,  59,  98,  19,  52,  95,  88,  65],       [ 85,  76,  56,  72,  43,  79,  53,  37],       [ -8,  46,  -9,  27, -10, -11,  93,  69],       [ 49,  46,  12,  83,  15,  63,  20,  79]])


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

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

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