Python 使用2个索引列表为2D Numpy数组编制索引

Python 使用2个索引列表为2D Numpy数组编制索引,python,arrays,numpy,Python,Arrays,Numpy,我遇到了一个奇怪的情况 我有一个2D Numpy数组,x: x = np.random.random_integers(0,5,(20,8)) 我有两个索引器——一个是行索引,一个是列索引。为了索引X,我必须执行以下操作: row_indices = [4,2,18,16,7,19,4] col_indices = [1,2] x_rows = x[row_indices,:] x_indexed = x_rows[:,column_indices] 而不仅仅是: x_new = x[row

我遇到了一个奇怪的情况

我有一个2D Numpy数组,x:

x = np.random.random_integers(0,5,(20,8))
我有两个索引器——一个是行索引,一个是列索引。为了索引X,我必须执行以下操作:

row_indices = [4,2,18,16,7,19,4]
col_indices = [1,2]
x_rows = x[row_indices,:]
x_indexed = x_rows[:,column_indices]
而不仅仅是:

x_new = x[row_indices,column_indices]
(失败原因:错误,无法广播(20,)和(2,))


我希望能够使用广播在一行中进行索引,因为这样可以保持代码的干净性和可读性……此外,我对python的知识不太了解,但据我所知,在一行中进行索引应该更快(我将使用相当大的数组)


测试用例:

x = np.random.random_integers(0,5,(20,8))

row_indices = [4,2,18,16,7,19,4]
col_indices = [1,2]
x_rows = x[row_indices,:]
x_indexed = x_rows[:,col_indices]

x_doesnt_work = x[row_indices,col_indices]
请注意,numpy有非常不同的规则,这取决于您使用的索引类型。因此,应该通过
np.ndarray
tuple
对几个元素进行索引(请参阅)

因此,您只需将您的
列表
转换为
np.ndarray
,它就可以正常工作。

那么:

x[row_indices][:,col_indices]
比如说,

x = np.random.random_integers(0,5,(5,5))
## array([[4, 3, 2, 5, 0],
##        [0, 3, 1, 4, 2],
##        [4, 2, 0, 0, 3],
##        [4, 5, 5, 5, 0],
##        [1, 1, 5, 0, 2]])

row_indices = [4,2]
col_indices = [1,2]
x[row_indices][:,col_indices]
## array([[1, 5],
##        [2, 0]])
使用索引或布尔数组/掩码的
np.ix
选择或赋值 1.使用
索引数组
A.选择

我们可以使用来获得索引数组的元组,这些数组可以相互广播,从而产生更高维度的索引组合。因此,当该元组用于索引输入数组时,将为我们提供相同的高维数组。因此,要基于两个
1D
索引数组进行选择-

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

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

x[np.ix_(row_indices,col_indices)] = # scalar or broadcastable array
x[np.ix_(row_mask,col_mask)] = # scalar or broadcastable array
2.带
遮罩
我们还可以将布尔数组/掩码与
np.ix
一起使用,类似于索引数组的使用方式。这可再次用于从输入阵列中选择块,也可用于分配到其中

A.选择

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

x[np.ix_(row_mask,col_mask)]
In [225]: x[np.ix_(row_indices,col_indices)]
Out[225]: 
array([[76, 56],
       [70, 47],
       [46, 95],
       [76, 56],
       [92, 46]])
In [22]: x[np.ix_(row_mask,col_mask)]
Out[22]: 
array([[88, 46, 44, 81],
       [31, 47, 52, 15],
       [74, 95, 81, 97]])
B.分配

以下是作业的作业-

x[np.ix_(row_indices,col_indices)] = # scalar or broadcastable array
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]
In [36]: row_indices = [1, 4]

In [37]: col_indices = [1, 3]
带有
np.ix\uux
-

In [224]: np.ix_(row_indices,col_indices) # Broadcasting of indices
Out[224]: 
(array([[4],
        [2],
        [5],
        [4],
        [1]]), array([[1, 2]]))
选择-

x[np.ix_(row_mask,col_mask)]
In [225]: x[np.ix_(row_indices,col_indices)]
Out[225]: 
array([[76, 56],
       [70, 47],
       [46, 95],
       [76, 56],
       [92, 46]])
In [22]: x[np.ix_(row_mask,col_mask)]
Out[22]: 
array([[88, 46, 44, 81],
       [31, 47, 52, 15],
       [74, 95, 81, 97]])
同样,这实际上与使用二维阵列版本的
行索引
执行旧式广播相同,该二维阵列版本将其元素/索引发送到
轴=0
,从而在
轴=1
处创建单态维度,从而允许使用
列索引
进行广播。因此,我们将有一个这样的替代解决方案-

In [227]: x[np.asarray(row_indices)[:,None],col_indices]
Out[227]: 
array([[76, 56],
       [70, 47],
       [46, 95],
       [76, 56],
       [92, 46]])
如前所述,对于作业,我们只是这样做

行、列索引数组-

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]
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]])
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]])
使用2D块(可广播阵列)进行指定-

2。将
np.ix
掩码一起使用

输入阵列-

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)
选择-

x[np.ix_(row_mask,col_mask)]
In [225]: x[np.ix_(row_indices,col_indices)]
Out[225]: 
array([[76, 56],
       [70, 47],
       [46, 95],
       [76, 56],
       [92, 46]])
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 [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]])
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]])
使用2D块(可广播阵列)进行指定-


我认为您正在尝试执行以下(同等)操作之一:

x_does_work = x[row_indices,:][:,col_indices]
x_does_work = x[:,col_indices][row_indices,:]
这实际上将创建一个子集
x
,其中只包含选定的行,然后从中选择列,或者在第二种情况下反之亦然。第一个案例可以被认为是

x_does_work = (x[row_indices,:])[:,col_indices]

如果您使用np.newaxis编写它,那么您的第一次尝试将起作用

x_new = x[row_indices[:, np.newaxis],column_indices]

包括一个示例案例?挑剔:
np.random.randint(0,6)
适用于
np.random.random\u整数(0,5)
。该案例的预期输出是什么?您预期的结果是什么?您正在尝试获取所选行的第1列和第2列中的所有元素吗?请尝试以下操作:
x_new=x[row_index,:][:,col_index]
Ah,因此如果我采用
row_index
的转置,应该是相同的?当“indexes”元组的元素大小不同时,它似乎不一样work@ShihabShahriar不同尺寸表示什么?你有部分索引吗?看起来你有一个不同的问题,如果是这样的话,请问一个新的问题。这对于fetch来说是可以的,但是对于赋值来说是失败的。行索引生成副本。@hpaulj都是因为高级索引而生成副本的,对吗?@wedran,有一层索引,
x[[1,2,3]]=2
,视图和副本之间的区别无关紧要(
x.\uuu setitem\uuuuu(…,2)
。当您使用顺序索引时,您必须注意这个问题。然后,
\uuuuuu setitem\uuuuuuu
修改第一个
\uuuu getitem\uuuuuuu
。我认为,您可能需要将行[列]索引更改为np.array。