如何将n -长度的numpy数组追加到另一个n维数组中?

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

Situation

情况

I assumed this would be easy - but turns out there are a few restrictions. I have an empty array, that at this point, is empty and has unknown dimensions.

我原以为这很容易,但结果是有一些限制。我有一个空数组,在这一点,它是空的并且有未知的维数。

mainArray = np.array([])

later on, I want to append arrays to my main array, which are of different lengths.

稍后,我想将数组附加到主数组中,主数组的长度不同。

I have tried

我有试过

*Please assume all arrays I have attempted to append are the result of np.zeros(n)

*请假设我尝试添加的所有数组都是np. 0 (n)的结果

I have tried np.append() but this does not maintain the correct dimensions (assumes I want a linear array).

我尝试过np.append(),但是这并不能保持正确的维度(假设我需要一个线性数组)。

I have tried np.concatenate() however, this error

我尝试过np.concatenate(),但是这个错误

TypeError: only length-1 arrays can be converted to Python scalars

implies I cannot concatenate to an empty array...?

暗示我不能连接到一个空数组…?

I have tried np.vstack() but get

我尝试过np.vstack()但是get

ValueError: all the input array dimensions except for the concatenation axis must match exactly

...which implies I cannot have added arrays of different lengths?

…这意味着我不能添加不同长度的数组?

Question

问题

How can I append n-length arrays to an empty n-dimensional array?

如何将n长度数组追加到一个空的n维数组中?

update

更新

Here is an example of an output:

下面是一个输出示例:

[[0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0]]

Where length of 3 is a variable

3的长度是一个变量

2 个解决方案

#1


4  

Your starting array is not empty (ok, it does have 0 elements), and not of unknown dimensions. It has a well defined shape and number of dimensions (1d).

你的初始数组不是空的(好吧,它确实有0个元素),并且不是未知维度的。它有一个明确的形状和尺寸(1d)。

In [704]: a=np.array([])
In [705]: a.shape
Out[705]: (0,)
In [706]: a.ndim
Out[706]: 1

Some examples on concatenate that work with a

一些与a一起工作的例子。

In [708]: np.concatenate((a,a,a,a)).shape
Out[708]: (0,)
In [709]: np.concatenate((a,np.zeros(3))).shape
Out[709]: (3,)

As a general rule, don't start with an 'empty' array and try to append to it repeatedly. That's a list approach, and is not efficient with arrays. And because of the dimensionality issue, might not work.

一般来说,不要从一个“空”数组开始,并尝试重复地添加到它。这是一种列表方法,对数组无效。因为维度问题,可能行不通。

A correct way of doing a repeated append is something like:

重复添加的正确方法是:

alist = []
for i in range(3):
     alist.append([1,2,3])
np.array(alist)

Are the sublists all the same length or not? In your last example, they differ, and the array version is dtype=object. It is 1d, with pointers to lists else where in memory - i.e. glorified list.

子列表的长度是否相同?在上一个示例中,它们是不同的,数组版本是dtype=object。它是1d,具有指向内存中其他地方的列表的指针——即美化的列表。

In [710]: np.array([[0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0]])
Out[710]: array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0]], dtype=object)

This is a very different thing from what you'd get by vstack of 3 arrays of the same length.

这和vstack的3个长度相同的数组是完全不同的。

I think you need more practice with basic array construction, and the meaning of shape and dimensions. The title itself shows some confusion - array of length N array of N-dimensions. Those are very different descriptions.

我认为你需要更多地练习基本的阵列结构,以及形状和尺寸的含义。标题本身显示了N维长度N的混乱数组。这些都是非常不同的描述。

============

= = = = = = = = = = = =

The basic point with concatenate is that a (n1,m) array can be joined with a (n2,m) array to produce a (n1+n2,m) array. Similarly for other dimensions. If one array is 1d (m,) it needs to be expanded to (1,m) to perform this concatenation. vstack does this kind of expansion for you.

连接的基本要点是,一个(n1,m)数组可以与一个(n2,m)数组结合,生成一个(n1+n2,m)数组。类似的其他维度。如果一个数组是1d (m,),那么需要将它扩展到(1,m)来执行这种连接。vstack为你做了这种扩展。

This means that a (0,) shape array can be concatenated horizontally with other 1d arrays, but can't participate in a 2d vertical concatenation except with a (n2,0) array. A dimension may have size 0, but this is rarely useful, since it doesn't contain any data.

这意味着(0,)形状数组可以与其他1d数组水平连接,但除了(n2,0)数组之外,不能参与二维垂直连接。维度可能具有0大小,但这很少有用,因为它不包含任何数据。

#2


1  

One of the key concepts of numpy's ndarray is that it is rectangular: all elements at a given depth have the same length. This allows simple indexing with a tuple of positions in each dimension to be mapped onto a single flat array:

numpy的ndarray的一个关键概念是它是矩形的:在给定深度的所有元素都有相同的长度。这允许使用每个维度的位置元组进行简单的索引,并将其映射到单个平面数组中:

