在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
不是视图,因为它是标量,而不是数组。基本切片生成的所有数组始终是原始数组的视图相关问题 更多 >
编程相关推荐