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

标签 python numpy numpy-ndarray

关于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/

相关文章:

python - 如何在不将其放入列表的情况下处理一行 for 循环?

python - MultiheadAttention 的可学习参数数量

python - 非常简单的信号处理

python - 在 numpy 中将数组拼接在一起

python - knn 的 y 轴样本不匹配

python - Spyder 随机无法定位 chromedriver

python re.search 匹配 csv.reader 中的所有内容?

python - 如何使用 pandas/matplotlib 绘制/管理 2 列分类数据?

Python:求解矩阵方程 Ax = b,其中 A 包含变量

python - 合并按列中的值分组的 2d numpy 数组