在NumPy中使用2维进行索引时出错

时间:2021-08-21 21:27:02

Why does this work:

为什么这样做:

>>> (tf[:,[91,1063]])[[0,3,4],:]
array([[ 0.04480133,  0.01079433],
       [ 0.11145042,  0.        ],
       [ 0.01177578,  0.01418614]])

But this does not:

但这不是:

>>> tf[[0,3,4],[91,1063]]
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,) 

What am I doing wrong?

我究竟做错了什么?

1 个解决方案

#1


tf[:,[91,1063]])[[0,3,4],:]

operates in 2 steps, first selecting 2 columns, and then 3 rows from that result

以两个步骤操作,首先选择2列,然后从该结果中选择3行

tf[[0,3,4],[91,1063]]

tries to select tf[0,91], tf[3,1063] and ft[4, oops].

尝试选择tf [0,91],tf [3,1063]和ft [4,oops]。

tf[[[0],[3],[4]], [91,1063]]

should work, giving the same result as your first expression. think of the 1st list being a column, selecting rows.

应该工作,给出与第一个表达式相同的结果。将第一个列表视为一列,选择行。

tf[np.array([0,3,4])[:,newaxis], [91,1063]]

is another way of generating that column index array

是另一种生成列索引数组的方法

tf[np.ix_([0,3,4],[91,1063])]

np.ix_ can help generate these index arrays.

np.ix_可以帮助生成这些索引数组。

In [140]: np.ix_([0,3,4],[91,1063])
Out[140]: 
(array([[0],
        [3],
        [4]]), array([[  91, 1063]]))

These column and row arrays are broadcast together to produce a 2d array of coordinates

这些列和行数组一起广播以产生2d坐标数组

[[(0,91), (0,1063)]
 [(3,91), ...     ]
 ....             ]]

This is the relevant part of the docs: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing

这是文档的相关部分:http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing

I'm basically repeating my answer to Composite Index updates for Numpy Matrices

我基本上重复了我对Numpy Matrices的综合索引更新的答案

#1


tf[:,[91,1063]])[[0,3,4],:]

operates in 2 steps, first selecting 2 columns, and then 3 rows from that result

以两个步骤操作,首先选择2列,然后从该结果中选择3行

tf[[0,3,4],[91,1063]]

tries to select tf[0,91], tf[3,1063] and ft[4, oops].

尝试选择tf [0,91],tf [3,1063]和ft [4,oops]。

tf[[[0],[3],[4]], [91,1063]]

should work, giving the same result as your first expression. think of the 1st list being a column, selecting rows.

应该工作,给出与第一个表达式相同的结果。将第一个列表视为一列,选择行。

tf[np.array([0,3,4])[:,newaxis], [91,1063]]

is another way of generating that column index array

是另一种生成列索引数组的方法

tf[np.ix_([0,3,4],[91,1063])]

np.ix_ can help generate these index arrays.

np.ix_可以帮助生成这些索引数组。

In [140]: np.ix_([0,3,4],[91,1063])
Out[140]: 
(array([[0],
        [3],
        [4]]), array([[  91, 1063]]))

These column and row arrays are broadcast together to produce a 2d array of coordinates

这些列和行数组一起广播以产生2d坐标数组

[[(0,91), (0,1063)]
 [(3,91), ...     ]
 ....             ]]

This is the relevant part of the docs: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing

这是文档的相关部分:http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing

I'm basically repeating my answer to Composite Index updates for Numpy Matrices

我基本上重复了我对Numpy Matrices的综合索引更新的答案