numpy数组索引与列表和数组

时间:2022-09-03 21:44:35

I have:

我有:

>>> 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])])

When I do

当我做

>>> a[l] # Case 1, l is a list of scalers

I get

我明白了

array([[1, 2],
       [3, 4]])

which means indexing happened only on 0th axis.

这意味着索引只发生在第0轴上。

But when I do

但是,当我这样做

>>> a[j] # Case 2, j is a list of numpy arrays

I get

我明白了

array([1, 4])

which means indexing happened along axis 0 and axis 1.

这意味着沿轴0和轴1发生索引。

Q1: When used for indexing, why is there a difference in treatment of list of scalers and list of numpy arrays ? (Case 1 vs Case 2). In Case 2, I was hoping to see indexing happen only along axis 0 and get

Q1:当用于索引时,为什么缩放器列表和numpy数组列表的处理存在差异? (案例1与案例2)。在案例2中,我希望看到索引仅沿轴0发生并获得

array( [[[1,2],
          [3,4]], 

        [[1,2],
         [3,4]]])

Now, when using numpy array of arrays instead

现在,当使用numpy数组的数组时

>>> j1 = np.array(j) # numpy array of arrays

The result below indicates that indexing happened only along axis 0 (as expected)

下面的结果表明索引只发生在轴0上(如预期的那样)

>>> a[j1] Case 3, j1 is a numpy array of numpy arrays
array([[[1, 2],
        [3, 4]],

       [[1, 2],
        [3, 4]]])

Q2: When used for indexing, why is there a difference in treatment of list of numpy arrays and numpy array of numpy arrays? (Case 2 vs Case 3)

Q2:当用于索引时,为什么numpy数组列表和numpy数组numpy数组的处理存在差异? (案例2与案例3)

2 个解决方案

#1


1  

Case1, a[l] is actually a[(l,)] which expands to a[(l, slice(None))]. That is, indexing the first dimension with the list l, and an automatic trailing : slice. Indices are passed as a tuple to the array __getitem__, and extra () may be added without confusion.

Case1,a [l]实际上是[(l,)],它扩展为[(l,slice(None))]。也就是说,使用列表l索引第一个维度,并使用自动尾随:slice。索引作为元组传递给数组__getitem__,并且可以添加extra()而不会产生混淆。

