使用索引或布尔数组/掩码进行选取或赋值的np.ix_
1. 使用索引数组
A. 选取
我们可以使用np.ix_
获得一个元组形式的索引数组,它们可以相互广播以产生更高维度的索引组合。因此,当该元组用于对输入数组进行索引时,将会得到相同的高维数组。因此,如果要基于两个1D
索引数组进行选取,则可以采用以下方式 -
x_indexed = x[np.ix_(row_indices,col_indices)]
B. 赋值
我们可以使用相同的符号将标量或可传播数组赋值给这些索引位置。因此,以下内容适用于赋值 -
x[np.ix_(row_indices,col_indices)] =
2. 使用masks
我们也可以使用布尔数组/掩码与np.ix_
一起使用,类似于索引数组的使用。这也可用于选择输入数组中的一个块,并用于将其分配给它。
A. 选择
因此,使用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_
与 索引数组
输入数组和索引数组 -
In [221]: x
Out[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_indices
Out[222]: [4, 2, 5, 4, 1]
In [223]: col_indices
Out[223]: [1, 2]
np.ix_
的索引数组元组 -
In [224]: np.ix_(row_indices,col_indices) # Broadcasting of indices
Out[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建议的,这实际上与使用老派广播并创建一个在axis=1
上具有其元素/索引发送的row_indices
的二维数组版本相同,从而在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)] = -1
In [39]: x
Out[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_arr
In [42]: x
Out[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]])
2. 使用 np.ix_
与 masks
输入数组 -
In [19]: x
Out[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)] = -1
In [24]: x
Out[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]])
使用二维块(可广播数组)创建任务 -
In [25]: rand_arr = -np.arange(12).reshape(3,4)
In [26]: x[np.ix_(row_mask,col_mask)] = rand_arr
In [27]: x
Out[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]])
np.random.randint(0, 6)
比np.random.random_integers(0, 5)
更受欢迎。 - Mad Physicistx_indexed
...因此,我需要根据索引获取行,列由列索引指定。 - Chrisx_new = x[row_indices,:][:,col_indices]
。 - Dataman