简体   繁体   English

numpy多维数组索引

[英]numpy multidimensional array indexing

I have a strange issue when I am doing indexing in a numpy multidimensional array. 当我在一个numpy多维数组中进行索引时,我有一个奇怪的问题。 So, I have an array of shape (4, 882). 所以,我有一个形状阵列(4,882)。 I have another array called matches of shape (276, 2). 我有另一个叫做形状匹配的数组(276,2)。 This match array holds indexes of valid entries in my original multidimensional array. 此匹配数组保存原始多维数组中有效条目的索引。 What I would like to do is select the first 2 rows and all the columns matching the indexes in the match array. 我想要做的是选择匹配数组中索引匹配的前2行和所有列。 So, I do something as follows: 所以,我做了如下的事情:

import numpy as np
k = get_array()  # This has shape (4, 882)
m = get_match()  # This has shape (276, 2)

s = k[[1, 0], m[:, 0]]

This raises the error: 这引发了错误:

ValueError: shape mismatch: objects cannot be broadcast to a single shape

However, when I do: 但是,当我这样做时:

s = k[[1, 0], :][:, m[:, 0]]

This works fine. 这很好用。 So, this is in effect selecting the subset of rows first and then the columns but I am not sure why my first attempt is wrong. 所以,这实际上首先选择行的子集然后选择列,但我不确定为什么我的第一次尝试是错误的。 Also, doing: 另外,做:

s = k[[1, 0], :]

of course works. 当然有效。

The error message is a little confusing because the shape mismatch isn't between k and m . 错误消息有点混乱,因为形状不匹配不在km之间。 It's between [1, 0] and m[:,0] . 它在[1, 0]m[:,0] Here are three ways to fix it, using the following (more easily visualizable) arrays: 以下是使用以下(更容易可视化的)数组来修复它的三种方法:

>>> k
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23]])
>>> m
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])
  1. Change the indexing list to a 2-d list with a shape that can be broadcasted against m : 将索引列表更改为具有可以针对m 广播的形状的二维列表:

     >>> k[[[1], [0]],m[:, 0]] array([[ 8, 10, 12, 14], [ 0, 2, 4, 6]]) 
  2. Convert it to an array and reshape the array: 将其转换为数组并重塑数组:

     >>> k[numpy.array([1, 0]).reshape(2, 1),m[:, 0]] array([[ 8, 10, 12, 14], [ 0, 2, 4, 6]]) 
  3. Convert it to an array and slice it using numpy.newaxis , aka None : 将其转换为数组并使用numpy.newaxis切割它,即None

     >>> k[numpy.array([1, 0])[:,None],m[:, 0]] array([[ 8, 10, 12, 14], [ 0, 2, 4, 6]]) 

There are many others, but these are the three that come most readily to mind. 还有很多其他的,但这些是最容易想到的三个。

You're getting an error because numpy needs indices passed in this way to have the same shape, or to be broadcastable to the same shape. 你得到一个错误,因为numpy需要以这种方式传递的索引具有相同的形状,或者可以播放到相同的形状。 By making the [1, 0] list a "column array," you make them broadcastable. 通过使[1, 0]列表成为“列数组”,可以使它们成为可广播的。 The same thing happens when you try to multiply m by [1, 0] : 当你尝试将m乘以[1, 0]时会发生同样的事情:

>>> m[:,0] * [0, 1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape

And all the same fixes apply. 所有相同的修复都适用。 For example: 例如:

>>> m[:,0] * [[0], [1]]
array([[0, 0, 0, 0],
       [0, 2, 4, 6]])

Finally, note that you could also fix it by passing a differently-shaped slice of m -- observe that the output is transposed: 最后,请注意你也可以通过传递一个不同形状的m片来修复它 - 观察输出是否被转置:

>>> k[[1, 0],m[:, 0:1]]
array([[ 8,  0],
       [10,  2],
       [12,  4],
       [14,  6]])

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

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