[英]Index a 2D Numpy array with 2 lists of indices
I've got a strange situation.我有一个奇怪的情况。
I have a 2D Numpy array, x:我有一个 2D Numpy 数组,x:
x = np.random.random_integers(0,5,(20,8))
And I have 2 indexers--one with indices for the rows, and one with indices for the column.我有 2 个索引器——一个带有行索引,一个带有列索引。 In order to index X, I am having to do the following:
为了索引 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]
Instead of just:而不仅仅是:
x_new = x[row_indices,column_indices]
(which fails with: error, cannot broadcast (20,) with (2,)) (失败:错误,不能用(2,)广播(20,))
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).我希望能够使用广播在一行中进行索引,因为这将使代码保持干净和可读......此外,我对底层的 python 了解不多,但据我所知它,在一行中完成它应该更快(我将使用相当大的数组)。
Test Case:测试用例:
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_
using indexing or boolean arrays/masksnp.ix_
进行选择或分配indexing-arrays
indexing-arrays
A. Selection一个选择
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.我们可以使用
np.ix_
来获取索引数组的元组,这些数组可以相互广播,以产生更高维的索引组合。 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 -因此,要根据两个
1D
引数组进行选择,它将是 -
x_indexed = x[np.ix_(row_indices,col_indices)]
B. Assignment 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
masks
masks
We can also use boolean arrays/masks with np.ix_
, similar to how indexing arrays are used.我们还可以将布尔数组/掩码与
np.ix_
一起np.ix_
,类似于索引数组的使用方式。 This can be used again to select a block off the input array and also for assignments into it.这可以再次用于从输入数组中选择一个块,也可以用于分配给它。
A. Selection一个选择
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 -因此,使用
row_mask
和col_mask
布尔数组分别作为行和列选择的掩码,我们可以使用以下选择 -
x[np.ix_(row_mask,col_mask)]
B. Assignment B. 分配
And the following works for assignments -以下适用于作业 -
x[np.ix_(row_mask,col_mask)] = # scalar or broadcastable array
1. Using np.ix_
with indexing-arrays
1. 使用
np.ix_
和indexing-arrays
Input array and 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]
Tuple of indexing arrays with np.ix_
-带有
np.ix_
的索引数组元组 -
In [224]: np.ix_(row_indices,col_indices) # Broadcasting of indices
Out[224]:
(array([[4],
[2],
[5],
[4],
[1]]), array([[1, 2]]))
Make selections -做出选择——
In [225]: x[np.ix_(row_indices,col_indices)]
Out[225]:
array([[76, 56],
[70, 47],
[46, 95],
[76, 56],
[92, 46]])
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
.正如OP所建议的那样,这实际上与使用
row_indices
的二维数组版本执行老式广播相同,该版本的元素/索引发送到axis=0
,从而在axis=1
处创建单例维度,从而允许使用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.如前所述,对于分配,我们只是这样做。
Row, col indexing arrays -行、列索引数组 -
In [36]: row_indices = [1, 4]
In [37]: col_indices = [1, 3]
Make assignments with scalar -使用标量进行分配 -
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]])
Make assignments with 2D block(broadcastable array) -使用 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. Using np.ix_
with masks
2. 使用带有
masks
np.ix_
Input array -输入数组 -
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]])
Input row, col masks -输入行,列掩码 -
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)
Make selections -做出选择——
In [22]: x[np.ix_(row_mask,col_mask)]
Out[22]:
array([[88, 46, 44, 81],
[31, 47, 52, 15],
[74, 95, 81, 97]])
Make assignments with scalar -使用标量进行分配 -
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]])
Make assignments with 2D block(broadcastable array) -使用 2D 块(可广播阵列)进行分配 -
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]])
What about:关于什么:
x[row_indices][:,col_indices]
For example,例如,
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]])
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])
Notice that numpy has very different rules depending on what kind of indexes you use.请注意,根据您使用的索引类型,numpy 有非常不同的规则。 So indexing several elements should be by a
tuple
of np.ndarray
(see indexing manual ).所以索引几个元素应该由
np.ndarray
tuple
(参见 索引手册)。
So you need only to convert your list
to np.ndarray
and it should work as expected.所以你只需要将你的
list
转换为np.ndarray
并且它应该可以按预期工作。
I think you are trying to do one of the following (equlvalent) operations:我认为您正在尝试执行以下(等效)操作之一:
x_does_work = x[row_indices,:][:,col_indices]
x_does_work = x[:,col_indices][row_indices,:]
This will actually create a subset of x
with only the selected rows, then select the columns from that, or vice versa in the second case.这实际上将创建一个仅包含选定行的
x
子集,然后从中选择列,或者在第二种情况下反之亦然。 The first case can be thought of as第一种情况可以认为是
x_does_work = (x[row_indices,:])[:,col_indices]
如果你用 np.newaxis 编写它,你的第一次尝试会奏效
x_new = x[row_indices[:, np.newaxis],column_indices]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.