Case2, a[j] is treated as a[array([0, 1]), array([0, 1]] or a[(array(([0, 1]), array([0, 1])]. In other words, as a tuple of indexing objects, one per dimension. It ends up returning a[0,0] and a[1,1].

Case2,a [j]被视为[array([0,1]),array([0,1]]或[(array(([0,1]),array([0,1])换句话说,作为索引对象的元组,每个维度一个。它最终返回[0,0]和[1,1]。

Case3, a[j1] is a[(j1, slice(None))], applying the j1 index to just the first dimension.

Case3,a [j1]是[(j1,slice(None))],将j1索引应用于第一维。

Case2 is a bit of any anomaly. Your intuition is valid, but for historical reasons, this list of arrays (or list of lists) is interpreted as a tuple of arrays.

Case2有点异常。你的直觉是有效的,但由于历史原因,这个数组列表(或列表列表)被解释为数组的元组。

This has been discussed in other SO questions, and I think it is documented. But off hand I can't find those references.

这已经在其他SO问题中讨论过,我认为它已被记录在案。但手头我找不到那些参考文献。

So it's safer to use either a tuple of indexing objects, or an array. Indexing with a list has a potential ambiguity.

因此,使用索引对象元组或数组更安全。使用列表进行索引具有潜在的模糊性。


numpy array indexing: list index and np.array index give different result

numpy数组索引:list index和np.array index给出不同的结果

This SO question touches on the same issue, though the clearest statement of what is happening is buried in a code link in a comment by @user2357112.

这个问题触及了同样的问题,尽管最清楚地说明正在发生的事情被@ user2357112隐藏在评论中的代码链接中。

Another way of forcing the Case3 like indexing, make the 2nd dimension slice explicit, a[j,:]

强制Case3的另一种方式,如索引,使第二维切片显式,a [j,:]

In [166]: a[j]
Out[166]: array([1, 4])
In [167]: a[j,:]
Out[167]: 
array([[[1, 2],
        [3, 4]],

       [[1, 2],
        [3, 4]]])

(I often include the trailing : even if it isn't needed. It makes it clear to me, and readers, how many dimensions we are working with.)

(我经常包括尾随:即使不需要它。它让我和读者清楚地知道我们正在使用多少维度。)

#2


0  

A1: The structure of l is not the same as j.

A1:l的结构与j不同。

l is just one-dimension while j is two-dimension. If you change one of them:

l只是一维而j是二维的。如果您更改其中一个:

# l = [0, 1]                                 # just one dimension!
l = [[0, 1], [0, 1]]                         # two dimensions
j = [np.array([0,1]), np.array([0, 1])]      # two dimensions

They have the same behave.

他们有相同的行为。

A2: The same, the structure of arrays in Case 2 and Case 3 are not the same.

A2:同样,案例2和案例3中的数组结构也不尽相同。

#1


1  

Case1, a[l] is actually a[(l,)] which expands to a[(l, slice(None))]. That is, indexing the first dimension with the list l, and an automatic trailing : slice. Indices are passed as a tuple to the array __getitem__, and extra () may be added without confusion.

Case1,a [l]实际上是[(l,)],它扩展为[(l,slice(None))]。也就是说,使用列表l索引第一个维度,并使用自动尾随:slice。索引作为元组传递给数组__getitem__,并且可以添加extra()而不会产生混淆。

Case2, a[j] is treated as a[array([0, 1]), array([0, 1]] or a[(array(([0, 1]), array([0, 1])]. In other words, as a tuple of indexing objects, one per dimension. It ends up returning a[0,0] and a[1,1].

Case2,a [j]被视为[array([0,1]),array([0,1]]或[(array(([0,1]),array([0,1])换句话说,作为索引对象的元组,每个维度一个。它最终返回[0,0]和[1,1]。

Case3, a[j1] is a[(j1, slice(None))], applying the j1 index to just the first dimension.

Case3,a [j1]是[(j1,slice(None))],将j1索引应用于第一维。

Case2 is a bit of any anomaly. Your intuition is valid, but for historical reasons, this list of arrays (or list of lists) is interpreted as a tuple of arrays.

Case2有点异常。你的直觉是有效的,但由于历史原因,这个数组列表(或列表列表)被解释为数组的元组。

This has been discussed in other SO questions, and I think it is documented. But off hand I can't find those references.

这已经在其他SO问题中讨论过,我认为它已被记录在案。但手头我找不到那些参考文献。

So it's safer to use either a tuple of indexing objects, or an array. Indexing with a list has a potential ambiguity.

因此,使用索引对象元组或数组更安全。使用列表进行索引具有潜在的模糊性。


numpy array indexing: list index and np.array index give different result

numpy数组索引:list index和np.array index给出不同的结果

This SO question touches on the same issue, though the clearest statement of what is happening is buried in a code link in a comment by @user2357112.

这个问题触及了同样的问题,尽管最清楚地说明正在发生的事情被@ user2357112隐藏在评论中的代码链接中。

Another way of forcing the Case3 like indexing, make the 2nd dimension slice explicit, a[j,:]

强制Case3的另一种方式,如索引,使第二维切片显式,a [j,:]

In [166]: a[j]
Out[166]: array([1, 4])
In [167]: a[j,:]
Out[167]: 
array([[[1, 2],
        [3, 4]],

       [[1, 2],
        [3, 4]]])

(I often include the trailing : even if it isn't needed. It makes it clear to me, and readers, how many dimensions we are working with.)

(我经常包括尾随:即使不需要它。它让我和读者清楚地知道我们正在使用多少维度。)

#2


0  

A1: The structure of l is not the same as j.

A1:l的结构与j不同。

l is just one-dimension while j is two-dimension. If you change one of them:

l只是一维而j是二维的。如果您更改其中一个:

# l = [0, 1]                                 # just one dimension!
l = [[0, 1], [0, 1]]                         # two dimensions
j = [np.array([0,1]), np.array([0, 1])]      # two dimensions

They have the same behave.

他们有相同的行为。

A2: The same, the structure of arrays in Case 2 and Case 3 are not the same.

A2:同样,案例2和案例3中的数组结构也不尽相同。