关于numpy索引page ,有一个警告段落
The definition of advanced indexing means that x[(1,2,3),] is fundamentally different than x[(1,2,3)]. The latter is equivalent to x[1,2,3] which will trigger basic selection while the former will trigger advanced indexing. Be sure to understand why this occurs.
我尝试运行以下代码
import numpy as np
x = np.arange(3*4).reshape((3, 4))
y = x[(1, 2)]
z = x[(1, 2),]
print("base:", x.base, y.base, z.base)
print("id:", id(x.base), id(y.base), id(z.base))
print(np.shares_memory(x, y), np.shares_memory(x, z))
得到的结果为
base: [ 0 1 2 3 4 5 6 7 8 9 10 11] None None
id: 4299634928 4297628200 4297628200
False False
似乎 y
没有返回 view因此 x[(1, 2)]
不能成为基本索引,因为
All arrays generated by basic slicing are always views of the original array.
文档中是否有错误?还是我哪里理解错了?
最佳答案
y
不是 View ,因为它是标量,而不是数组。通过基本切片生成的所有数组始终是原始数组的 View 。
关于python - numpy 索引文档中的错误示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50802238/