我有:
>>> a
array([[1, 2],
[3, 4]])
>>> type(l), l # list of scalers
(<type 'list'>, [0, 1])
>>> type(i), i # a numpy array
(<type 'numpy.ndarray'>, array([0, 1]))
>>> type(j), j # list of numpy arrays
(<type 'list'>, [array([0, 1]), array([0, 1])])
当我执行时
>>> a[l] # Case 1, l is a list of scalers
我明白了
array([[1, 2],
[3, 4]])
这意味着索引仅在第 0 轴上发生。
但是,当我执行以下操作时:
>>> a[j] # Case 2, j is a list of numpy arrays
我理解了
array([1, 4])
这意味着索引沿轴0和轴1发生。
Q1:在用于索引时,为什么列表标量和numpy数组列表的处理有所不同?(情况1与情况2)。在情况2中,我希望只沿轴0进行索引,并得到
array( [[[1,2],
[3,4]],
[[1,2],
[3,4]]])
现在,当使用numpy数组的数组时:
>>> j1 = np.array(j) # numpy array of arrays
以下结果表明,索引仅沿轴0发生(如预期)。
>>> a[j1] Case 3, j1 is a numpy array of numpy arrays
array([[[1, 2],
[3, 4]],
[[1, 2],
[3, 4]]])
问题2:当用于索引时,为什么对于numpy数组列表和numpy数组的numpy数组有所不同?(情况2与情况3)
k = [[0,1], [0,1]]; a[k]
不是一样的吗?在这种情况下,您会看到列表和Numpy数组列表之间的相同行为。 - andrew_reece[a[jj] for jj in j]
给出了你在 Q1 中所期望的结果。输入一个列表的列表,无论是 Numpy 数组还是其他类型,基本上都是一次返回一组索引。 - andrew_reece