array[x,y,z] #where array.shape = (a,b,c)
#is equivalent to:
array.flat[b*c*x + c*y + z]

This allows very fast and efficient operation in c for many algorithms.

这为许多算法提供了非常快速和有效的c操作。

Numpy does technically support what you want to do, but the implementation is basically reverting functionality back to native lists. The way to do it with numpy is to specify the array dtype=object.

Numpy在技术上支持您想要做的事情,但是实现基本上是将功能返回到本地列表。使用numpy执行此操作的方法是指定数组dtype=object。

;TLDR

Arrays are intended to be created the correct size ahead of time and never change size. The functionality does exist, but it bypasses some of the advantages of using numpy arrays in the first place.

数组的目的是预先创建正确的大小,并且不会更改大小。这个功能确实存在,但是它首先忽略了使用numpy数组的一些优点。

#1


4  

Your starting array is not empty (ok, it does have 0 elements), and not of unknown dimensions. It has a well defined shape and number of dimensions (1d).

你的初始数组不是空的(好吧,它确实有0个元素),并且不是未知维度的。它有一个明确的形状和尺寸(1d)。

In [704]: a=np.array([])
In [705]: a.shape
Out[705]: (0,)
In [706]: a.ndim
Out[706]: 1

Some examples on concatenate that work with a

一些与a一起工作的例子。

In [708]: np.concatenate((a,a,a,a)).shape
Out[708]: (0,)
In [709]: np.concatenate((a,np.zeros(3))).shape
Out[709]: (3,)

As a general rule, don't start with an 'empty' array and try to append to it repeatedly. That's a list approach, and is not efficient with arrays. And because of the dimensionality issue, might not work.

一般来说,不要从一个“空”数组开始,并尝试重复地添加到它。这是一种列表方法,对数组无效。因为维度问题,可能行不通。

A correct way of doing a repeated append is something like:

重复添加的正确方法是:

alist = []
for i in range(3):
     alist.append([1,2,3])
np.array(alist)

Are the sublists all the same length or not? In your last example, they differ, and the array version is dtype=object. It is 1d, with pointers to lists else where in memory - i.e. glorified list.

子列表的长度是否相同?在上一个示例中,它们是不同的,数组版本是dtype=object。它是1d,具有指向内存中其他地方的列表的指针——即美化的列表。

In [710]: np.array([[0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0]])
Out[710]: array([[0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0]], dtype=object)

This is a very different thing from what you'd get by vstack of 3 arrays of the same length.

这和vstack的3个长度相同的数组是完全不同的。

I think you need more practice with basic array construction, and the meaning of shape and dimensions. The title itself shows some confusion - array of length N array of N-dimensions. Those are very different descriptions.

我认为你需要更多地练习基本的阵列结构,以及形状和尺寸的含义。标题本身显示了N维长度N的混乱数组。这些都是非常不同的描述。

============

= = = = = = = = = = = =

The basic point with concatenate is that a (n1,m) array can be joined with a (n2,m) array to produce a (n1+n2,m) array. Similarly for other dimensions. If one array is 1d (m,) it needs to be expanded to (1,m) to perform this concatenation. vstack does this kind of expansion for you.

连接的基本要点是,一个(n1,m)数组可以与一个(n2,m)数组结合,生成一个(n1+n2,m)数组。类似的其他维度。如果一个数组是1d (m,),那么需要将它扩展到(1,m)来执行这种连接。vstack为你做了这种扩展。

This means that a (0,) shape array can be concatenated horizontally with other 1d arrays, but can't participate in a 2d vertical concatenation except with a (n2,0) array. A dimension may have size 0, but this is rarely useful, since it doesn't contain any data.

这意味着(0,)形状数组可以与其他1d数组水平连接,但除了(n2,0)数组之外,不能参与二维垂直连接。维度可能具有0大小,但这很少有用,因为它不包含任何数据。

#2


1  

One of the key concepts of numpy's ndarray is that it is rectangular: all elements at a given depth have the same length. This allows simple indexing with a tuple of positions in each dimension to be mapped onto a single flat array:

numpy的ndarray的一个关键概念是它是矩形的:在给定深度的所有元素都有相同的长度。这允许使用每个维度的位置元组进行简单的索引,并将其映射到单个平面数组中:

array[x,y,z] #where array.shape = (a,b,c)
#is equivalent to:
array.flat[b*c*x + c*y + z]

This allows very fast and efficient operation in c for many algorithms.

这为许多算法提供了非常快速和有效的c操作。

Numpy does technically support what you want to do, but the implementation is basically reverting functionality back to native lists. The way to do it with numpy is to specify the array dtype=object.

Numpy在技术上支持您想要做的事情,但是实现基本上是将功能返回到本地列表。使用numpy执行此操作的方法是指定数组dtype=object。

;TLDR

Arrays are intended to be created the correct size ahead of time and never change size. The functionality does exist, but it bypasses some of the advantages of using numpy arrays in the first place.

数组的目的是预先创建正确的大小,并且不会更改大小。这个功能确实存在,但是它首先忽略了使用numpy数组的一些优点。