如何将N个长度的Numpy数组附加到另​​一个N维数组? [英] How can I append a numpy array of N-Length to another array of N-dimensions?

查看:103
本文介绍了如何将N个长度的Numpy数组附加到另​​一个N维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

我认为这很容易-但事实证明存在一些限制. 我有一个空数组,此时,它是空的并且尺寸未知.

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.

我尝试过

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

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

我尝试过np.append(),但这不能保持正确的尺寸(假设我想要线性数组).

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

我尝试过np.concatenate(),但此错误

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

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

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

我尝试了np.vstack()但得到了

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

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

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

问题

如何将n长度数组附加到空的n维数组?

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

更新

以下是输出示例:

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

长度为3的变量

推荐答案

您的起始数组不为空(好的,它确实有0个元素),并且尺寸未知.它具有定义明确的形状和尺寸数(1d).

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).

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

有关连接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,)

作为一般规则,请勿以空"数组开头,并尝试重复附加到该数组.这是一种list方法,对于数组而言效率不高.而且由于尺寸问题,可能无法正常工作.

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)

子列表的长度是否相同?在您的最后一个示例中,它们不同,并且阵列版本为dtype=object.它是1d,指向内存中其他列表的指针-即荣耀列表.

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.

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)

这与通过vstack相同长度的3个数组得到的结果完全不同.

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

我认为您需要更多有关基本数组构造以及shape和尺寸的含义的练习.标题本身显示出一些混乱-array of length N array of N-dimensions.这些是完全不同的描述.

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.

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

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

concatenate的基本要点是(n1,m)数组可以与(n2,m)数组连接在一起以生成(n1+n2,m)数组.其他尺寸也一样.如果一个数组为1d (m,),则需要将其扩展为(1,m)来执行此串联. vstack为您进行这种扩展.

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.

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

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.

这篇关于如何将N个长度的Numpy数组附加到另​​一个N维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