在numpy中创建具有不同维度的数组数组 [英] Creating array of arrays in numpy with different dimensions

查看:95
本文介绍了在numpy中创建具有不同维度的数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一组 numpy 数组,每个数组都有不同的维度.到目前为止,似乎还不错.例如,如果我运行:

I'm trying to create an array of numpy arrays, each one with a different dimension. So far, it seems to be fine. For example, if I run:

np.array([np.zeros((10,3)), np.zeros((11,8))])

结果是:

array([ array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]]),
       array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])], dtype=object)

两个矩阵的维数完全不同,生成数组没有任何问题.但是,如果两个矩阵的第一个维度相同,则不再起作用:

The dimension of the two matrices are completely different and the array is generated without any problem. However, if the first dimension of the two matrices is the same, it doesn't work anymore:

np.array([np.zeros((10,3)), np.zeros((10,8))])
Traceback (most recent call last):

  File "<ipython-input-123-97301e1424ae>", line 1, in <module>
    a=np.array([np.zeros((10,3)), np.zeros((10,8))])

ValueError: could not broadcast input array from shape (10,3) into shape (10)

这是怎么回事?

谢谢!

推荐答案

这之前已经解决了 (为什么我在尝试转换 np.array(some_list) ValueError: 无法广播输入数组时出错;长度不等的数组中的Numpy数组).基本上 np.array 做三件事之一:

This has been hashed out before (Why do I get error trying to cast np.array(some_list) ValueError: could not broadcast input array; Numpy array in array with unequal length). Basically np.array does one of 3 things:

  • 制作一个基本数据类型的 n 维数组,例如漂浮.

  • make an n-dimensional array of a basic dtype, e.g. float.

创建一个对象数据类型数组

make an object dtype array

提出错误,说前两个不可能.

raise an error, saying the first two are not possible.

后两个选项是后备选项,仅当第一个不可能时才采用.

These second two alternatives are fallback options, taken only if the first is impossible.

没有深入研究编译代码如何工作的细节,显然会发生什么

Without digging into the details of how the compiled code works, apparently what happens with

np.array([np.zeros((10,3)), np.zeros((10,8))])

是它首先看到了共同的第一维,并从中推断出它可以采取第一选择.看起来它初始化了一个 (10,2) 数组(列表中有 2 个项目),并试图将第一个数组放入第一行,因此将 (10,3) 数组放入 (10,) 插槽.

is that it first sees the common first dimension, and deduces from that that it can take the first choice. It looks like it initialed a (10,2) array (2 items in your list), and tried to put the first array into the first row, hence the failed attempt to put a (10,3) array into a (10,) slot.

因此,如果您真的想要一个对象 dtype 数组,并且没有遇到第 1 种或第 3 种情况,则需要进行某种迂回"创建.

So if you really want an object dtype array, and not hit either the 1st or 3rd cases, you need to do some sort of 'round-about' creation.

PaulP 和我一直在探索 强制 numpy 创建对象数组的替代方案

PaulP and I have been exploring alternatives in Force numpy to create array of objects

之前:如何创建一个 numpy 列表数组?

在这个问题中,我建议进行此迭代:

In this question I suggest this iteration:

A=np.empty((3,),dtype=object)
for i,v in enumerate(A): A[i]=[v,i]

或者你的情况

In [451]: res = np.empty(2, object)
In [452]: alist = [np.zeros((10,3)), np.zeros((10,8))]
In [453]: for i,v in enumerate(alist):
     ...:     res[i] = v

防止 numpy 创建多维数组

与其对 alist 进行迭代,不如这样做:

Rather than iterate on alist, it may work to do:

res[:] = alist

它似乎在我尝试过的大多数情况下都有效,但如果您广播错误,请不要感到惊讶.

It seems to work in most cases that I've tried, but don't be surprised if you broadcasting errors.

这篇关于在numpy中创建具有不同维度的数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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