numpy - 为什么 Z[(0,2)] 是视图而 Z[(0, 2), (0)] 是副本? [英] numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?

查看:77
本文介绍了numpy - 为什么 Z[(0,2)] 是视图而 Z[(0, 2), (0)] 是副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 numpy 元组索引行为不一致?请解释这些行为背后的理性或设计决策.在我的理解中,Z[(0,2)]Z[(0, 2), (0)] 都是 元组索引并期望复制/查看的一致行为.如果不正确,请解释,

Why are the numpy tuple indexing behaviors inconsistent? Please explain the rational or design decision behind these behaviors. In my understanding, Z[(0,2)] and Z[(0, 2), (0)] are both tuple indexing and expected the consistent behavior for copy/view. If this is incorrect, please explain,

import numpy as np
Z = np.arange(36).reshape(3, 3, 4)
print("Z is \n{}\n".format(Z))

b =  Z[
    (0,2)      # Select Z[0][2]
]
print("Tuple indexing Z[(0,2)] is \n{}\nIs view? {}\n".format(
    b,
    b.base is not None
))

c = Z[         # Select Z[0][0][1] & Z[0][2][1]
    (0,2),
    (0)
]
print("Tuple indexing Z[(0, 2), (0)] is \n{}\nIs view? {}\n".format(
    c,
    c.base is not None
))

Z is 
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]

 [[24 25 26 27]
  [28 29 30 31]
  [32 33 34 35]]]

Tuple indexing Z[(0,2)] is 
[ 8  9 10 11]
Is view? True

Tuple indexing Z[(0, 2), (0)] is 
[[ 0  1  2  3]
 [24 25 26 27]]
Is view? False

Numpy 索引令人困惑,不知道人们是如何建立理解的.如果有好的理解方法或备忘单,请指教.

Numpy indexing is confusing and wonder how people built the understanding. If there is a good way to understand or cheat-sheets, please advise.

推荐答案

它是创建元组的逗号.() 只是在需要的地方设置边界.

It's the comma that creates a tuple. The () just set boundaries where needed.

因此

Z[(0,2)]
Z[0,2]

相同,在第一个二维上选择.返回的是元素还是数组取决于 Z 有多少维.

are the same, select on the first 2 dimension. Whether that returns an element, or an array depends on how many dimensions Z has.

同样的解释适用于另一种情况.

The same interpretation applies to the other case.

Z[(0, 2), (0)]
Z[( np.array([0,2]), 0)]
Z[ np.array([0,2]), 0]

是相同的 - 第一个维度使用列表/数组进行索引,因此是高级索引.这是一个副本.

are the same - the first dimensions is indexed with a list/array, and thus is advanced indexing. It's a copy.

[ 8  9 10 11]

是 3d 数组的一行;它是一个连续的 Z

is a row of the 3d array; its a contiguous block of Z

[[ 0  1  2  3]
 [24 25 26 27]]

Z 的 2 行.它们不是连续的,因此无法仅通过形状和步幅(以及数据缓冲区中的偏移量)来识别它们.

is 2 rows from Z. They aren't contiguous, so there's no way of identifying them with just shape and strides (and offset in the databuffer).

__array_interface__ 提供有关数组底层数据的详细信息

__array_interface__ gives details about the underlying data of an array

In [146]: Z = np.arange(36).reshape(3,3,4)
In [147]: Z.__array_interface__
Out[147]: 
{'data': (38255712, False),
 'strides': None,
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (3, 3, 4),
 'version': 3}
In [148]: Z.strides
Out[148]: (96, 32, 8)

对于视图:

In [149]: Z1 = Z[0,2]
In [150]: Z1
Out[150]: array([ 8,  9, 10, 11])
In [151]: Z1.__array_interface__
Out[151]: 
{'data': (38255776, False),    # 38255712+8*8
 'strides': None,
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (4,),
 'version': 3}

数据缓冲区指针是 Z 缓冲区中更远的 8 个元素.形状大大缩小.

The data buffer pointer is 8 elements further along in Z buffer. Shape is much reduced.

In [152]: Z2 = Z[[0,2],0]
In [153]: Z2
Out[153]: 
array([[ 0,  1,  2,  3],
       [24, 25, 26, 27]])
In [154]: Z2.__array_interface__
Out[154]: 
{'data': (31443104, False),     # an entirely different location
 'strides': None,
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (2, 4),
 'version': 3}

Z2 与两个选择相同:

In [158]: Z[0,0]
Out[158]: array([0, 1, 2, 3])
In [159]: Z[2,0]
Out[159]: array([24, 25, 26, 27])

不是

Z[0][0][1] & Z[0][2][1]
Z[0,0,1] & Z[0,2,1]

将其与 2 行切片进行比较:

Compare that with a 2 row slice:

In [156]: Z3 = Z[0:2,0]
In [157]: Z3.__array_interface__
Out[157]: 
{'data': (38255712, False),   # same as Z's
 'strides': (96, 8),
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (2, 4),
 'version': 3}

如果新数组可以用shapestrides和全部或部分原始数据缓冲区来描述,则返回一个视图.

A view is returned if the new array can be described with shape, strides and all or part of the original data buffer.

这篇关于numpy - 为什么 Z[(0,2)] 是视图而 Z[(0, 2), (0)] 是副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