简体   繁体   English

我可以从 numpy 数组列表中创建一个具有一个可变维度的 numpy 数组吗?

[英]Can I create a numpy array with one variable dimension from a list of numpy arrays?

Here is a miminum example of my problem, I have a list of numpy arrays that look like this:这是我的问题的最小示例,我有一个如下所示的 numpy 数组列表:

a = np.zeros([4,3])
b = np.ones([5,3])
my_list = [a, b]

my_list
[array([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]), array([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])]

Arrays in the list could have variable length in one (and only one) of the dimension (here 4 or 5).列表中的数组可以在一个(并且只有一个)维度(此处为 4 或 5)中具有可变长度。

What I would like to have eventually is a numpy array that is of dimension (2, "Variable-size", 3).我最终想要的是一个尺寸为(2,“Variable-size”,3)的numpy数组。 The output should look something like the following:输出应如下所示:

array([[[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],
        [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]])

np.vstack() or np.concatenate() do not return me the shape I am looking for as they are producing an output that has a (9, 3) shape. np.vstack() 或 np.concatenate() 不会返回我正在寻找的形状,因为它们正在生成具有 (9, 3) 形状的输出。

You can't have numpy array with the shape: (2, "Variable-size", 3) , but you can concatenate two arrays with the shape ("Variable-size", 3) to (shape1[0] + shape2[0], 3) .您不能使用形状为 numpy 的数组: (2, "Variable-size", 3) ,但您可以将两个形状为("Variable-size", 3)的数组连接到(shape1[0] + shape2[ 0], 3) . As you wrote:正如你所写:

I am looking for as they are producing an output that has a (9, 3) shape我正在寻找,因为他们正在生成具有 (9, 3) 形状的输出

numpy.concatenate()

can be a solution to your problem:可以解决您的问题:

np.concatenate((a,b))

Out:出去:

array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

If you still want to maintain a variable 3rd dimension in numpy, the only way to do it with padding , here with zero padding :如果您仍想在 numpy 中保持可变的第三维,则使用padding进行此操作的唯一方法,此处使用零填充

import numpy as np

a = np.array([
    [50., 50., 50.],
    [50., 50., 50.],
    [50., 50., 50.],
    [50., 50., 50.],
    [0., 0., 0.] # zero padding 
])

b = np.array([
    [80., 80., 80.],
    [80., 80., 80.],
    [80., 80., 80.],
    [80., 80., 80.],
    [80., 80., 80.]
])

new_matrix = [a, b]

print(new_matrix)

Out:出去:

[array([[50., 50., 50.],
       [50., 50., 50.],
       [50., 50., 50.],
       [50., 50., 50.],
       [ 0.,  0.,  0.]]), array([[80., 80., 80.],
       [80., 80., 80.],
       [80., 80., 80.],
       [80., 80., 80.],
       [80., 80., 80.]])]

This method is usable widely at a sort of image processing solutions.该方法可广泛用于一种图像处理解决方案。 With this trick you can leverage all the positive properties of matrix operations in numpy too, but you can keep your data shape relatively flexible.通过这个技巧,您也可以利用numpy中矩阵运算的所有积极属性,但您可以保持数据形状相对灵活。 As Willem mentioned above numpy only work with "rectangular" data and operations with variable dimensional matrices would be in the most situation ambiguous.正如威廉上面提到的,numpy 仅适用于“矩形”数据,而具有可变维数矩阵的操作在大多数情况下都是模棱两可的。

If you don't even want to use any of the mentioned solution, you have to choose list and numpy array combinations ie numpy arrays with any dimensions in a list.如果您甚至不想使用任何上述解决方案,则必须选择列表和 numpy 数组组合,即列表中具有任何维度的 numpy 数组。

在第三轴上堆叠具有相同维度的二维数组时我做了什么:

var_vid = np.concatenate((var_vid, np.expand_dims(new_array, axis=2)), axis=2)

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

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