简体   繁体   English

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

[英]Wrong examples in numpy indexing documentation?

On the numpy indexing page , there is a warning paragraph 在numpy索引页面上 ,有一个警告段落

The definition of advanced indexing means that x[(1,2,3),] is fundamentally different than x[(1,2,3)]. 高级索引的定义意味着x [(1,2,3),]与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. 后者等效于x [1,2,3],它将触发基本选择,而前者将触发高级索引。 Be sure to understand why this occurs. 确保了解为什么会发生这种情况。

I tried to run the following code 我试图运行以下代码

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

and got the results as 并得到了结果

base: [ 0  1  2  3  4  5  6  7  8  9 10 11] None None
id: 4299634928 4297628200 4297628200
False False

It seems that y doesn't return a view and thus x[(1, 2)] can't be a basic indexing because 似乎y不会返回视图 ,因此x[(1, 2)]不能作为基本索引,因为

All arrays generated by basic slicing are always views of the original array. 通过基本切片生成的所有阵列始终都是原始阵列的视图。

Is it a mistake in the documentation? 文档中有错误吗? Or did I misunderstand somewhere? 还是我误解了某个地方?

y isn't a view because it's a scalar, not an array. y不是视图,因为它是标量,而不是数组。 All arrays generated by basic slicing are always views of the original array. 通过基本切片生成的所有阵列始终都是原始阵列的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM