简体   繁体   English

在NumPy数组中的多个索引处获取元素

[英]Getting elements at multiple indices in a NumPy array

I am trying to access specific rows and columns of a NumPy array as the documentation explains but I think I am missing something. 我正在尝试按照文档说明访问NumPy数组的特定行和列,但我认为我缺少一些东西。

I have the following array: 我有以下数组:

arr = np.random.randint(10, size=(6, 4))
array([[1, 9, 6, 4],
       [8, 5, 0, 3],
       [3, 7, 3, 2],
       [1, 4, 8, 0],
       [5, 5, 8, 0],
       [0, 6, 4, 9]])

And I want to get the first and last row; 我想获得第一行和最后一行; and the first, third and last column, so I am trying: 第一列,第三列和最后一列,所以我正在尝试:

arr[(0, -1),(0, 1, 3)]

But this is producing the following error: 但这会产生以下错误:

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,)

I think I am misunderstanding this type of integer indexing. 我想我误解了这种类型的整数索引。 I would expect this output: 我期望这个输出:

array([[1, 9, 4],
       [0, 6, 9]])

I can do it this way but it feels really awkward: 我可以这样,但是感觉很尴尬:

arr[(0,-1),:][:,(0,1,3)]

How can I get the i-th elements of different dimensions? 如何获得不同尺寸的第i个元素?

You are looking for np.ix_ : 您正在寻找np.ix_

arr[np.ix_((0, -1),(0, 1, 3))]

Results in 结果是

array([[1, 9, 4],
       [0, 6, 9]])

One way is to explicitly specify the index of each element of the result, where the key difference is that the row specification should be 2d: 一种方法是显式指定结果的每个元素的索引,主要区别在于行规范应为2d:

>>> np.random.seed(444)
>>> arr = np.random.randint(10, size=(6, 4))
>>> arr
array([[3, 0, 7, 8],
       [3, 4, 7, 6],
       [8, 9, 2, 2],
       [2, 0, 3, 8],
       [0, 6, 6, 0],
       [3, 0, 6, 7]])

>>> rows = [[0],
...         [-1]]
>>> cols = [0, 1, -1]
>>> arr[rows, cols]
array([[3, 0, 8],
       [3, 0, 7]])

This example takes advantage of broadcasting for the row index. 本示例利用广播来获取行索引。 In other words, you can take advantage of the fact that the rows are repeating themselves for the remaining two columns in addition to the 0th. 换句话说,您可以利用以下事实:除了第0行之外,其余两列在重复行。

To use advanced indexing one needs to select all elements explicitly. 要使用高级索引,需要明确选择所有元素。 ... However, since the indexing arrays above just repeat themselves, broadcasting can be used. ...但是,由于上面的索引数组只会重复自身,因此可以使用广播。 An example where you could not take advantage of broadcasting, and would need to specify each individual index, would be something like: 例如,您可能无法利用广播的优势,而需要指定每个索引,例如:

rows = [[1, 2],
        [3, 4]]
cols = [[2, 3],
        [1, 0]]

In that case, you're selecting elements at (1, 2), (2, 3), ... 在这种情况下,您要选择(1、2),(2、3),...

See this example from the docs. 请参阅文档中的示例。

You might notice that this is exactly what is produced by np.ix_ (though I'm not saying it's not a useful function): 您可能会注意到,这正是np.ix_产生的(尽管我并不是说它不是有用的函数):

>>> rows, cols = np.ix_((0, -1),(0, 1, 3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM