[英]Dynamic Python Array Slicing
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. 我面临的情况是,我有一个非常大的numpy.ndarray
(实际上,它是一个hdf5数据集),我需要快速找到一个子集,因为它们的整个数组不能保存在内存中。 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. 但是,我也不想迭代这样的数组(甚至声明内置的numpy迭代器抛出一个MemoryError
),因为我的脚本需要几天才能运行。
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. 动态切片意味着构造一个元组并传递它。
For example, instead of 例如,而不是
my_array[0,0,0]
I might use 我可能会用
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,)]
where I'll get a SyntaxError
. 我会得到一个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
: 您可以使用python的slice
自动 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])
The slice
method reads as slice(*start*, stop[, step])
. slice
方法读取为slice(*start*, stop[, step])
。 If only one argument is passed, then it is interpreted as slice(0, stop)
. 如果只传递一个参数,则将其解释为slice(0, stop)
。
In the example above :
is translated to slice(0, end)
which is equivalent to slice(None)
. 在上面的示例中:
转换为slice(0, end)
,它相当于slice(None)
。
Other slice examples: 其他切片示例:
:5 -> slice(5)
1:5 -> slice(1, 5)
1: -> slice(1, None)
1::2 -> slice(1, None, 2)
Okay, I finally found an answer just as someone else did. 好吧,我终于像其他人一样找到了答案。
Suppose I have array: 假设我有数组:
my_array[...]
>array(
[[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]],
[[10, 11, 12],
[13, 14, 15],
[16, 17, 18]]])
I can use the slice
object, which apparently is a thing: 我可以使用slice
对象,这显然是一件事:
sl1 = slice( None )
sl2 = slice( 1,2 )
sl3 = slice( None )
ad_array.matrix[(sl1, sl2, sl3)]
>array(
[[[ 4, 5, 6]],
[[13, 14, 15]]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.