如何将numpy.array追加到其他numpy.array?

时间:2021-09-10 21:42:09

I want to create 2D numpy.array knowing at the begining only its shape, i.e shape=2. Now, I want to create in for loop ith one dimensional numpy.arrays, and add them to the main matrix of shape=2, so I'll get something like this:

我想创建2D numpy.array只知道它的形状,即shape = 2。现在,我想在for循环中创建一维numpy.arrays,并将它们添加到shape = 2的主矩阵中,所以我会得到这样的东西:

matrix=
[numpy.array 1]
[numpy.array 2]
...
[numpy.array n]

How can I achieve that? I try to use:

我怎样才能做到这一点?我尝试使用:

matrix = np.empty(shape=2)
for i in np.arange(100):
    array = np.zeros(random_value)
    matrix = np.append(matrix, array)

But as a result of print(np.shape(matrix)), after loop, I get something like:

但是由于print(np.shape(matrix)),在循环之后,我得到类似的东西:

(some_number, )

How can I append each new array in the next row of the matrix? Thank you in advance.

如何将每个新数组附加到矩阵的下一行?先感谢您。

3 个解决方案

#1


I would suggest working with list

我建议使用列表

matrix = []

for i in range(10):
    a = np.ones(2)
    matrix.append(a)

matrix = np.array(matrix)

list does not have the downside of being copied in the memory everytime you use append. so you avoid the problem described by ali_m. at the end of your operation you just convert the list object into a numpy array.

每次使用追加时,list都没有被复制到内存中的缺点。所以你避免了ali_m描述的问题。在操作结束时,您只需将列表对象转换为numpy数组。

#2


I suspect the root of your problem is the meaning of 'shape' in np.empty(shape=2)

我怀疑你的问题的根源是np.empty(shape = 2)中'shape'的含义

If I run a small version of your code

如果我运行一个小版本的代码

matrix = np.empty(shape=2)
for i in np.arange(3):
    array = np.zeros(3)
    matrix = np.append(matrix, array)

I get

array([  9.57895902e-259,   1.51798693e-314,   0.00000000e+000,
     0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
     0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
     0.00000000e+000,   0.00000000e+000])

See those 2 odd numbers at the start? Those are produced by np.empty(shape=2). That matrix starts as a (2,) shaped array, not an empty 2d array. append just adds sets of 3 zeros to that, resulting in a (11,) array.

在开始时看到那两个奇数?那些是由np.empty(shape = 2)产生的。该矩阵以(2)形状的数组开始,而不是空的2d数组。 append只是添加3个零的集合,产生一个(11,)数组。

Now if you started with a 2 array with the right number of columns, and did concatenate on the 1st dimension you would get a multirow array. (rows only have meaning in 2d or larger).

现在,如果你开始使用具有正确数量的列的2数组,并且在第一维上连接,那么你将得到一个多行数组。 (行仅具有2d或更大的含义)。

mat=np.zeros((1,3))
for i in range(1,3):
    mat = np.concatenate([mat, np.ones((1,3))*i],axis=0)

produces:

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

A better way of doing an iterative construction like this is with list append

像这样进行迭代构造的更好方法是使用list append

alist = []
for i in range(0,3):
    alist.append(np.ones((1,3))*i)
mat=np.vstack(alist)

alist is:

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

mat is

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

