使用2个索引列表对2D Numpy数组进行索引

45

我有一个奇怪的情况。

我有一个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]

包括一个示例案例? - Divakar
1
挑剔一点:np.random.randint(0, 6)np.random.random_integers(0, 5)更受欢迎。 - Mad Physicist
那种情况的预期输出是什么? - Divakar
我期望的是 x_indexed...因此,我需要根据索引获取行,列由列索引指定。 - Chris
3
请尝试以下代码:x_new = x[row_indices,:][:,col_indices] - Dataman
显示剩余3条评论
5个回答

57

使用索引或布尔数组/掩码进行选取或赋值的np.ix_

1. 使用索引数组

A. 选取

我们可以使用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_一起使用,类似于索引数组的使用。这也可用于选择输入数组中的一个块,并用于将其分配给它。

A. 选择

因此,使用row_maskcol_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]])

啊,那么如果我对row_indices进行转置,应该是一样的吧? - Chris

12

怎么样:

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]])

5
对于提取数据来说这很好,但对于赋值操作来说失败了。行索引会创建一个副本。 - hpaulj
@hpaulj 都因为使用高级索引而创建了副本,是吗? - monolith
@wedran,使用一层索引时,x[[1,2,3]] = 2,视图和副本之间的区别并不重要(x.__setitem__(..., 2))。只有在使用顺序索引时,您才需要注意这个问题。然后,__setitem__ 修改第一个 __getitem__ - hpaulj

7
import numpy as np
x = np.random.random_integers(0,5,(4,4))
x
array([[5, 3, 3, 2],
       [4, 3, 0, 0],
       [1, 4, 5, 3],
       [0, 4, 3, 4]])

# This indexes the elements 1,1 and 2,2 and 3,3
indexes = (np.array([1,2,3]),np.array([1,2,3]))
x[indexes]
# returns array([3, 5, 4])

请注意,numpy根据您使用的索引种类有非常不同的规则。因此,索引多个元素应该是一个np.ndarraytuple(请参见索引手册)。
因此,您只需要将您的list转换为np.ndarray,它应该按预期工作。

当“indexes”元组的元素大小不同时,似乎它无法工作。 - Shihab Shahriar Khan
@ShihabShahriar 不同的大小表示什么?你有部分索引吗?如果是这样,那似乎你有一个不同的问题,请提出一个新的问题。 - MSeifert

5

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

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]

我喜欢这个表述的明确。 - eric

4

如果你使用np.newaxis编写代码,第一次尝试就会生效。

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

我认为你可能需要将row[column]_indices更改为np.array。 - Joonho Park

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接