numpy索引文档中的错误示例?

2024-05-12 13:32:35 发布

您现在位置:Python中文网/ 问答频道 /正文

在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.

文件中有错误吗?还是我误解了什么


Tags: ofthetonumpyidbasebasicis