简体   繁体   English

如何迭代这个n维数据集?

[英]How to iterate over this n-dimensional dataset?

I have a dataset which has 4 dimensions (for now...) and I need to iterate over it. 我有一个有4个维度的dataset (现在......),我需要迭代它。

To access a value in the dataset , I do this: 要访问dataset的值,我这样做:

value = dataset[i,j,k,l]

Now, I can get the shape for the dataset : 现在,我可以获得datasetshape

shape = [4,5,2,6]

The values in shape represent the length of the dimension. shape的值表示维度的长度。

How, given the number of dimensions, can I iterate over all the elements in my dataset? 考虑到维度的数量,我可以如何迭代数据集中的所有元素? Here is an example: 这是一个例子:

for i in range(shape[0]):
    for j in range(shape[1]):
        for k in range(shape[2]):
            for l in range(shape[3]):
                print('BOOM')
                value = dataset[i,j,k,l]

In the future, the shape may change. 将来, shape可能会发生变化。 So for example, shape may have 10 elements rather than the current 4. 因此,例如, shape可能有10个元素而不是当前4个元素。

Is there a nice and clean way to do this with Python 3? 使用Python 3有一个很好的,干净的方法吗?

You could use itertools.product to iterate over the cartesian product 1 of some values (in this case the indices): 您可以使用itertools.product迭代某些值的笛卡尔积 1 (在本例中为索引):

import itertools
shape = [4,5,2,6]
for idx in itertools.product(*[range(s) for s in shape]):
    value = dataset[idx]
    print(idx, value)
    # i would be "idx[0]", j "idx[1]" and so on...

However if it's a numpy array you want to iterate over, it could be easier to use np.ndenumerate : 但是,如果这是你想要遍历一个numpy的阵列,它可能是更容易使用np.ndenumerate

import numpy as np

arr = np.random.random([4,5,2,6])
for idx, value in np.ndenumerate(arr):
    print(idx, value)
    # i would be "idx[0]", j "idx[1]" and so on...

1 You asked for clarification what itertools.product(*[range(s) for s in shape]) actually does. 1您要求澄清itertools.product(*[range(s) for s in shape])实际上是做什么的。 So I'll explain it in more details. 所以我会更详细地解释一下。

For example is you have this loop: 例如,你有这个循环:

for i in range(10):
    for j in range(8):
        # do whatever

This can also be written using product as: 这也可以使用以下product编写:

for i, j in itertools.product(range(10), range(8)):
#                                        ^^^^^^^^---- the inner for loop
#                             ^^^^^^^^^-------------- the outer for loop
    # do whatever

That means product is just a handy way of reducing the number of independant for-loops. 这意味着product只是减少独立 for循环数量的一种方便方法。

If you want to convert a variable number of for -loops to a product you essentially need two steps: 如果要将可变数量的for -loops转换为product ,则基本上需要两个步骤:

# Create the "values" each for-loop iterates over
loopover = [range(s) for s in shape]

# Unpack the list using "*" operator because "product" needs them as 
# different positional arguments:
prod = itertools.product(*loopover)

for idx in prod:
     i_0, i_1, ..., i_n = idx   # index is a tuple that can be unpacked if you know the number of values.
                                # The "..." has to be replaced with the variables in real code!
     # do whatever

That's equivalent to: 这相当于:

for i_1 in range(shape[0]):
    for i_2 in range(shape[1]):
        ... # more loops
            for i_n in range(shape[n]):  # n is the length of the "shape" object
                # do whatever

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM