本文介绍了用2个索引列表索引2D Numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的情况.

I've got a strange situation.

我有一个二维的Numpy数组,x:

I have a 2D Numpy array, x:

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

我有2个索引器-一个索引为行,一个索引为列.为了索引X,我必须执行以下操作:

And I have 2 indexers--one with indices for the rows, and one with indices for the column. In order to index X, I am having to do the following:

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]

(失败:错误,无法通过(2,)广播(20,))

(which fails with: error, cannot broadcast (20,) with (2,))

我希望能够使用广播在一行中建立索引,因为这样可以使代码保持干净和可读性...而且,我对幕后的python并不太了解,但是据我所知,它在一行中应该更快(并且我将使用相当大的数组).

I'd like to be able to do the indexing in one line using the broadcasting, since that would keep the code clean and readable...also, I don't know all that much about python under the hood, but as I understand it, it should be faster to do it in one line (and I'll be working with pretty big arrays).

测试用例:

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]

推荐答案

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

1.使用indexing-arrays

A.选择

Selections or assignments with np.ix_ using indexing or boolean arrays/masks

1. With indexing-arrays

A. Selection

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

We can use np.ix_ to get a tuple of indexing arrays that are broadcastable against each other to result in a higher-dimensional combinations of indices. So, when that tuple is used for indexing into the input array, would give us the same higher-dimensional array. Hence, to make a selection based on two 1D indexing arrays, it would be -

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

B.分配

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

We can use the same notation for assigning scalar or a broadcastable array into those indexed positions. Hence, the following works for assignments -

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

2.使用masks

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

2. With masks

We can also use boolean arrays/masks with np.ix_, similar to how indexing arrays are used. This can be used again to select a block off the input array and also for assignments into it.

A.选择

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

Thus, with row_mask and col_mask boolean arrays as the masks for row and column selections respectively, we can use the following for selections -

x[np.ix_(row_mask,col_mask)]

B.分配

以下内容适用于作业-

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


样品运行

1.将np.ix_indexing-arrays


Sample Runs

1. Using np.ix_ with indexing-arrays

输入数组和索引数组-

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建议 ,实际上与使用row_indices的2D数组版本的 old-school 广播相同,该版本的元素/索引被发送axis=0,从而创建axis=1处的单个尺寸,因此允许使用col_indices进行广播.因此,我们将有一个类似的替代解决方案-

As suggested by OP, this is in effect same as performing old-school broadcasting with a 2D array version of row_indices that has its elements/indices sent to axis=0 and thus creating a singleton dimension at axis=1 and thus allowing broadcasting with col_indices. Thus, we would have an alternative solution like so -

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

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

As discussed earlier, for the assignments, we simply do so.

行,列索引数组-

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块(可广播数组)进行分配-

Make assignments with 2D block(broadcastable array) -

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

2. Using np.ix_ with 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]])

输入行,col掩码-

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

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

Make assignments with 2D block(broadcastable array) -

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

这篇关于用2个索引列表索引2D Numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:23