numpy 多维索引:使用 np 数组和列表给出不同的结果
numpy multidimensional indexing :use np array and list give different results
为什么 b1 b2 给出不同的结果,但 b0 ,b1,b3 给出相同的结果。得到一个
"FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)]
instead of arr[seq]
. In the future this will be interpreted as an array index, arr[np.array(seq)]
, which will result either in an error or a different result."
a = np.array([[6, 7], [8, 9]])
print(a.shape)
print(a)
print()
b0 = a[[0, 0]]
print('b0')
print(b0.shape)
print(b0)
print()
b1 = a[[[0, 0]]]
print('b1')
print(b1.shape)
print(b1)
print()
b2 = a[np.array([[0, 0]])]
print('b2')
print(b2.shape)
print(b2)
print()
b3= a[np.array([0, 0])]
print('b3')
print(b3.shape)
print(b3)
(2, 2)
[[6 7]
[8 9]]
b0
(2, 2)
[[6 7]
[6 7]]
b1
(2, 2)
[[6 7]
[6 7]]
b2
(1, 2, 2)
[[[6 7]
[6 7]]]
b3
(2, 2)
[[6 7]
[6 7]]
作为历史事件
b1 = a[[[0, 0]]]
was/is 评价为
b1 = a[([0, 0],)]
b1 = a[[0, 0]]
警告告诉我们这个异常正在被纠正。
像 a[i,j]
这样的索引实际上是 a[(i,j)]
,将元组传递给 getitem
方法。由于过去各种包的合并方式,在某些情况下,列表以相同的方式解释。你找到了一个。
未来警告告诉我们,进行高级索引的最干净、最清晰的方法是使用元组和数组。在使用列表时,过去和现在仍然存在歧义。
这在 1.15.0 发行说明弃用中有所描述,
Multidimensional indexing with anything but a tuple is deprecated. This means that
the index list in ind = [slice(None), 0]; arr[ind] should be changed to a tuple,
e.g., ind = [slice(None), 0]; arr[tuple(ind)] or arr[(slice(None), 0)].
That change is necessary to avoid ambiguity in expressions such as
arr[[[0, 1], [0, 1]]], interpreted as arr[array([0, 1]), array([0, 1])],
that will be interpreted as arr[array([[0, 1], [0, 1]])] in the future.
所以将来 b1 的行为会像 b2,但现在它会像 b0。
为什么 b1 b2 给出不同的结果,但 b0 ,b1,b3 给出相同的结果。得到一个
"FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)]
instead of arr[seq]
. In the future this will be interpreted as an array index, arr[np.array(seq)]
, which will result either in an error or a different result."
a = np.array([[6, 7], [8, 9]])
print(a.shape)
print(a)
print()
b0 = a[[0, 0]]
print('b0')
print(b0.shape)
print(b0)
print()
b1 = a[[[0, 0]]]
print('b1')
print(b1.shape)
print(b1)
print()
b2 = a[np.array([[0, 0]])]
print('b2')
print(b2.shape)
print(b2)
print()
b3= a[np.array([0, 0])]
print('b3')
print(b3.shape)
print(b3)
(2, 2) [[6 7] [8 9]]
b0 (2, 2) [[6 7] [6 7]]
b1 (2, 2) [[6 7] [6 7]]
b2 (1, 2, 2) [[[6 7] [6 7]]]
b3 (2, 2) [[6 7] [6 7]]
作为历史事件
b1 = a[[[0, 0]]]
was/is 评价为
b1 = a[([0, 0],)]
b1 = a[[0, 0]]
警告告诉我们这个异常正在被纠正。
像 a[i,j]
这样的索引实际上是 a[(i,j)]
,将元组传递给 getitem
方法。由于过去各种包的合并方式,在某些情况下,列表以相同的方式解释。你找到了一个。
未来警告告诉我们,进行高级索引的最干净、最清晰的方法是使用元组和数组。在使用列表时,过去和现在仍然存在歧义。
这在 1.15.0 发行说明弃用中有所描述,
Multidimensional indexing with anything but a tuple is deprecated. This means that
the index list in ind = [slice(None), 0]; arr[ind] should be changed to a tuple,
e.g., ind = [slice(None), 0]; arr[tuple(ind)] or arr[(slice(None), 0)].
That change is necessary to avoid ambiguity in expressions such as
arr[[[0, 1], [0, 1]]], interpreted as arr[array([0, 1]), array([0, 1])],
that will be interpreted as arr[array([[0, 1], [0, 1]])] in the future.
所以将来 b1 的行为会像 b2,但现在它会像 b0。