python - 使用 bool 值或序列对象获取 numpy 数组的 View (高级索引)

标签 python arrays numpy

如何通过 bool 值或整数元组作为索引返回 numpy 数组的 View (而不是副本)?

The trouble is this typically returns a copy:

Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.

Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view).

我这样做的动机是节省内存。这是问题的一个简单示例:

import numpy as np

big_number = 10
x = np.ones((big_number, big_number, big_number))

#
sub_array = np.s_[(1, 2, 3, 5, 7), :, :]
y = x[sub_array]
print(y.flags['OWNDATA'])

True

一般来说,索引元组(1,2,3,5,7)没有任何结构,所以我很困惑如何将它调整为基本 numpy 索引所需的常规步幅

最佳答案

可视化两个数组是否可以共享内存的一种方法是查看它们的“ravel”

In [422]: x = np.arange(24).reshape((4,3,2))
In [423]: x
Out[423]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[ 6,  7],
        [ 8,  9],
        [10, 11]],

       [[12, 13],
        [14, 15],
        [16, 17]],

       [[18, 19],
        [20, 21],
        [22, 23]]])
In [424]: y = x[[1,3,0,2],:,:]  # rearrange the 1st axis
In [425]: y
Out[425]: 
array([[[ 6,  7],
        [ 8,  9],
        [10, 11]],

       [[18, 19],
        [20, 21],
        [22, 23]],

       [[ 0,  1],
        [ 2,  3],
        [ 4,  5]],

       [[12, 13],
        [14, 15],
        [16, 17]]])

In [428]: x.ravel(order='K')
Out[428]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
In [429]: y.ravel(order='K')
Out[429]: 
array([ 6,  7,  8,  9, 10, 11, 18, 19, 20, 21, 22, 23,  0,  1,  2,  3,  4,
        5, 12, 13, 14, 15, 16, 17])

注意 y 中的元素如何以不同的顺序出现。我们无法“跨越”x 来获取 y

如果没有 order 参数,ravel 使用“C”,当新数组进行某种轴转置时,这可能会让我们感到困惑。正如另一个答案中所指出的,x.T 是一个 View ,通过重新排序轴来实现,从而改变步幅。

In [430]: x.T.ravel() # 逐行查看转置数组 输出[430]: 数组([ 0, 6, 12, 18, 2, 8, 14, 20, 4, 10, 16, 22, 1, 7, 13, 19, 3, 9, 15, 21, 5, 11, 17, 23]) In [431]: x.T.ravel(order='K') # 逐列查看转置数组 输出[431]: 数组([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])

__array_interface__ 是查看数组底层结构的便捷工具:

In [432]: x.__array_interface__
Out[432]: 
{'data': (45848336, False),
 'strides': None,
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (4, 3, 2),
 'version': 3}
In [433]: y.__array_interface__
Out[433]: 
{'data': (45892944, False),
 'strides': None,
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (4, 3, 2),
 'version': 3}
In [434]: x.T.__array_interface__
Out[434]: 
{'data': (45848336, False),     # same as for x
 'strides': (8, 16, 48),        # reordered strides
 'descr': [('', '<i8')],
 'typestr': '<i8',
 'shape': (2, 3, 4),
 'version': 3}

关于python - 使用 bool 值或序列对象获取 numpy 数组的 View (高级索引),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593720/

相关文章:

python - py2neo:具有多个键/值的 Graph.find_one

python - pymongo 没有关闭连接

javascript - 根据 JS 数组中的值将元素追加到 DOM

java - 如何将同一个文本文件中的整数和字符串添加到不同的数组中

python - 在 Pandas/Python 中用多个连续的 nan 向后插值?

python - numpy.gradient() 似乎产生错误的边界值(使用一阶差分)

python - statsmodels add_constant 用于 OLS 拦截,这实际上在做什么?

python - Django - 根据字段出现的次数进行过滤

Java,格式化字符串数组

python - 高效地从数组中获取批量元素 - Python