动态Python数组切片 [英] Dynamic Python Array Slicing
问题描述
我面临的情况是我有一个非常大的numpy.ndarray
(实际上是hdf5数据集),我需要快速找到一个子集,因为它们的整个数组无法保存在内存中.但是,我也不想遍历这样的数组(即使声明内置的numpy迭代器也会抛出MemoryError
),因为我的脚本实际上需要几天才能运行.
I am facing a situation where I have a VERY large numpy.ndarray
(really, it's an hdf5 dataset) that I need to find a subset of quickly because they entire array cannot be held in memory. However, I also do not want to iterate through such an array (even declaring the built-in numpy iterator throws a MemoryError
) because my script would take literally days to run.
因此,我面临着遍历数组某些维度的情况,以便可以对完整数组的精简子集执行数组操作.为此,我需要能够动态切出数组的一个子集.动态切片意味着构造一个元组并将其传递.
As such, I'm faced with the situation of iterating through some dimensions of the array so that I can perform array-operations on pared down subsets of the full array. To do that, I need to be able to dynamically slice out a subset of the array. Dynamic slicing means constructing a tuple and passing it.
例如,代替
my_array[0,0,0]
我可能会使用
my_array[(0,0,0,)]
这是问题所在:如果我想沿着数组的特定维度/轴手动切出所有值,我可以做类似的事情
Here's the problem: if I want to slice out all values along a particular dimension/axis of the array manually, I could do something like
my_array[0,:,0]
> array([1, 4, 7])
但是,如果我使用元组,这是行不通的:
However, I this does not work if I use a tuple:
my_array[(0,:,0,)]
我会得到一个SyntaxError
.
当我必须动态构造切片以将某些内容放在数组的括号中时,该怎么办?
How can I do this when I have to construct the slice dynamically to put something in the brackets of the array?
推荐答案
You could slice automaticaly using python's slice
:
>>> a = np.random.rand(3, 4, 5)
>>> a[0, :, 0]
array([ 0.48054702, 0.88728858, 0.83225113, 0.12491976])
>>> a[(0, slice(None), 0)]
array([ 0.48054702, 0.88728858, 0.83225113, 0.12491976])
slice
方法读取为slice(*start*, stop[, step])
.如果仅传递一个参数,则将其解释为slice(0, stop)
.
The slice
method reads as slice(*start*, stop[, step])
. If only one argument is passed, then it is interpreted as slice(0, stop)
.
在上面的示例中,:
被翻译为slice(0, end)
,等效于slice(None)
.
In the example above :
is translated to slice(0, end)
which is equivalent to slice(None)
.
其他切片示例:
:5 -> slice(5)
1:5 -> slice(1, 5)
1: -> slice(1, None)
1::2 -> slice(1, None, 2)
这篇关于动态Python数组切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!