With vstack you can get by with np.ones((3,), since it turns all of its inputs into 2d array.

使用vstack,您可以使用np.ones((3,),因为它将所有输入转换为2d数组。

append would work, but it also requires axis=0 parameter, and 2 arrays. It gets misused, often by mistaken analogy to the list append. It is just another front end to concatenate. So I prefer not to use it.

append可以工作,但它还需要axis = 0参数和2个数组。它被滥用,通常被错误地类比为列表追加。它只是连接的另一个前端。所以我不想使用它。

Notice that other posters assumed your random value changed during the iteration. That would produce a arrays of differing lengths. For 1d appending that would still produce the long 1d array. But a 2d append wouldn't work, because an 2d array can't be ragged.

请注意,其他海报假设您的随机值在迭代期间发生了变化。这将产生不同长度的阵列。对于1d追加仍然会产生长1d数组。但是2d追加不起作用,因为2d数组不能被破坏。

mat = np.zeros((2,),int)
for i in range(4):
    mat=np.append(mat,np.ones((i,),int)*i)
# array([0, 0, 1, 2, 2, 3, 3, 3])

#3


The function you are looking for is np.vstack

您正在寻找的功能是np.vstack

Here is a modified version of your example

这是您的示例的修改版本

import numpy as np

matrix = np.empty(shape=2)

for i in np.arange(3):
    array = np.zeros(2)
    matrix = np.vstack((matrix, array))

The result is

结果是

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

#1


I would suggest working with list

我建议使用列表

matrix = []

for i in range(10):
    a = np.ones(2)
    matrix.append(a)

matrix = np.array(matrix)

list does not have the downside of being copied in the memory everytime you use append. so you avoid the problem described by ali_m. at the end of your operation you just convert the list object into a numpy array.

每次使用追加时,list都没有被复制到内存中的缺点。所以你避免了ali_m描述的问题。在操作结束时,您只需将列表对象转换为numpy数组。

#2


I suspect the root of your problem is the meaning of 'shape' in np.empty(shape=2)

我怀疑你的问题的根源是np.empty(shape = 2)中'shape'的含义

If I run a small version of your code

如果我运行一个小版本的代码

matrix = np.empty(shape=2)
for i in np.arange(3):
    array = np.zeros(3)
    matrix = np.append(matrix, array)

I get

array([  9.57895902e-259,   1.51798693e-314,   0.00000000e+000,
     0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
     0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
     0.00000000e+000,   0.00000000e+000])

See those 2 odd numbers at the start? Those are produced by np.empty(shape=2). That matrix starts as a (2,) shaped array, not an empty 2d array. append just adds sets of 3 zeros to that, resulting in a (11,) array.

在开始时看到那两个奇数?那些是由np.empty(shape = 2)产生的。该矩阵以(2)形状的数组开始,而不是空的2d数组。 append只是添加3个零的集合,产生一个(11,)数组。

Now if you started with a 2 array with the right number of columns, and did concatenate on the 1st dimension you would get a multirow array. (rows only have meaning in 2d or larger).

现在,如果你开始使用具有正确数量的列的2数组,并且在第一维上连接,那么你将得到一个多行数组。 (行仅具有2d或更大的含义)。

mat=np.zeros((1,3))
for i in range(1,3):
    mat = np.concatenate([mat, np.ones((1,3))*i],axis=0)

produces:

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

A better way of doing an iterative construction like this is with list append

像这样进行迭代构造的更好方法是使用list append

alist = []
for i in range(0,3):
    alist.append(np.ones((1,3))*i)
mat=np.vstack(alist)

alist is:

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

mat is

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

With vstack you can get by with np.ones((3,), since it turns all of its inputs into 2d array.

使用vstack,您可以使用np.ones((3,),因为它将所有输入转换为2d数组。

append would work, but it also requires axis=0 parameter, and 2 arrays. It gets misused, often by mistaken analogy to the list append. It is just another front end to concatenate. So I prefer not to use it.

append可以工作,但它还需要axis = 0参数和2个数组。它被滥用,通常被错误地类比为列表追加。它只是连接的另一个前端。所以我不想使用它。

Notice that other posters assumed your random value changed during the iteration. That would produce a arrays of differing lengths. For 1d appending that would still produce the long 1d array. But a 2d append wouldn't work, because an 2d array can't be ragged.

请注意,其他海报假设您的随机值在迭代期间发生了变化。这将产生不同长度的阵列。对于1d追加仍然会产生长1d数组。但是2d追加不起作用,因为2d数组不能被破坏。

mat = np.zeros((2,),int)
for i in range(4):
    mat=np.append(mat,np.ones((i,),int)*i)
# array([0, 0, 1, 2, 2, 3, 3, 3])

#3


The function you are looking for is np.vstack

您正在寻找的功能是np.vstack

Here is a modified version of your example

这是您的示例的修改版本

import numpy as np

matrix = np.empty(shape=2)

for i in np.arange(3):
    array = np.zeros(2)
    matrix = np.vstack((matrix, array))

The result is

结果是

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