遍历一个包含未知数量的 numpy 数组作为其值的 n 维 numpy 数组。

huangapple go评论80阅读模式
英文:

Iterate over a n-dimensional numpy array which contains unknown number of numpy array as its values

问题

我有一个如下形式的numpy数组:

[[array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ]) 0 0]
 [0 array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]) 0]
 [0 0 0]]

这个数组将是N维的,在特定位置上,我会有需要迭代的数组。而我不知道最终的维度,或者会有多少个数组。

我该如何编写一个程序/函数,以便获得所有可能的N维张量/矩阵,每个位置只有一个值?(即,在存在数组的位置,我需要对它们进行迭代,并生成一个仅在每个位置具有单个值的张量/矩阵列表)。

我不知道这是否重要,但每个方向的最大长度为3。

(问题的物理意义:基本上,我想为另一个软件创建输入文件。我需要迭代我将指定的电场方向上的变化。所以,在1维中,我将只有X、Y和Z,对于2维,我将有XX、YY、ZZ、XZ等等。在每个位置上,我指示要迭代的值数组,例如linspace(10, 100, 10)等)。

先提前感谢。

我甚至不知道如何着手解决这个问题,或者如何搜索它。我在考虑也许创建一些额外的函数来帮助我迭代等等。

因此,期望的结果是,在每次迭代之后,我得到一个仅包含其中的数组值的numpy数组,以便我可以将其提交给另一个函数。例如,从上面的np.array中得到的结果将是:

首先,

[[1 0 0]
 [0 1 0]
 [0 0 0]]

其次,

[[1 0 0]
 [0 2 0]
 [0 0 0]]

...

最后,

[10 0 0]
 [0 10 0]
 [0 0 0]]
英文:

I have a numpy array of the following form

[[array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ]) 0 0]
 [0 array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]) 0]
 [0 0 0]]

The array will be N dimensional, and at specific positions, I will have arrays over which I need to iterate. And I do not know the final dimension, or how many arrays I will have.

How do I write a program/function so that I get all the possible N dimensional tensors/matrices with only a single value at each position? (i.e., in positions where there are arrays, I need to iterate over them and I need to generate a list of tensor/matrices having only singles values at each position.

I do not know if it is important, but the maximum length in each direction is 3.

(Physical meaning of the problem: basically, I want to create input files for another software. I need to iterate over variation of electric field in the directions which I will specify. So, in 1 dimension, I will have only X, Y and Z, for 2 dimensions I will have XX, YY, ZZ, XZ, etc. And in each position I indicate the arrays of values over which I want to iterate, i.e. for example a linspace(10, 100, 10) etc).

Thanks in advance.

I do not even know how to approach it, or how to google it. I was thinking maybe creating some additional function to help me somehow iterate etc.

So, the desired result is that after each iteration I get a numpy array with only values from arrays inside it, so I can submit it to another function. For example, the resulting np.arrays from the np.array above would be

First,

[[1 0 0]
 [0 1 0]
 [0 0 0]]
Second, 

[[1 0 0]
 [0 2 0]
 [0 0 0]]

...

Last, 

[10 0 0]
 [0 10 0]
 [0 0 0]]

答案1

得分: 0

内置的itertools模块有一个完美的函数适用于此:product

然后只需处理numpy数组。我发现关键是创建一个布尔值掩码,指示嵌套数组的位置。

我只在3x3矩阵上进行了测试,但我认为它应该适用于任何数组。

from itertools import product
import numpy as np

array = np.array([[np.array([1, 2]), 0.5, 0.75],
                  [-0.5, np.array([10, 20]), -0.75],
                  [np.array([-1, -2]), 3.5, 3.75]], dtype=object)

# 创建一个矢量化函数来查找嵌套数组
is_cell_an_array = np.vectorize(lambda cell: isinstance(cell, np.ndarray))
# 创建一个布尔值数组,指示嵌套单元格的位置
nested_mask = is_cell_an_array(array)

# 使用itertools.product生成所有嵌套数组的组合
for permutation in product(*array[nested_mask]):
    # 创建一个新数组
    new_array = np.zeros(array.shape, dtype=float)
    # 复制嵌套数组的唯一组合到此循环中
    new_array[nested_mask] = permutation
    # 从原始数组复制其余值
    new_array[~nested_mask] = array[~nested_mask]

    print(new_array)
英文:

The builtin itertools module has the perfect function for this: product

Then it's just a matter of handling the numpy array. I found the key to that was creating a mask of boolean values indicating where the nested arrays are.

I've only tested it on a 3x3 matrix but I think it should work on any array.

from itertools import product

import numpy as np


array = np.array([[np.array([1, 2]), 0.5, 0.75],
                  [-0.5, np.array([10, 20]), -0.75],
                  [np.array([-1, -2]), 3.5, 3.75]], dtype=object)

# create a vectorized function to find nested arrays
is_cell_an_array = np.vectorize(lambda cell: isinstance(cell, np.ndarray))
# create an array of booleans indicating where the nested cells are
nested_mask = is_cell_an_array(array)

# use itertools.product to generate all the combinations of the nested arrays
for permutation in product(*array[nested_mask]):
    # create a new array
    new_array = np.zeros(array.shape, dtype=float)
    # copy in the unique combination of the nested arrays for this loop
    new_array[nested_mask] = permutation
    # copy in the rest of the values from the original array
    new_array[~nested_mask] = array[~nested_mask]

    print(new_array)

huangapple
  • 本文由 发表于 2023年7月17日 15:46:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定